MD5 Hash on iPhone

Hi,

I'm trying to create a PHP compatible MD5 hash of a NSString object - and have found several posts on these forums explaining how to use the CommonDigest header to achieve it.

What I have is:

NSString * md5( NSString *str )
{
const char *cStr = [str NSASCIIStringEncoding];
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]
];
}


But this doesn't seem to produce the same result as this Java code

// This is used by md5 hash
private String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
/**
* Generates a PHP compatible md5 hash
*/
private String md5(String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return hex(md.digest(message.getBytes("CP1252")));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
}
return null;
}
Can anybody shed any light on how I can get CommonDigest to produce the same MD5 as the one produced by the Java code?
Many thanks
John

iPhone 3G, Mac OS X (10.4.10)

Posted on May 28, 2009 1:18 AM

Reply
1 reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

MD5 Hash on iPhone

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