在不使用密钥的情况下,为目标C中等价于C#的字符串生成sha256哈希

本文关键字:字符串 等价于 哈希 sha256 目标 密钥 情况下 | 更新日期: 2023-09-27 18:29:42

在c#中,我正在进行

 HashAlgorithm hash=SHA256.create();
    string myHash = Convert.ToBase64String( hasher.ComputeHash(Encoding.UTF8.GetBytes("hello")));

在目标c中,我正在进行

const unsigned char arr[32];
        CC_SHA256([@"hello" UTF8String], 32, &arr);
        NSMutableData *HM = [NSData dataWithBytes:(const void *)arr length:32];
        NSLog(@"macHah  %@",[HM base64EncodingWithLineLength:0]);

但是它们都生成不同的散列

在不使用密钥的情况下,为目标C中等价于C#的字符串生成sha256哈希

尝试使用以下方法:

- (NSData *)sha256:(NSData *)data
{
    unsigned char hash[CC_SHA256_DIGEST_LENGTH];
    if ( CC_SHA256([data bytes], [data length], hash) )
    {
        NSData *hashData = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
        return hashData;
    }
    return nil;
}