给定消息和盐,如何对返回的哈希字符串进行编码?
我需要重现php函数:
hash_hmac('sha256','message','salt');
谢谢
最佳答案
找到了答案:
#import <CommonCrypto/CommonHMAC.h>
-(NSString *) hashString :(NSString *) data withSalt: (NSString *) salt {
const char *cKey = [salt cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSString *hash;
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", cHMAC[i]];
hash = output;
return hash;
}
关于objective-c - 如何在 cocoa /iOS中进行SHA哈希,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9454620/