Unique MD5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Unique MD5

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Unique MD5

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Unique MD5

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Unique MD5

Post 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.`
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Unique MD5

Post by Kieran Huggins »

better yet, take an md5 of the ( username or a rand() salt) and the current timestamp. You'll never collide.
Post Reply