问题
Alright, I have two files. They are the EXACT SAME.
The first file is:
http://iadsonline.com/servconfig.php
And the second file is:
http://xzerox.info/servconfig.php
However, when I use md5_file() to get their MD5, They return two different MD5's.
The first returns cc7819055cde3194bb3b136bad5cf58d, which is incorrect, and the second returns 96a0cec80eb773687ca28840ecc67ca1, which is correct.
The file is simply an
To verify, I've used this code:
$contents = file_get_contents($URL);
echo htmlentities($contents);
And they both return
So why is it hashing them differently?
回答1:
The second one ends in a newline, the first does not.
回答2:
Trying with curl, I see the first one is without a newline after it, the second one is with a newline after it. So of course they'll hash differently. And indeed, even at the command line (bash prompt):
$ md5 sc.dat
MD5 (sc.dat) = cc7819055cde3194bb3b136bad5cf58d
$ md5 zz.dat
MD5 (zz.dat) = 96a0cec80eb773687ca28840ecc67ca1
回答3:
Could you have whitespace in either of those files? Open them up in a text editor and show all characters.
Alternatively, run something like this
echo str_replace(array("\n", "\t", "\r"), '[I AM HIDING!]', file_get_contents($URL));
If you see [I AM HIDING!], you will know what to do :)
回答4:
this also happened with me. I set the same encoding (utf-8 without BOM) to all files that store and retrieve hashed strings :) now md5() gives the same results :)
来源:https://stackoverflow.com/questions/2799815/why-is-the-same-input-returning-two-different-md5-hashes