Page 1 of 1

Unique MD5

Posted: Fri Jun 20, 2008 9:32 pm
by GeXus
I'm using this code to create a unique hash

Code: Select all

 
md5(uniqid(rand(), true));
 
But I'm having instances where there are some of the same... Is that possible?

Re: Unique MD5

Posted: Fri Jun 20, 2008 9:54 pm
by Kieran Huggins
this is a common fallacy when trying to create a random number.

chaining hashes and/or random functions actually dramatically decreases the randomness. You should only ever use one approach.

What are you trying to accomplish, exactly?

Re: Unique MD5

Posted: Sat Jun 21, 2008 12:21 am
by Ambush Commander
While it's possible, it is highly unlikely that you generated collisions. uniqid(rand(), true) should be fairly unique, as you've chained two "random" functions together. The md5 afterwards, however, decreases randomness.

Re: Unique MD5

Posted: Sat Jun 21, 2008 1:08 am
by Zoxive
Just do a simple check to see if that md5 exists (Where ever you are putting it), it it does have it run the function again.`

Re: Unique MD5

Posted: Sat Jun 21, 2008 3:04 am
by Kieran Huggins
better yet, take an md5 of the ( username or a rand() salt) and the current timestamp. You'll never collide.