Working SHA1 code in iOS5 not working in iOS6
-
03-07-2021 - |
Question
The following worked just fine with iOS5 as the base class but fails (SIGABRT) with iOS6. Could it be an OS thing or an architecture thing?
Important to also note is the accompanying MD5 hash does work.
-(NSString *)SHA1Hash {
const char *cStr = [self UTF8String];
unsigned char digest[16];
CC_SHA1( cStr, strlen(cStr), digest ); // This is the sha1 call
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
Thanks for any and all help!
Solution
You were probably getting "lucky" on iOS 5. SHA-1 digests are 20 bytes, not 16:
unsigned char digest[16];
OTHER TIPS
Use the macro CC_SHA1_DIGEST_LENGTH
to declare your digest length. 16 is too short so you are trashing the stack.
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
From man page for CC_SHA1
CC_SHA1() computes the SHA-1 message digest of the len bytes at data and places it in md (which must have space for CC_SHA1_DIGEST_LENGTH == 20 bytes of output). It returns the md pointer.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow