簡體   English   中英

在不使用鍵的情況下為等效於C#的目標C中的字符串生成sha256哈希

[英]generate sha256 hash for a string in objective C equivalent to C# without using key

在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]);

但它們都產生不同的哈希

嘗試使用以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM