如何在cocoa/ iOS的SHA散列
鉴于消息和盐如何编码它返回散列的string?
我需要重现的PHPfunction:
hash_hmac('sha256','message','salt');
谢谢
find答案:
#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; }