Saturn
Saturn

Reputation: 18159

MD5 on Mac application?

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?...

Upvotes: 1

Views: 1456

Answers (2)

nacho4d
nacho4d

Reputation: 45128

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 :)

Upvotes: 2

Yann Ramin
Yann Ramin

Reputation: 33197

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

Upvotes: 3

Related Questions