0

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!

sbonami
  • 1,892
  • 17
  • 33

2 Answers2

2

You were probably getting "lucky" on iOS 5. SHA-1 digests are 20 bytes, not 16:

unsigned char digest[16];
Mike
  • 36
  • 1
  • My luck has ran out then, don't know how that managed to work for so long. You answered correctly first so I'll award you the accepted, thanks! – sbonami Sep 25 '12 at 14:16
2

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.

James
  • 1,341
  • 7
  • 13