Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing MD5 hash on UIImage on iPhone and file on server

I am attempting to compute a MD5 hash within an iOS app, in an effort to compare hashes between the file saved within the app and the same file stored on a 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);

The code for the web server:

$file = 'advert.png';
echo 'MD5 file hash of ' . $file . ': ' . md5_file($file);

The app generates: D41D8CD98F00B204E9800998ECF8427E

The PHP generates: 3ef9386b1dd50e8e166efbe48f0f9401

md5sum generates: 3ef9386b1dd50e8e166efbe48f0f9401

UPDATE:

Just ran the app through the simulator and it correctly computes the hash: 3ef9386b1dd50e8e166efbe48f0f9401.

When ran on my iPhone 4 running iOS 5.1 it calculates as: ddf017003e063e353a5e4ec2cc4a5095

like image 262
Dan Smith Avatar asked Jan 19 '26 21:01

Dan Smith


1 Answers

D41D8CD98F00B204E9800998ECF8427E is the MD5 sum of an empty file. Your don't read the file correctly, the reason is probably that dataWithContentsOfFile: needs an absolute path. Try:

NSString *path = [[NSBundle mainBundle] pathForResource:@"advert" ofType:@"png"];
NSData *plistData = [NSData dataWithContentsOfFile:path];
like image 163
unbeli Avatar answered Jan 21 '26 11:01

unbeli