How to use NSString with CommonCrypto HMAC correctly?
How to use NSString with CommonCrypto HMAC correctly?
- Subject: How to use NSString with CommonCrypto HMAC correctly?
- From: Alex Reynolds <email@hidden>
- Date: Sat, 13 Dec 2008 08:01:34 -0800
I am trying to use the HMAC SHA-1 components of Apple's CommonCrypto
library with NSStrings, and I am running into some difficulty.
I have the following method:
- (NSString *) hmacSha1:(NSString *)key data:(NSString *)data
{
unsigned char digest[CC_SHA1_DIGEST_LENGTH] = {0};
char *keychar = (char *)malloc([key length]);
char *datachar = (char *)malloc([data length]);
strcpy(keychar, [key UTF8String]);
strcpy(datachar, [data UTF8String]);
CCHmacContext hctx;
CCHmacInit(&hctx, kCCHmacAlgSHA1, keychar, strlen(keychar));
CCHmacUpdate(&hctx, datachar, strlen(datachar));
CCHmacFinal(&hctx, digest);
NSData *encStrD = [NSData dataWithBytes:digest
length:CC_SHA1_DIGEST_LENGTH];
NSString *encStr = [NSString base64StringFromData:encStrD length:
[encStrD length]];
free(keychar);
free(datachar);
return [[[NSString alloc] initWithString:[NSString
base64StringFromData:encStrD length:[encStrD length]]] autorelease];
}
My key is derived from text entered into a UITextField. I call its -
text method, which returns a NSString instance.
Problem:
When I pass in the NSString "key" from this UITextField, the value of
"digest" is wrong. (I have a JavaScript utility that I can use to
calculate correct digests, for verification purposes.)
Workaround:
If I manually set "key" inside the method, then "digest" is computed
correctly:
- (NSString *) hmacSha1:(NSString *)key data:(NSString *)data {
key = @"here_is_my_secret_key_blah_blah_blah";
...
}
Steps to troubleshoot:
I have two lines in this method (not shown) that log the bytes and
length of the key:
NSLog(@"hmacSha1secretKeyB: %@", [[NSData dataWithBytes:keychar
length:strlen(keychar)] description]);
NSLog(@"hmacSha1secretKeyL: %d", strlen(keychar));
I have other similar log statements that show the bytes and length of
the digest.
The strange thing is that the key bytes that are logged are identical,
whether the key's NSString value comes from the UITextField -text
method call, or whether I use key = @"..."
Nonetheless, when the key comes from the UITextField, the digest is
wrong. When the key is manually set, the digest is correct.
Why would manually assigning a value to the key variable resolve this
issue?
I apologize if this is a dumb question. I have been tearing my hair
out to get this working. Thanks for any advice.
Regards,
Alex
NB. Please ignore the NSString helper method that does base 64
encoding, which I have tested separately. The values that I need to
test are actually the digest, not the base 64 encoding, which comes
after.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden