djfm
djfm

Reputation: 2468

disagreeing md5 hashes of same string, with example

This is getting me crazy, my md5's don't agree. I have this string:

The Combinations Generator is a tool that allows you to easily create a series of combinations by selecting the related attributes. For example, if you're selling t-shirts in three different sizes and two different colors, the generator will create six combinations for you.

When I hash it on my computer using the md5 function (with php 5.5.0) it produces the following hash: 422f3f656e1a5f95e8b5cf7565d815b5

http://www.miraclesalad.com/webtools/md5.php agrees with my computer's result.

http://www.md5.cz/ disagrees with both my computer and miraclesalad.

This string/md5 pair was initially computed by another computer which also gives the same result as md5.cz.

I read about encoding issues (although the string doesn't contain any non ASCII characters), so I tried the following code on my computer:

<?php

$str = "The Combinations Generator is a tool that allows you to easily create a series of combinations by selecting the related attributes. For example, if you're selling t-shirts in three different sizes and two different colors, the generator will create six combinations for you.";

echo "$str<BR/>";
echo md5($str)."<BR/>";
echo md5(utf8_encode($str))."<BR/>";
echo md5(utf8_decode($str))."<BR/>";


die();

The output is:

The Combinations Generator is a tool that allows you to easily create a series of combinations by selecting the related attributes. For example, if you're selling t-shirts in three different sizes and two different colors, the generator will create six combinations for you.

422f3f656e1a5f95e8b5cf7565d815b5

422f3f656e1a5f95e8b5cf7565d815b5

422f3f656e1a5f95e8b5cf7565d815b5

So it is not about utf8.

Any idea what's happening?

Upvotes: 2

Views: 2392

Answers (4)

spartan
spartan

Reputation: 51

The strings need to be exactly the same, including the whitespaces. Probably the sites are using some transformation like trim() or stripslashes(). md5 will return the same value only if the strings are exact.

Upvotes: 0

LS97
LS97

Reputation: 336

I tried feeding the string above incrementally to both sites you linked to in your question, and it turns out that the character breaking the generator at md5.cz is the apostrophe in if you're selling t-shirts.

If you strip the string of special characters before feeding it to a hasher, possibly preserving the string's uniqueness using something like urlencode(), you should get matching hashes for any string.

Upvotes: 0

Tjofras
Tjofras

Reputation: 2096

My best guess is that it has something to do with the ' mark in the word "you're" and character encodings. If you remove that quote both sites report the same md5.

Upvotes: 5

Marc B
Marc B

Reputation: 360862

md5 is md5. That's all there is to it. If you get different hashes from different (non-buggy) implementations, then you're feeding in diffent inputs. Remember that md5 is DESIGNED to produce wildly different outputs if the input(s) are even slightly different. A single whitespace character (tab, linebreak, etc...) at the end of one of your test strings will totally trash your expected hash, because you've fed in a different input.

Upvotes: -1

Related Questions