Jump to content

Hash Collision with Random Key


GB_001

Recommended Posts

Hello, I created a system where emails get encrypted with a random key that gets stored in a database, what are the odds of the Hashes Colliding?

 

Part of the code:

 

function genRandomString($num) {    $length = $num;    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';    $string = "";        for ($p = 0; $p < $length; $p++) {        $string .= $characters[mt_rand(0, strlen($characters))];    }    return $string;}$Key=genRandomString(10);$email_s=hash_hmac('ripemd160', $email, $Key);

 

 

Thankyou, GB.

Link to comment
https://forums.phpfreaks.com/topic/213636-hash-collision-with-random-key/
Share on other sites

It's impossible to calculate the odds because the length of the random string is dynamic (passed in as an argument to the function). The longer the string, the longer the odds.

 

If you really want to test this then create a script which constantly calls your function and stores the results in a MySQL table, leave it running for some time to get a few thousand records....or more. Then do a SELECT DISTINCT query on the table and if the distinct row count matches the actual row count then you know they are all unique.

 

Another option here is to use uniq() and then hash() it using your preferred algo.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.