将此代码从C#转换为Objective-C,这是正确的吗?

时间:2013-01-18 07:13:33

标签: objective-c

我正在进行一些身份验证,并且已经提供了示例C#代码,我需要将其转换为Objective-C,因为完整性检查有人可以验证以下代码是否相同?

谢谢。

C#

        // Create Byte array of password string
        ASCIIEncoding encoder = new ASCIIEncoding();
        Byte[] _secretBytes = encoder.GetBytes(password);

        // Create a new salt
        Byte[] _saltBytes = new Byte[4];
        _saltBytes[0] = (byte)(salt >> 24);
        _saltBytes[1] = (byte)(salt >> 16);
        _saltBytes[2] = (byte)(salt >> 8);
        _saltBytes[3] = (byte)(salt);

        // append the two arrays
        Byte[] toHash = new Byte[_secretBytes.Length + _saltBytes.Length];
        Array.Copy(_secretBytes, 0, toHash, 0, _secretBytes.Length);
        Array.Copy(_saltBytes, 0, toHash, _secretBytes.Length, _saltBytes.Length);

// objective-c

    NSUInteger len = [passwordData length];
    Byte *secretBytes = (Byte*)malloc(len);
    memcpy(secretBytes, [passwordData bytes], len);

    // create new byte array for new salt
    NSInteger saltInt = [salt integerValue];
    Byte *saltBytes = (Byte*)malloc(4);
    saltBytes[0] = (Byte)(saltInt >> 24);
    saltBytes[1] = (Byte)(saltInt >> 16);
    saltBytes[2] = (Byte)(saltInt >> 8);
    saltBytes[3] = (Byte)saltInt;

    // now create a hash byte array
    NSUInteger hashLength = sizeof(secretBytes) + sizeof(saltBytes);
    Byte *hashedBytes = (Byte*)malloc(hashLength);

    // now copy the bytes over
    memcpy(hashedBytes, &secretBytes, sizeof(secretBytes));
    memcpy(hashedBytes, &saltBytes, sizeof(saltBytes));

0 个答案:

没有答案