嗨,有人在使用SHA512 iOS实现吗?该代码似乎无法生成与php相同的代码。
<?php
$code = hash("SHA512", '123' . '123' );
echo $code;
?>
输出:
263fec58861449aacc1c328a4aff64aff4c62df4a2d50b3f207fa89b6e242c9aa778e7a8baeffef85b6ca6d2e7dc16ff0a760d59c13c238f6bcdc32f8ce9cc62
- (NSString *) sha512:(NSString *) input withSalt: (NSString *) salt {
const char *cKey = [salt cStringUsingEncoding:NSUTF8StringEncoding];
const char *data = [input cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char digest[CC_SHA512_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA512, cKey, strlen(cKey), data, strlen(data), digest);
NSString *hash;
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
hash = output;
return hash;
}
[self sha512:@"123" withSalt:@"123"];
输出:
3cafe40f92be6ac77d2792b4b267c2da11e3f3087b93bb19c6c5133786984b44
最佳答案
您的代码有两个问题:
hash_hmac('SHA512', '123', '123' );
0634fd04380bbaf5069c8c46a74c7d21df7414888d980c27a16d5e262cb8c9059139c212d0926000faf026e483904cefae2f5e9d9bd5f51fbc2ac4c4de518115
它的长度为128个字符[512位],表面上看起来是SHA512函数的预期长度。
关于php - SHA512带盐的iOS专用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18859005/