Using MD5 hash on a string in cocoa?

Well, first off, MD5 isn't encryption. So if you're looking for encryption, you're looking in the wrong place But if you just want to hash something using MD5 on an iPhone, this should give you the information you need: Calculate MD5 on iPhone.

Well, first off, MD5 isn't encryption. So if you're looking for encryption, you're looking in the wrong place. But if you just want to hash something using MD5 on an iPhone, this should give you the information you need: Calculate MD5 on iPhone.

16 Correct, but -1. I use stackoverflow to avoid other forum sites. Please consider posting an actual answer rather than a link.

– bentford Jan 20 '10 at 0:37.

Noticed this in the Facebook Connect source code. Looks pretty solid, give it a shot. #import ... + (NSString*)md5HexDigest:(NSString*)input { const char* str = input UTF8String; unsigned char resultCC_MD5_DIGEST_LENGTH; CC_MD5(str, strlen(str), result); NSMutableString *ret = NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2; for(int I = 0; i.

3 +1 this is basically equivalent to my snippet above, but looks a bit cleaner. – bentford Dec 30 '10 at 6:21.

This is what I use. Credits go to Alistair McMillan. #import + (NSString *) md5:(NSString *)str { const char *cStr = str UTF8String; unsigned char result16; 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", result0, result1, result2, result3, result4, result5, result6, result7, result8, result9, result10, result11, result12, result13, result14, result15 ; } NOTE #1: I didn't have to link to any libraries NOTE #2: I couldn't find -lcrypto in the external framework list on the iphone, and this works without -lcrypto.

I added the following to my "NSString+MyGoonk" category: #include - (NSString *)md5 { NSData *data = self dataUsingEncoding: NSUTF8StringEncoding; unsigned char *digest = MD5(data bytes, data length, NULL); return NSString stringWithUTF8String: (char *)digest; } Two things: this assumes your string is UTF8. I'm sure there's a way to make it more generic, but I almost never use anything else. You have to link -lcrypto into your project.

After spending too much time trying to figure this out I made a comprehensive post with correct code and how to use it. You can find the post here on my blog. saobart.com/md5-has-in-objective-c.

Thanks. Very helpful. – znq May 29 '10 at 16:31 Thanks man.

I think this is the correct answer. – itsaboutcode Aug 2 '10 at 19:18 It's worth noting that this solution is identical to the code I posted above. – bentford Dec 30 '10 at 6:19 @bentford It's not identical; yours is more efficient.

Code in the above blog uses %02X then converts to lowercase after... – JosephH Oct 20 at 12:13.

MD5 is not encryption, it is a cryptographic hash function. It's a one-way function whose output is a 128-bit number. The fact that it is cryptographic means that it is a computationally hard problem that, given an MD5 hash output, compute a string whose MD5 is that value.So, MD5 can be used for data integrity checks, but not for encryption.

1 It should be noted that MD5 has been compromised so that it's no longer infeasible to attack it. That might not matter depending on what you're using it for, but it's worth keeping in mind. En.wikipedia.Org/wiki/MD5#Vulnerability – James Williams Mar 16 '09 at 22:52 6 It should be noted that the original poster didn't ask for encryption… – Martín Marconcini Oct 6 '09 at 10:33 1 They did at first.

– Joe Oct 1 '10 at 9:08.

FYI: CC_MD5() is 10.5+ although the manual says 10.4. If you need 10.4 compatibility use CC_MD5_Init(), CC_MD5_Update() and CC_MD5_Final() instead.

It's worth mentioning that the OpenSSL methods are deprecated on more recent versions of OS X, and the MD5 digest is conventionally lower case. Personally I'm more a fan of the unrolled style for efficiency, and I think using ObjC categories for this is a better fit. For MD5Digest.

H: #include @interface NSString (MD5Digest) - (NSString*) md5Digest; @end @interface NSData (MD5Digest) - (NSString*) md5Digest; @end And MD5Digest. M: #include #include "MD5Digest. H" static NSString* md5Digest(const void *data, CC_LONG length) { unsigned char digestCC_MD5_DIGEST_LENGTH; unsigned char* d = CC_MD5(data, length, digest); return NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, nil; } @implementation NSString (MD5Digest) - (NSString*) md5Digest { return md5Digest(self UTF8String, self lengthOfBytesUsingEncoding:NSUTF8StringEncoding); } @end @implementation NSData (MD5Digest) - (NSString*) md5Digest { return md5Digest(self bytes, self length); } @end.

The good thing about MD5 is that it isn't encryption. When you start using encryption on your iPhone, you need to comply to a lot of guidelines etc too. Also, when you hash a string, you can't get that string back from the hash.

1 The question no longer mentions encryption. – bentford Dec 30 '10 at 6:21.

I need to hash a string using the MD5 technique in cocoa. Any frameworks that are used must be able to be accessed on the iphone. Please provide code if possible.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions