From 8c4c0aad468451636f961a9c96867359e46ec5dd Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 10 Aug 2010 18:05:34 -0600 Subject: [PATCH] Better randomid generation (?) Since uniqid() is essentially microtime(), adding microtime() information does not get us anything. So use uniqid/mt_rand exclusively, add a bit more entropy with the current PID, and encode with base64 - packs even more info into the characters. --- framework/Support/lib/Horde/Support/Randomid.php | 16 ++++++---------- framework/Support/test/Horde/Support/RandomidTest.php | 6 ------ 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/framework/Support/lib/Horde/Support/Randomid.php b/framework/Support/lib/Horde/Support/Randomid.php index 703c70f5c..150effcfe 100644 --- a/framework/Support/lib/Horde/Support/Randomid.php +++ b/framework/Support/lib/Horde/Support/Randomid.php @@ -8,12 +8,12 @@ /** * Class for generating a random ID string. This string uses all characters - * in the class [0-9a-z]. + * in the class [0-9a-zA-Z]. * * * * @@ -35,22 +35,18 @@ class Horde_Support_Randomid /** * New random ID. - * - * @param integer $length The length of the ID. */ - public function __construct($length = 16) + public function __construct() { - $this->generate($length); + $this->generate(); } /** * Generate a random ID. - * - * @param integer $length The length of the ID. */ - public function generate($length = 16) + public function generate() { - $this->_rid = substr(base_convert(dechex(strtr(microtime(), array('0.' => '', ' ' => ''))) . strtr(uniqid(mt_rand(), true), array('.' => '')), 16, 36), 0, $length); + $this->_rid = rtrim(base64_encode(pack('H*', strtr(uniqid(mt_rand(), true), array('.' => '')) . dechex(getmypid()))), '='); } /** diff --git a/framework/Support/test/Horde/Support/RandomidTest.php b/framework/Support/test/Horde/Support/RandomidTest.php index e710b83c9..7645ba1a7 100644 --- a/framework/Support/test/Horde/Support/RandomidTest.php +++ b/framework/Support/test/Horde/Support/RandomidTest.php @@ -17,12 +17,6 @@ */ class Horde_Support_RandomidTest extends PHPUnit_Framework_TestCase { - public function testRandomidLength() - { - $rid = (string)new Horde_Support_Randomid; - $this->assertEquals(16, strlen($rid)); - } - public function testRandomidDuplicates() { $values = array(); -- 2.11.0