figo2476 Posted May 31, 2008 Share Posted May 31, 2008 Hi: md5_file gives same hash of 2 different image jpg files. I assume: 1. One image is a copy of another, but it has different file names. Or 2. It will give the same hash for 2 different files, somehow. Does anyone has any idea why this happen? Link to comment https://forums.phpfreaks.com/topic/108116-md5_file-gives-same-hash-of-2-different-image-jpg-files/ Share on other sites More sharing options...
LemonInflux Posted May 31, 2008 Share Posted May 31, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108116-md5_file-gives-same-hash-of-2-different-image-jpg-files/#findComment-554150 Share on other sites More sharing options...
kbh43dz_u Posted May 31, 2008 Share Posted May 31, 2008 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 I would also say it is a simple copy - or there is an error in your PHP-script kind regards Link to comment https://forums.phpfreaks.com/topic/108116-md5_file-gives-same-hash-of-2-different-image-jpg-files/#findComment-554153 Share on other sites More sharing options...
figo2476 Posted May 31, 2008 Author Share Posted May 31, 2008 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 Link to comment https://forums.phpfreaks.com/topic/108116-md5_file-gives-same-hash-of-2-different-image-jpg-files/#findComment-554179 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2008 Share Posted May 31, 2008 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? Link to comment https://forums.phpfreaks.com/topic/108116-md5_file-gives-same-hash-of-2-different-image-jpg-files/#findComment-554207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.