为目标C中的字符串生成sha256哈希,相当于C#,而不使用键

为目标C中的字符串生成sha256哈希,相当于C#,而不使用键,c#,objective-c,hash,sha256,C#,Objective C,Hash,Sha256,在c#中,我正在做 HashAlgorithm hash=SHA256.create(); string myHash = Convert.ToBase64String( hasher.ComputeHash(Encoding.UTF8.GetBytes("hello"))); const unsigned char arr[32]; CC_SHA256([@"hello" UTF8String], 32, &arr); NSMutableDa

在c#中,我正在做

 HashAlgorithm hash=SHA256.create();
    string myHash = Convert.ToBase64String( hasher.ComputeHash(Encoding.UTF8.GetBytes("hello")));
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中,我正在做

 HashAlgorithm hash=SHA256.create();
    string myHash = Convert.ToBase64String( hasher.ComputeHash(Encoding.UTF8.GetBytes("hello")));
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]);

但这两种方法都会生成不同的散列

请尝试使用以下方法:

- (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;
}

尝试使用以下方法:

- (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;
}