• 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
NSString -initWithData:encoding returning nil
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: NSString -initWithData:encoding returning nil
      • From: Jens Alfke <email@hidden>
    • Re: NSString -initWithData:encoding returning nil
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Re: setNeedsDisplayInRect not triggering drawRect?
  • Next by Date: Re: NSString -initWithData:encoding returning nil
  • Previous by thread: Re: Application main menu title behavior change in 10.6
  • Next by thread: Re: NSString -initWithData:encoding returning nil
  • Index(es):
    • Date
    • Thread