1

I'm using the following function for SHA1 hash from NSString in my application:

-(NSString *)stringToSha1:(NSString *)str{
    const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:str.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    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;

}

This works like a charm on my iphone devices with ios 5.0 and up, but on my iPad running version 4.3.5 it crashes on the line

CC_SHA1(data.bytes, data.length, digest);

It says the following:

dyld: lazy symbol binding failed: can't resolve symbol _CC_SHA1 in because dependent dylib #3 could not be loaded

dyld: can't resolve symbol _CC_SHA1 in because dependent dylib #3 could not be loaded

Any ideas on how to make it work on my iPad 4.3.5 device?

Madoc
  • 1,605
  • 3
  • 17
  • 30

2 Answers2

2

If you look at the header file you'll see:

extern unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md) __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_5_0);

The __OSX_AVAILABLE_STARTING(...) stuff means that this function is only available on OS X 10.4+ and iOS 5.0+. This is why your application is crashing.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
  • Yea i've seen that. On the other hand, several places they say it should work for 4.3 nontheless, so it is very strange :\ See here for one of several examples: http://stackoverflow.com/questions/7971827/are-cc-md5-and-cc-sha1-available-in-ios-4 – Madoc Jul 10 '12 at 07:56
1

Found the solution myself, though i have no idea why it worked.

Removed the Framework "libcommonCrypto.dylib" from my project, clean, build and it worked.

Tested on IOS 4.1 and 4.3, works like a charm.

Madoc
  • 1,605
  • 3
  • 17
  • 30
  • 1
    If this works as a solution then go ahead and mark it as the answer, even if it is your own. – TheZ Jul 16 '12 at 23:45