I am trying to calculate the MD5 hash in an iOS application in order to compare the hashes between the file stored in the application and the same file stored on the web server using PHP.
This is the code for the iOS app:
unsigned char result[CC_MD5_DIGEST_LENGTH];
NSData* data = [NSData dataWithContentsOfFile:@"advert.png"];
const void* src = [data bytes];
CC_MD5(src, [data length], result);
NSString *imageHash = [[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]]
lowercaseString];
NSLog(@"%@", imageHash);
Code for web server:
$file = 'advert.png';
echo 'MD5 file hash of ' . $file . ': ' . md5_file($file);
Application creates: D41D8CD98F00B204E9800998ECF8427E
PHP generates: 3ef9386b1dd50e8e166efbe48f0f9401
md5sum generates: 3ef9386b1dd50e8e166efbe48f0f9401
UPDATE
Just launched the application through the simulator and correctly calculated the hash: 3ef9386b1dd50e8e166efbe48f0f9401.
When iOS 5.1 works on my iPhone 4, it calculates as: ddf017003e063e353a5e4ec2cc4a5095