Search code examples
objective-cios6commoncrypto

Working SHA1 code in iOS5 not working in iOS6


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];