Comparing MD5 hash with UIImage on iPhone and server file

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

+3
source share
3 answers

D41D8CD98F00B204E9800998ECF8427E - MD5 . , , dataWithContentsOfFile: . :

NSString *path = [[NSBundle mainBundle] pathForResource:@"advert" ofType:@"png"];
NSData *plistData = [NSData dataWithContentsOfFile:path];
+6

, iPhone PNG .

+3

. :

NSString *fileName = @"1.png";    
UIImage *image = [UIImage imageNamed:fileName];
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSData *dataFromFile = [NSData dataWithContentsOfFile:filePath];
NSData *dataFromImage = [NSData dataWithData:UIImagePNGRepresentation(image)];
NSString *md5FileToString = [dataFromFile md5Hash];
NSString *md5ImageToString = [dataFromImage md5Hash];
NSLog(@"%@,%@",md5FileToString,md5ImageToString);
0

All Articles