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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.