added class to generate prn
added script to test this class some cosmetics on the other test scripts
This commit is contained in:
parent
00abfde228
commit
7d99cd7318
6 changed files with 213 additions and 56 deletions
132
backend/lib/Util/Random.php
Normal file
132
backend/lib/Util/Random.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @package util
|
||||
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*/
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Volkszaehler\Util;
|
||||
|
||||
/**
|
||||
* Extensible PRNG
|
||||
*
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
<?php
|
||||
/**
|
||||
* simple test for jquery, ajax and json padding
|
||||
* Simple test for jquery, ajax and json padding
|
||||
*
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @package tests
|
||||
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
*/
|
||||
/*
|
||||
* This file is part of volkzaehler.org
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
<?php
|
||||
/**
|
||||
* a simple test for Volkszaehler\Util\Configuration
|
||||
* A test for our Configuration class
|
||||
*
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @package tests
|
||||
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
*/
|
||||
/*
|
||||
* This file is part of volkzaehler.org
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
/**
|
||||
* Some testing for PDOs performence
|
||||
*
|
||||
* @package tests
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
*/
|
||||
/*
|
||||
* This file is part of volkzaehler.org
|
||||
|
|
67
share/tests/random.php
Normal file
67
share/tests/random.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
/**
|
||||
* Some tests for our Random, Token & UUID classes
|
||||
*
|
||||
* @package tests
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
*/
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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'
|
||||
);
|
||||
|
||||
?>
|
||||
<h4>PRNG tests</h4>
|
||||
<pre>
|
||||
|
||||
<?php
|
||||
echo 'Numbers: ' . implode(',', Util\Random::get(0, 100, 100)) . PHP_EOL;
|
||||
echo 'String: ' . Util\Random::getString($chars, 100) . PHP_EOL;
|
||||
echo 'Bytes: ' . Util\Random::getBytes(100) . PHP_EOL;
|
||||
?>
|
||||
</pre>
|
||||
<h4>Token tests</h4>
|
||||
<pre>
|
||||
<?php
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
echo new Model\Token() . PHP_EOL;
|
||||
}
|
||||
?>
|
||||
</pre>
|
||||
<h4>UUId tests</h4>
|
||||
<pre>
|
||||
<?php
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
echo Util\UUID::mint(4) . PHP_EOL;
|
||||
}
|
||||
?>
|
||||
</pre>
|
Loading…
Add table
Reference in a new issue