18

So as many of you may already know, CommonCrypto isn't included in the iPhone SDK anymore (as on 3.0).

Where else can I easily get hold of an MD5 function? Am I going to have to compile OpenSSL into my project just to get MD5??

A few clarifications:

  • This isn't for security
  • This is going to be used with an API that requires an MD5 digest of a string. I have no control over this API.

If I could use SHA256, I would. I'm all for moving forward, but I can't change the API.


Edit

It seems I was mistaken. CommonCrypto is still in the iPhone SDK, just not explicity as it used to be. I was looking for the libCrypto library to include in my project. I was unaware that you could include the CommonCrypto headers without linking against the library...

Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • 1
    And while you're updating your code, replace MD5 with something more secure like SHA1. – Georg Schölly Dec 04 '09 at 14:31
  • 1
    It's not for security, it's simply for a digest. – Jasarien Dec 04 '09 at 14:46
  • you can still use CommonCrypto in an app, I did. I haven't submitted to the app store yet but I haven't heard anything about apps getting rejected for using it. Are you sure the distribution thing didn't have to do with some kind of licensing? I know there are all these weird international laws and stuff around encryption. – bpapa Dec 04 '09 at 15:23
  • 2
    Yes, great question. To simplify how I resolved it: All that needs to be dne is remove 'libcommonCrypto.dylib' from your targets build phases but still import it where you need it. – capikaw Mar 20 '13 at 15:29

1 Answers1

22

Are you sure its not included? I have apps compiled on the device for 3.0 using the following code that works:

#import <CommonCrypto/CommonDigest.h>

#define CC_MD5_DIGEST_LENGTH 16   /* digest length in bytes */

- (NSString *)md5:(NSString *)str { 
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH]; 
    CC_MD5(cStr, strlen(cStr), result); 
    return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",         
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]];    
}
coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • I'm using your function to calculate the hash of a file, but it crashes with large strings (like those created from files). And Xcode gives no error at all. Any ideas on adjustments I could make to the function? And any suggestions for re-creating the exact same hash with PHP on the server? – Andrew Nov 20 '10 at 20:24
  • im not sure that the crash you are seeing is related to this simple function – coneybeare Nov 20 '10 at 23:28
  • Well it works when I send it a short string like `@"foo"` or when I send it a short `NSString` but when I send it an `NSString` created from a file it crashes. – Andrew Nov 21 '10 at 01:04
  • 2
    I bet your string created is not properly retained. – coneybeare Nov 21 '10 at 01:42