diff --git a/backend/lib/Util/Random.php b/backend/lib/Util/Random.php index 57dc3f2..9e46cc2 100644 --- a/backend/lib/Util/Random.php +++ b/backend/lib/Util/Random.php @@ -30,7 +30,7 @@ namespace Volkszaehler\Util; * @package util */ class Random { - protected static $func = 'twister'; + protected static $func = NULL; protected static $source = NULL; /** @@ -44,22 +44,29 @@ class Random { self::$source = fopen('/dev/urandom', 'rb'); self::$func = 'fRead'; } - else if (class_exists('COM', 0)) { + elseif (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) {} } + else { + self::$func = 'twister'; + } + return self::$func; } /** * * @param intger $bytes - * @todo check if initialized */ public static function getBytes($count) { + if (!isset(self::$func)) { + self::init(); + } + return call_user_func(array('self', self::$func), $count); } @@ -69,7 +76,7 @@ class Random { * @param integer $count length of string */ public function getString(array $chars, $length) { - $numbers = self::get(0, count($chars) - 1, $length); + $numbers = self::getNumbers(0, count($chars) - 1, $length); $string = ''; foreach ($numbers as $number) { @@ -87,7 +94,7 @@ class Random { * @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) { + public static function getNumbers($min, $max, $count = 1) { $bytes = self::getBytes($count); $numbers = array(); diff --git a/share/tests/random.php b/share/tests/random.php index 8bcfc5b..0354e6a 100644 --- a/share/tests/random.php +++ b/share/tests/random.php @@ -31,8 +31,6 @@ 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', @@ -41,10 +39,13 @@ $chars = array( ?>

PRNG tests

+ +

PNRG generator:

+