1

I want to encrypt hash some strings with MD5 in my Mac application.

I Googled about it, but it keeps throwing me examples on how to do it with iPhone apps, like MD5 algorithm in Objective C or Using MD5 hash on a string in cocoa?...

Community
  • 1
  • 1
Saturn
  • 17,888
  • 49
  • 145
  • 271

2 Answers2

3

MD5 is not encryption!. Please see http://en.wikipedia.org/wiki/Cryptographic_hash_function

All of your examples do in fact work on OS X. CommonCrypto is part of libSystem. For a more complete example, I suggest this CocoaWithLove tutorial (and sample code!)

http://cocoawithlove.com/2009/07/hashvalue-object-for-holding-md5-and.html

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • CommonCrypto is not part of Foundation; it is part of libSystem. That difference aside, both libraries are available on both OSs. – Peter Hosey Aug 21 '11 at 23:10
2

MD5 is not encryption is just a unique string (that you usually store in a hash table) calculated from a stream (that could your text, image, sound, data, etc)

Here is sample I used:

#import <CommonCrypto/CommonDigest.h>

const char *cStr = [someNSString UTF8String];
unsigned char resultChar[16];
CC_MD5( cStr, strlen(cStr), resultChar);
NSString *md5 = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                 resultChar[0], resultChar[1], resultChar[2], resultChar[3], 
                 resultChar[4], resultChar[5], resultChar[6], resultChar[7],
                 resultChar[8], resultChar[9], resultChar[10], resultChar[11],
                 resultChar[12], resultChar[13], resultChar[14], resultChar[15]];

Now just use the md5 var for your purposes :)

nacho4d
  • 43,720
  • 45
  • 157
  • 240