Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Calculate MD5 on iPhone

Is there any library for iPhone SDK that calculates hashes like MD5?

Mac, Windows Vista

Posted on May 3, 2008 4:07 PM

Reply
13 replies

May 3, 2008 8:57 PM in response to EagerEyes

EagerEyes wrote:
There's a function CC_MD5, which is used in the WiTap example.


Unfortunately WiTap's code contains condition:

#if TARGET OSIPHONE
uniqueID = [[UIDevice currentDevice] uniqueIdentifier];
.....................
#else
.....................
if((buffer = [uniqueID UTF8String])) {
bzero(md5, CC MD5_DIGESTLENGTH);
CC_MD5(buffer, strlen(buffer), md5);
............................................
#endif

Basically that means CC_MD5 call can't be used with iPhone OS

May 4, 2008 7:16 AM in response to henson2k

One of my iPhone apps uses the <CommonCrypto/CommonDigest.h> framework for calculating hashes. There is plenty of documentation in the XCode doc sets on the subject. Do a full text search in All Doc Sets for 'CommonDigest'. You will find the CC_MD5 man pages. (For new applications it is preferred to use SHA-256.)

In the iPhone Simulator it all works fine. I can not test it on the iPhone itself

btw. You can put the hash bytes in a NSData object using the dataWithBytes message.

May 31, 2008 12:14 PM in response to Sbrocket52

i'm wondering if i'm including them from the wrong place, but they are all right here:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/includ e/CommonCrypto/

i include it and use it like this:


#import <CommonCrypto/CommonDigest.h>
....
unsigned char digest[16];
char finaldigest[32];
int i;

CC_MD5([self bytes],[self length],digest);

May 31, 2008 12:28 PM in response to Sbrocket52

I just tried this and it works fine on the device. The header is here:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/includ e/CommonCrypto/CommonDigest.h

I guess that is because it's not part of a framework but of the C library? No idea. In any case, it pays to try things out ...

EDIT: looks like you found it while I was compiling ...

Jun 11, 2008 5:39 PM in response to henson2k

<pre class="jive-pre">
// Import library
#import <CommonCrypto/CommonDigest.h>
...
// Function definition
NSString * md5( NSString *str )
{
const char *cStr = [str UTF8String];
unsigned char result[CC MD5_DIGESTLENGTH];
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]
];
}
...
// Implementation
NSString *digest = md5(@"test");
NSLog(@"MD5 TEST %@", digest);
</pre>

The answer you should get is: 098F6BCD4621D373CADE4E832627B4F6
You can check your results on this page http://www.adamek.biz/md5-generator.php

Calculate MD5 on iPhone

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.