Welcome Guest, Not a member yet? Register   Sign In
general md5 hashing question ... not CI specific
#1

[eluser]skattabrain[/eluser]
ok, so when I take $value and encrypt it like this ... $new_value = md5($value) ... I know that md5($value) will always equal $new_value.

question ... can anything other that md5($value) ever equal the value I now have call $new_value?

is there a 1 in a billion chance that md5($some_other_rare_value) will ever be the same as $new_value?

i'm creating a tracking system and i need a very quick, non-resource intensive way to come up with a unique value that will never conflict with it ... even if i have a trillion records in the db.

might seem like a goofy question ... but i'm curious.

basically ... i need a hash that is not so much secure (no concern in this application) but unique. i create my hash by taking ...

$hash = md5(microtime().$_SERVER['REMOTE_ADDR']);
#2

[eluser]davidbehler[/eluser]
In theory it's possible that there is more than 1 value that create the same hash when using md5. But the chances are pretty slim so there shouldn't be much of a problem if you use md5 for creating your unique value.
Before inserting an entry you could check if there already is an entry with that id and if so just create a new hash by adding a random number to your original value for example.
#3

[eluser]Rick Jolly[/eluser]
waldmeister is right, there could be collisions. Instead of md5, you could use uniqid(). It uses microtime and optionally a prefix (which could be a random number) and entrophy (which is just more random characters). As soon as you md5 a "unique" string, you make it less unique.
#4

[eluser]skattabrain[/eluser]
thanks waldmeister ... i want to minimize db noise as much as possible ... it's a tracking solution and the goal is for this db to handle millions if not billions of records ... so less calls = more better.

but even then, i've tested mysql with over 100,000,000 records in it and pulling an indexed hash was still lightning fast. i guess at that point server load comes into question.
#5

[eluser]skattabrain[/eluser]
[quote author="Rick Jolly" date="1231631900"]waldmeister is right, there could be collisions. Instead of md5, you could use uniqid(). It uses microtime and optionally a prefix (which could be a random number) and entrophy (which is just more random characters). As soon as you md5 a "unique" string, you make it less unique.[/quote]

posted then saw your post ... good to know ... can't believe i've never used that function before!!




Theme © iAndrew 2016 - Forum software by © MyBB