Jump to content

md5_file gives same hash of 2 different image jpg files


figo2476

Recommended Posts

Theoretically, not every MD5 string is unique. If you look at the MD5 hashes, you only have numbers 0-9 and letters a-f. This means there are only 16 available characters, meaning there are only 32^16 (<- I think that's how you work it out) individual hashes (they're not infinite, as all md5 strings work out as 32 characters). You might have just been extremely lucky (well, if you call it luck) to find 2 that match.

 

but yeah, much more likely is they're the same. :P

There are infinite files for one hash (theoretically)! Otherwise it could just give 128 Bit (or whatever -depending on the algorithm) of different files. But it should be really unlikely that you have two  ;D

 

I would also say it is a simple copy - or there is an error in your PHP-script ;)

 

kind regards


function my_md5($filename) {
	$temp_f = '/tmp/my_md5-'. rand(100, 999) .'.jpg';
	$src_img = imagecreatefromjpeg($filename);
	imagejpeg($src_img, $temp_f);
	imagedestroy($src_img);
	$md5 = @md5_file($temp_f);
	@unlink($temp_f);
	return $md5;
}

 

I assume md5 will actually look into the content of a file and hash it. By doing this, write the file to a tmp folder, will increase the chance to have a random hash

Is the code returning the same hash for files that you know are different or are you just asking in general? Are you sure you are calling the code with two different file names? The posted code worked as expected when I tried it.

 

Your code has two @'s in it, which means you are probably getting errors that you are suppressing. That would indicate the code is not working and the md5 being returned is perhaps that of an empty or non-existent file.

 

What errors or problem are you having that you are trying to solve?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.