NSString -initWithData:encoding returning nil
NSString -initWithData:encoding returning nil
- Subject: NSString -initWithData:encoding returning nil
- From: Alex Reynolds <email@hidden>
- Date: Fri, 4 Sep 2009 14:28:29 -0700
I have the following method, which digests a string with the SHA-1
algorithm:
- (NSString *) HMACSHA1DigestWithSecretKey:(NSString *)secretKey {
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
char *keyCharPtr = strdup([secretKey UTF8String]);
char *dataCharPtr = strdup([self UTF8String]);
printf("**** HMACSHA1DigestWithSecretKey: \n\tkeyCharPtr --
\n%s\n\tdataCharPtr -- \n%s\n", keyCharPtr, dataCharPtr);
CCHmacContext hctx;
CCHmacInit(&hctx, kCCHmacAlgSHA1, keyCharPtr, strlen(keyCharPtr));
CCHmacUpdate(&hctx, dataCharPtr, strlen(dataCharPtr));
CCHmacFinal(&hctx, digest);
NSData *encryptedStringData = [NSData dataWithBytes:digest
length:CC_SHA1_DIGEST_LENGTH];
NSLog(@"**** HMACSHA1DigestWithSecretKey: \n\tencryptedStringData
-- \n%@\n",
[encryptedStringData description]);
NSString *encryptedString = [[[NSString alloc]
initWithData:encryptedStringData encoding:NSUTF8StringEncoding]
autorelease];
NSLog(@"**** HMACSHA1DigestWithSecretKey: \n\tencryptedString -- \n%@\n",
encryptedString);
free(keyCharPtr);
free(dataCharPtr);
return encryptedString;
}
However, the step:
NSString *encryptedString =
[[[NSString alloc] initWithData:encryptedStringData
encoding:NSUTF8StringEncoding] autorelease];
returns nil.
Here's a sample transcript. I'm trying to digest a test-case key and data
from the RFC 2202 docs (cf. http://www.faqs.org/rfcs/rfc2202.html):
**** HMACSHA1DigestWithSecretKey:
keyCharPtr --
Jefe
dataCharPtr --
what do ya want for nothing?
**** HMACSHA1DigestWithSecretKey:
encryptedStringData --
<effcdf6a e5eb2fa2 d27416d5 f184df9c 259a7c79>
**** HMACSHA1DigestWithSecretKey:
encryptedString --
(null)
Why would the call to -initWithData:encoding (the "encryptedString"
variable) fail?
Thanks for your advice.
Regards,
Alex
_______________________________________________
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