Take the 2-minute tour ×

This is theoretical question but I am curious about it. What if I do this (code in PHP, but the language isn't really matter in this case):

$value  = ''; //starting value
$repeat = false;


     $value = md5($value);

     /*Save values in database, one row per value*/

     /*Check for repeated hash value in db, and set $repeat flag true if there is one*/


As you can see I suspect that there will be repeated hash values. I think there is no way that every existing text has its own value as it should mean that every hash value has its own and that doesn't make sense.

My questions are: Is there any article about this "problem" out there? It can happen I got the same value in one system for example when I hash files for check if they are valid? Can this caused problems anywhere in any system?

share|improve this question
md5 is utterly broken because of both collisions and speed. At least if you use it for password hashing or you are trying to "secure" something else that is. –  PeeHaa Aug 22 '12 at 7:24
It's a bit unclear what you're trying to do. Why are you hashing in such a chain? In one part you use use this chain, in the other you talk about file hashing. Those are completely different scenarios. –  CodesInChaos Aug 22 '12 at 7:27
@CodesInChaos the code is just for illustration, the point is that there are chance for collision. The file hashing is just an example as well. I accepted an answer that really answered what I wanted, plus the wikipedia article above is a helped too. –  TheNAkos Aug 22 '12 at 7:50

2 Answers 2

up vote 3 down vote accepted

If you care about multiple texts hashing to the same value, don't use MD5. MD5 has fast collision attacks, which violated the property you want. Use SHA-2 instead.

When using a secure hash function, collisions for 128 hashes are extremely difficult to find, and by that I mean that I know of no case where it happened. But if you want to avoid that chance, simply use 256 bit hashes. Then finding a collision using brute-force is beyond the computational power of all humanity for now. In particular there is no known message pair for which SHA-256(m1) == SHA-256(m2) with m1 != m2.

You're right that hashed can't be unique(See Pidgeonhole principle), but the chances of you actually finding such a case are extremely low. So don't bother with handling that case.

I typically aim for a 128 bit security level, so when I need a collision free hash function, I use a 256 bit hash function, such as SHA-256.

With your hash chain you won't find a collision, unless you're willing to wait for a long time. Collisions become likely once you have around 2^(n/2) times, which is 2^64 in the case of 128 bit hashes such as md5. I know of no brute-force collisions against a 128 bit hash. The only collisions I know are carefully crafted messages that exploit weaknesses in the hashing scheme you use (those exist against md5).

share|improve this answer
Thank you. I accept this because it pointed me to the pidgeon hole principle. And really informative answer. Of course I know I can't find a collision by this little code, I just wrote it for illustration. (to be honest I thought a lot about this years ago when I started to learn programming, but now it come up again, and wanted to clear this stuff from my mind... and I learned some new thing too :) ) –  TheNAkos Aug 22 '12 at 7:34
Should be "use scrypt/pkdf2 instead". –  Billy ONeal Aug 23 '12 at 3:54
@BillyONeal Why? I saw no indication that the OP needs key-strengthening. –  CodesInChaos Aug 23 '12 at 6:32
@CodesInChaos: Any time someone cares about how "fast" a hash is, they are typically dealing with password storage. (And given the comments in the OP's code, that seems to be what (s)he is doing) No message digest algorithm should be used for password storage. –  Billy ONeal Aug 23 '12 at 17:11

Hash it multiple times by same method or different method, Then it would be nearly impossible to repeat its self, Also check if they repeat then repeat the hash function until the values are different, Then save in database or use it where ever you like...

share|improve this answer
Completely false. If the compression function in the hash is vulnerable (which is what makes a hash vulnerable in MD5's case and similar hashes like SHA-1 and SHA-2), then running the same hash multiple times provides no additional protection. Besides, if the first hash is the same, hashing it again will produce a second hash which is, indeed, the same. –  Billy ONeal Aug 23 '12 at 3:53
No its not, For Example: Hash this "name", You will get "b068931cc450442b63f5b3d276ea4297", Now hash that result "b068931cc450442b63f5b3d276ea4297", You will get "eb8342ec6933b9dc8e9fe7e86d476f21" <-- So, They are definitely not same as you can see, This works for MD5 and SHA-1 and all others. –  devWaleed Aug 23 '12 at 6:27
If I can find a collision with the first hash "b068931cc450442b63f5b3d276ea4297", which may or may not be "name", then hashing it again does not change anything. The first hash already produced the same output. The second pass of MD5 will produce "eb8342ec6933b9dc8e9fe7e86d476f21" for both inputs that caused the first collision. –  Billy ONeal Aug 23 '12 at 17:10
It's unclear what you mean by using multiple methods. Do you concatenate their output, or do you chain them? –  CodesInChaos Aug 23 '12 at 17:14
If value repeats,Simply hash the answer of hash, If not then let it go with 1 hash.Thats what I am saying –  devWaleed Aug 23 '12 at 17:23

Your Answer


