2

I am currently in the mist of developing a website using PHP and MYSQL. It is a private website therefore registrations must be allowed using emails. In simple terms if a new user has to be registered, the administrator has to go into the system and add an email address to be registered.

What I want to do is to create a token or a pass value when this does happen.

Here are the steps:

  1. Administrator adds an email to the system
  2. A unique token value is created (e.g. 1234567890)
  3. The token value is then sent to the users email
  4. the user goes on the link provided and enters his email and the token value
  5. If Success - User is allowed to register
  6. If Fail! - Token is regenerated and send again to that email address

What I really want to know is what would be the best practice to create a token and how can we ensure to create a unique token every time an email is registered.

For further security can I ensure that each token only live for a couple of hours. But would this prevent unauthorized access into the system, or this is a bad idea for securing my website?

My thoughts of creating a unique token: Use hashing algorithms that use SALT so the results cannot be predicted or decrypted (Problems with MD5)

Any help or a lead towards the right direction would be greatfull.

luiges90
  • 4,493
  • 2
  • 28
  • 43
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85

1 Answers1

4

I like this method of generating a cryptographically secure pseudo-random number generator or (CSPRNG) for PHP. It was written by Scott:

<?php
   function crypto_rand_secure($min, $max) {
        $range = $max - $min;
        if ($range < 0) return $min; // not so random...
        $log = log($range, 2);
        $bytes = (int) ($log / 8) + 1; // length in bytes
        $bits = (int) $log + 1; // length in bits
        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
        do {
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
            $rnd = $rnd & $filter; // discard irrelevant bits
        } while ($rnd >= $range);
        return $min + $rnd;
}

function getToken($length=32){
    $token = "";
    $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
    $codeAlphabet.= "0123456789";
    for($i=0;$i<$length;$i++){
        $token .= $codeAlphabet[crypto_rand_secure(0,strlen($codeAlphabet))];
    }
    return $token;
}
?>

In terms of adding a timeout, I recommend taking care of this in the database. Add a column that is called like registration_timeout and then use mysql's addtime() function to set this colmn to the current time stamp + however long you want the timeout to be.

Also keep in mind that temporary email accounts are trivial to use (http://www.mailinator.com , http://www.guerrillamail.com, ect...), so asking for someone to register an email account doesn't mean anything. Further more a user account could end up on http://www.bugmenot.com .

Community
  • 1
  • 1
rook
  • 66,304
  • 38
  • 162
  • 239
  • Thanks dude.. Give me an insight. As far as the emails go. they are being controlled using allowed IP Addresses any way and on the plus the website is not for public use. – Angel.King.47 Jan 28 '10 at 23:38
  • 1
    Your algorithm does not meet the requirements of a CSPRNG. Wikipedia's page on CSRRNG provides a decent explanation of the requirements. –  May 11 '11 at 13:36
  • 1
    This answer should be edited. The function proposed by @Rook is no way a CSPRNG. One can argue that this solution doesn't solve the OP's problem because it does not meet the requirements of a secure PRNG if security of the PRNG is defined as passing the next-bit test. Something as simple as `bin2hex(openssl_random_pseudo_bytes(20))` should solve the problem stated by @Angel.King.74 in the original post. There is a very small chance of collision though. That should be taken care of by the application or by prefixing a non-random unique ID to this. – Susam Pal Mar 28 '12 at 15:25
  • @Susam Pal i agree this is a few years old. – rook Mar 28 '12 at 15:34
  • The openssl function is perfect. An actual solution that meets the CSPRNG requirement. – Stephen Smith Sep 18 '13 at 14:40