From 7d99cd7318f09c5b64f55ed1f04814d56a0b141b Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 31 Jul 2010 15:41:56 +0200 Subject: [PATCH] added class to generate prn added script to test this class some cosmetics on the other test scripts --- backend/lib/Util/Random.php | 132 ++++++++++++++++++++++++++++++++++ backend/lib/Util/UUID.php | 53 +------------- share/tests/ajax.php | 7 +- share/tests/configuration.php | 7 +- share/tests/pdobench.php | 3 + share/tests/random.php | 67 +++++++++++++++++ 6 files changed, 213 insertions(+), 56 deletions(-) create mode 100644 backend/lib/Util/Random.php create mode 100644 share/tests/random.php diff --git a/backend/lib/Util/Random.php b/backend/lib/Util/Random.php new file mode 100644 index 0000000..916e277 --- /dev/null +++ b/backend/lib/Util/Random.php @@ -0,0 +1,132 @@ +. + */ + +namespace Volkszaehler\Util; + +/** + * Extensible PRNG + * + * @author Steffen Vogel + */ +class Random { + protected static $func = 'twister'; + protected static $source = NULL; + + /** + * Initialize the PRNG + * + * Look for a system-provided source of randomness, which is usually crytographically secure. + * /dev/urandom is tried first simply out of bias for Linux systems. + */ + public static function init() { + if (is_readable('/dev/urandom')) { + self::$source = fopen('/dev/urandom', 'rb'); + self::$func = 'fRead'; + } + else if (class_exists('COM', 0)) { + try { + self::$source = new COM('CAPICOM.Utilities.1'); // See http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx + self::$func = 'COM'; + } + catch(\Exception $e) {} + } + return self::$func; + } + + /** + * + * @param intger $bytes + * @todo check if initialized + */ + public static function getBytes($count) { + return call_user_func(array('self', self::$func), $count); + } + + /** + * + * @param array $chars charset for random string + * @param integer $count length of string + */ + public function getString(array $chars, $length) { + $numbers = self::get(0, count($chars) - 1, $length); + $string = ''; + + foreach ($numbers as $number) { + $string .= $chars[$number]; + } + + return $string; + } + + /** + * Generate $count random numbers between $min and $max + * + * @param integer $min + * @param integer $max + * @param integer $count + * @return integer|array single integer if $count == 1 or array of integers if $count > 1 + */ + public function get($min, $max, $count = 1) { + $bytes = self::getBytes($count); + + $numbers = array(); + + for ($i = 0; $i < $count; $i++) { + $numbers[] = ord($bytes[$i]) % ($max - $min + 1) + $min; + } + + return ($count == 1) ? $numbers[0] : $numbers; + } + + /* + * Get the specified number of random bytes, using mt_rand(). + * Randomness is returned as a string of bytes. + */ + protected static function twister($count) { + $rand = ''; + for ($a = 0; $a < $count; $a++) { + $rand .= chr(mt_rand(0, 255)); + } + return $rand; + } + + /** + * Get the specified number of random bytes using a file handle + * previously opened with Random::init(). + * Randomness is returned as a string of bytes. + */ + protected static function fRead($count) { + return fread(self::$source, $count); + } + + /* + * Get the specified number of random bytes using Windows' + * randomness source via a COM object previously created by Random::init(). + * Randomness is returned as a string of bytes. + */ + protected static function COM($count) { + return base64_decode(self::$source->GetRandom($count, 0)); // straight binary mysteriously doesn't work, hence the base64 + } +} + +?> \ No newline at end of file diff --git a/backend/lib/Util/UUID.php b/backend/lib/Util/UUID.php index ad74fa6..1f59e67 100644 --- a/backend/lib/Util/UUID.php +++ b/backend/lib/Util/UUID.php @@ -70,8 +70,6 @@ class UUID { const nsURL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; const nsOID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; const nsX500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; - protected static $randomFunc = 'randomTwister'; - protected static $randomSource = NULL; //instance properties protected $bytes; @@ -210,7 +208,7 @@ class UUID { // Reorder bytes to their proper locations in the UUID $uuid = $time[4].$time[5].$time[6].$time[7].$time[2].$time[3].$time[0].$time[1]; // Generate a random clock sequence - $uuid .= self::randomBytes(2); + $uuid .= Random::getBytes(2); // set variant $uuid[8] = chr(ord($uuid[8]) & self::clearVar | self::varRFC); // set version @@ -221,7 +219,7 @@ class UUID { if (!$node) { // If no node was provided or if the node was invalid, // generate a random MAC address and set the multicast bit - $node = self::randomBytes(6); + $node = Random::getBytes(6); $node[0] = pack("C", ord($node[0]) | 1); } $uuid .= $node; @@ -232,7 +230,7 @@ class UUID { /* Generate a Version 4 UUID. These are derived soly from random numbers. */ // generate random fields - $uuid = self::randomBytes(16); + $uuid = Random::getBytes(16); // set variant $uuid[8] = chr(ord($uuid[8]) & self::clearVar | self::varRFC); // set version @@ -281,51 +279,6 @@ class UUID { else return pack("H*", $str); } - - public static function initRandom() { - /* Look for a system-provided source of randomness, which is usually crytographically secure. - /dev/urandom is tried first simply out of bias for Linux systems. */ - if (is_readable('/dev/urandom')) { - self::$randomSource = fopen('/dev/urandom', 'rb'); - self::$randomFunc = 'randomFRead'; - } - else if (class_exists('COM', 0)) { - try { - self::$randomSource = new COM('CAPICOM.Utilities.1'); // See http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx - self::$randomFunc = 'randomCOM'; - } - catch(\Exception $e) {} - } - return self::$randomFunc; - } - - public static function randomBytes($bytes) { - return call_user_func(array('self', self::$randomFunc), $bytes); - } - - protected static function randomTwister($bytes) { - /* Get the specified number of random bytes, using mt_rand(). - Randomness is returned as a string of bytes. */ - $rand = ""; - for ($a = 0; $a < $bytes; $a++) { - $rand .= chr(mt_rand(0, 255)); - } - return $rand; - } - - protected static function randomFRead($bytes) { - /* Get the specified number of random bytes using a file handle - previously opened with UUID::initRandom(). - Randomness is returned as a string of bytes. */ - return fread(self::$randomSource, $bytes); - } - - protected static function randomCOM($bytes) { - /* Get the specified number of random bytes using Windows' - randomness source via a COM object previously created by UUID::initRandom(). - Randomness is returned as a string of bytes. */ - return base64_decode(self::$randomSource->GetRandom($bytes,0)); // straight binary mysteriously doesn't work, hence the base64 - } } ?> diff --git a/share/tests/ajax.php b/share/tests/ajax.php index 181f599..c077f12 100644 --- a/share/tests/ajax.php +++ b/share/tests/ajax.php @@ -1,10 +1,11 @@ */ /* * This file is part of volkzaehler.org diff --git a/share/tests/configuration.php b/share/tests/configuration.php index f2340ea..a70300f 100644 --- a/share/tests/configuration.php +++ b/share/tests/configuration.php @@ -1,10 +1,11 @@ */ /* * This file is part of volkzaehler.org diff --git a/share/tests/pdobench.php b/share/tests/pdobench.php index c7ade44..cf9c4be 100644 --- a/share/tests/pdobench.php +++ b/share/tests/pdobench.php @@ -1,8 +1,11 @@ */ /* * This file is part of volkzaehler.org diff --git a/share/tests/random.php b/share/tests/random.php new file mode 100644 index 0000000..8bcfc5b --- /dev/null +++ b/share/tests/random.php @@ -0,0 +1,67 @@ + + */ +/* + * This file is part of volkzaehler.org + * + * volkzaehler.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * volkzaehler.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with volkszaehler.org. If not, see . + */ + +include '../../backend/lib/Util/Random.php'; +include '../../backend/lib/Util/UUID.php'; +include '../../backend/lib/Model/Token.php'; + +use Volkszaehler\Model; +use Volkszaehler\Util; + +Util\Random::init(); + +$chars = array( + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'v', 'u', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'V', 'U', 'W', 'X', 'Y', 'Z' + ); + +?> +

PRNG tests

+
+
+
+
+

Token tests

+
+
+
+

UUId tests

+
+
+