0

I wanted to pass a MD5 hash generated by Ruby program to a PHP program, and found something strange.

Ruby code(result: ad0efdf609e99ec50d9333dc0bd1c11a)

Digest::MD5.hexdigest 'test str1&test str2&test str3&test str4'

PHP code(result: 804160119894a4cc8c376fffbcc21e1c)

PHP online MD5 generator

You can see the results are different... but if I remove the "&" in my string:

Ruby code(result: 45fa91e4c89aa6f3bb501531a5de6bf4)

Digest::MD5.hexdigest 'test str1test str2test str3test str4'

PHP code(result: 45fa91e4c89aa6f3bb501531a5de6bf4)

PHP online MD5 generator

They are the same. Why did this happen? The MD5 algorithm should be same in any language, shouldn't it?

Bigxiang
  • 6,252
  • 3
  • 22
  • 20

2 Answers2

4

The results of that website are wrong.

Here comes an example (using PHP on the command line):

php -r 'echo md5("test str1&test str2&test str3&test str4");'

Output:

ad0efdf609e99ec50d9333dc0bd1c11a
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
2

804160119894a4cc8c376fffbcc21e1c is the MD5 hash for test str1, not test str1&test str2&test str3&test str4.

That on-line generator is obviously corrupting POST data when reading it. According to Firebug, data is already sent corrupted to the server, so the issue is on its client-side form handling. It's easy to find what's wrong:

function sendHttpRequest(w){
    var url = "http://www.md5.cz/getmd5.php";
    var idWhat = document.getElementById('what');
    var params = "what=" + idWhat.value;
                           ^^^^ Pardon???

The correct hash is ad0efdf609e99ec50d9333dc0bd1c11a.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thank you very much, your answer and above are both excellent, I want to accept you two but I can't. So I only can vote you up :) – Bigxiang Apr 14 '14 at 15:03