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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to