• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to use NSString with CommonCrypto HMAC correctly?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: How to use NSString with CommonCrypto HMAC correctly?
      • From: Chris Ridd <email@hidden>
  • Prev by Date: Re: How to handle document scaling in an NSRulerView Client?
  • Next by Date: __NSGetTransformedIdealAdvances crash
  • Previous by thread: Re: Now contents of the layer not being rotated [was Re: Rotating a CALayer more than once is not working ]
  • Next by thread: Re: How to use NSString with CommonCrypto HMAC correctly?
  • Index(es):
    • Date
    • Thread