0

我正在做一些身份验证,并提供了示例 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);

// 目标-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));
4

0 回答 0