note 33948 deleted from function.sha1 by nlopess
| From: | nlopess@php.net | Date: | Sun, 22 Feb 2004 09:59:28 +0000 |
| Subject: | note 33948 deleted from function.sha1 by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65525@lists.php.net to get a copy of this message | ||
Note Submitter: W2k
----
Since SHA1 generates a whole lot of random-looking characters, I figured it can be used to generate
"unique" ID's. This is the function I use in my home-rolled session management code:
function generateUniqueId()
{
return sprintf( "%s%02d", substr( sha1( (string) time() ), mt_rand( 0, 32 ), 8 ),
mt_rand( 0, 99 ) );
}
As you can see, this takes a random substr of 8 characters from the SHA1 hash of the current time,
and adds two random digits. This allows for a theoretical maximum of 3200 unique ID's per
second (assuming the random number generator hits all possible combinations).
This is only one way of generating a (hopefully) unique ID and probably not the best, but I'm
hoping it may be of use to someone.