26 #require_once 'Zend/Crypt/Math/BigInteger.php'; 47 public function rand($minimum, $maximum)
49 if (file_exists(
'/dev/urandom')) {
50 $frandom =
fopen(
'/dev/urandom',
'r');
51 if ($frandom !==
false) {
52 return fread($frandom, strlen($maximum) - 1);
55 if (strlen($maximum) < 4) {
56 return mt_rand($minimum, $maximum - 1);
59 $i2 = strlen($maximum) - 1;
60 for (
$i = 1;
$i < $i2;
$i++) {
61 $rand .= mt_rand(0, 9);
63 $rand .= mt_rand(0, 9);
74 public static function randBytes($length, $strong =
false)
76 $length = (int) $length;
81 return random_bytes($length);
84 $bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
85 if ($bytes !==
false && strlen($bytes) === $length) {
89 if (file_exists(
'/dev/urandom') &&
is_readable(
'/dev/urandom')) {
90 $frandom =
fopen(
'/dev/urandom',
'r');
91 if ($frandom !==
false) {
92 return fread($frandom, $length);
95 if (
true === $strong) {
96 require_once
'Zend/Crypt/Exception.php';
98 'This PHP environment doesn\'t support secure random number generation. ' .
99 'Please consider installing the OpenSSL and/or Mcrypt extensions' 103 for (
$i = 0;
$i < $length;
$i++) {
104 $rand .= chr(mt_rand(0, 255));
120 require_once
'Zend/Crypt/Exception.php';
122 'The min parameter must be lower than max parameter' 125 $range = $max - $min;
128 }
elseif ($range > PHP_INT_MAX || is_float($range)) {
129 require_once
'Zend/Crypt/Exception.php';
131 'The supplied range is too great to generate' 135 return random_int($min, $max);
144 $bits = (int) max($bits, 1);
145 $bytes = (int) max(ceil($bits / 8), 1);
146 $filter = (int) ((1 << $bits) - 1);
148 $rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
150 }
while ($rnd > $range);
151 return ($min + $rnd);
163 if (ord($long[0]) > 127) {
164 return "\x00" . $long;
177 return $this->_math->binaryToInteger($binary);
188 return $this->_math->integerToBinary($integer);
elseif(isset( $params[ 'redirect_parent']))
static randInteger($min, $max, $strong=false)
static randBytes($length, $strong=false)