note 88400 added to function.uniqid

From: Date: Thu, 22 Jan 2009 13:39:55 +0000
Subject: note 88400 added to function.uniqid
Groups: php.notes 
Request: Send a blank email to php-notes+get-149651@lists.php.net to get a copy of this message
I've created a class from the function below. With it, creating a lot of uuids beomes about 100 times faster because you do not need to fopen() for each uuid. uuid::get() still works as a static function if you just need a single uuid. <?php class uuid { protected $urand; public function __construct() { $this->urand = @fopen ( '/dev/urandom', 'rb' ); } /** * @brief Generates a Universally Unique IDentifier, version 4. * * This function generates a truly random UUID. The built in CakePHP String::uuid() function * is not cryptographically secure. You should uses this function instead. * * @see http://tools.ietf.org/html/rfc4122#section-4.4 * @see http://en.wikipedia.org/wiki/UUID * @return string A UUID, made up of 32 hex digits and 4 hyphens. */ function get() { $pr_bits = false; if (is_a ( $this, 'uuid' )) { if (is_resource ( $this->urand )) { $pr_bits .= @fread ( $this->urand, 16 ); } } if (! $pr_bits) { $fp = @fopen ( '/dev/urandom', 'rb' ); if ($fp !== false) { $pr_bits .= @fread ( $fp, 16 ); @fclose ( $fp ); } else { // If /dev/urandom isn't available (eg: in non-unix systems), use mt_rand(). $pr_bits = ""; for($cnt = 0; $cnt < 16; $cnt ++) { $pr_bits .= chr ( mt_rand ( 0, 255 ) ); } } } $time_low = bin2hex ( substr ( $pr_bits, 0, 4 ) ); $time_mid = bin2hex ( substr ( $pr_bits, 4, 2 ) ); $time_hi_and_version = bin2hex ( substr ( $pr_bits, 6, 2 ) ); $clock_seq_hi_and_reserved = bin2hex ( substr ( $pr_bits, 8, 2 ) ); $node = bin2hex ( substr ( $pr_bits, 10, 6 ) ); /** * Set the four most significant bits (bits 12 through 15) of the * time_hi_and_version field to the 4-bit version number from * Section 4.1.3. * @see http://tools.ietf.org/html/rfc4122#section-4.1.3 */ $time_hi_and_version = hexdec ( $time_hi_and_version ); $time_hi_and_version = $time_hi_and_version >> 4; $time_hi_and_version = $time_hi_and_version | 0x4000; /** * Set the two most significant bits (bits 6 and 7) of the * clock_seq_hi_and_reserved to zero and one, respectively. */ $clock_seq_hi_and_reserved = hexdec ( $clock_seq_hi_and_reserved ); $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2; $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000; return sprintf ( '%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node ); } } ---- Server IP: 89.248.170.61 Probable Submitter: 85.147.192.41 ---- Manual Page -- http://www.php.net/manual/en/function.uniqid.php Edit -- https://master.php.net/note/edit/88400 Del: integrated -- https://master.php.net/note/delete/88400/integrated Del: useless -- https://master.php.net/note/delete/88400/useless Del: bad code -- https://master.php.net/note/delete/88400/bad+code Del: spam -- https://master.php.net/note/delete/88400/spam Del: non-english -- https://master.php.net/note/delete/88400/non-english Del: in docs -- https://master.php.net/note/delete/88400/in+docs Del: other reasons-- https://master.php.net/note/delete/88400 Reject -- https://master.php.net/note/reject/88400 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149651) next »