Re: How to use NSString with CommonCrypto HMAC correctly?
Re: How to use NSString with CommonCrypto HMAC correctly?
- Subject: Re: How to use NSString with CommonCrypto HMAC correctly?
- From: Chris Ridd <email@hidden>
- Date: Sat, 13 Dec 2008 17:10:37 +0000
On 13 Dec 2008, at 16:01, Alex Reynolds wrote:
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]);
That's not going to work. The length of an NSString is not the same as
the number of bytes you need to hold a NUL-terminated UTF-8 version of
it. You'll always end up mallocing too few bytes :-(
Something like this would be more simpler/more accurate:
char *keychar = strdup([key UTF8String]);
or if you don't have or like strdup(), this:
char *t = [key UTFString];
char *keychar = (char *)malloc(strlen(t) + 1);
strcpy(keychar, t);
Dunno about the rest of your code, but that jumped out at me.
Cheers,
Chris
_______________________________________________
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