Need to generate HMAC SHA256 hash in Objective C as in Java
You need to fix your Java hmac printer, because 4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
isn't valid. All those ffffff
in there are a giveaway that you are sign-extending the bytes to 32-bit signed integers before converting them to hex. Presumably the correct hmac is 4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c
.
Anyway, I suspect you are calling your method incorrectly. I copied your code into a test program which gives me this output for your key and data:
2011-12-10 13:03:38.231 hmactest[8251:707] test hmac = <4ed8ce7c c4c71b2f 72dc21a1 e0e62d32 550b0771 296b9c11 59de8675 9928654c>
That matches your desired output (except for the sign-extension errors).
Here's my test program:
#import <Foundation/Foundation.h>#import <CommonCrypto/CommonHMAC.h>NSData *hmacForKeyAndData(NSString *key, NSString *data){ const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];}int main (int argc, const char * argv[]){ @autoreleasepool { // Compare to http://en.wikipedia.org/wiki/HMAC#Examples_of_HMAC_.28MD5.2C_SHA1.2C_SHA256_.29 NSLog(@"empty hmac = %@", hmacForKeyAndData(@"", @"")); NSLog(@"test hmac = %@", hmacForKeyAndData(@"YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==", @"id=456|time=19:10|nonce=8")); } return 0;}
Line
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
has a bug
If in your key would be 0x00 byte strlen(cKey)
would give wrong length and hmac generation process will produce some rubbish.
In my implementation I have changed that to:
CCHmac(kCCHmacAlgSHA256, cKey, [key length], cData, [data length], cHMAC);