note 33948 added to function.sha1
| From: | W2k at rack1 dot php dot net | Date: | Fri, 11 Jul 2003 18:00:52 +0000 |
| Subject: | note 33948 added to function.sha1 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51928@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.sha1.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33948
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33948&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33948&report=yes