I’m current having some trouble with this code Snippet:
- (NSString *) md5:( NSString *) str
{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
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]
];
}
This method is generating a hash of a downloaded XML File, which is complete different from the PHP function md5().
So how do I get the Hash which is identically to PHPs one or vice versa.
If You do
echo md5('hello')in PHP You would receive a 32 char long string, but when You use Your code snippet in ObjC, You transform the string chars to the HEX format (by using the formater %02x) – see here https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265 and here: Unsigned Char Array to Hex Representation NSString.And You use only half the MD5 string chars to create this HEX formated string… Either return the
resultimmediately or do the same HEX transformation in PHP 🙂