2007-07-02 06:19:55 +02:00
|
|
|
<?php
|
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Random Number Generator
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
|
|
|
* include('Crypt/Random.php');
|
|
|
|
*
|
|
|
|
* echo crypt_random();
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2011-04-18 14:17:40 +02:00
|
|
|
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* @category Crypt
|
|
|
|
* @package Crypt_Random
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright MMVII Jim Wigginton
|
2010-11-13 20:28:20 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $
|
2007-07-25 23:56:14 +02:00
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-19 23:39:44 +01:00
|
|
|
* Generate a random value.
|
|
|
|
*
|
|
|
|
* On 32-bit machines, the largest distance that can exist between $min and $max is 2**31.
|
|
|
|
* If $min and $max are farther apart than that then the last ($max - range) numbers.
|
|
|
|
*
|
|
|
|
* Depending on how this is being used, it may be worth while to write a replacement. For example,
|
|
|
|
* a PHP-based web app that stores its data in an SQL database can collect more entropy than this function
|
|
|
|
* can.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* @param optional Integer $min
|
|
|
|
* @param optional Integer $max
|
|
|
|
* @return Integer
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-02-19 23:39:44 +01:00
|
|
|
function crypt_random($min = 0, $max = 0x7FFFFFFF)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2010-02-28 06:28:38 +01:00
|
|
|
if ($min == $max) {
|
|
|
|
return $min;
|
|
|
|
}
|
|
|
|
|
2011-11-24 19:32:49 +01:00
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) {
|
|
|
|
// openssl_random_pseudo_bytes() is slow on windows per the following:
|
|
|
|
// http://stackoverflow.com/questions/1940168/openssl-random-pseudo-bytes-is-slow-php
|
2011-11-24 20:03:56 +01:00
|
|
|
if ((PHP_OS & "\xDF\xDF\xDF") !== 'WIN') { // PHP_OS & "\xDF\xDF\xDF" == strtoupper(substr(PHP_OS, 0, 3)), but a lot faster
|
2011-11-24 19:32:49 +01:00
|
|
|
extract(unpack('Nrandom', openssl_random_pseudo_bytes(4)));
|
|
|
|
|
|
|
|
return abs($random) % ($max - $min) + $min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-19 23:39:44 +01:00
|
|
|
// see http://en.wikipedia.org/wiki//dev/random
|
2011-02-06 02:12:20 +01:00
|
|
|
static $urandom = true;
|
|
|
|
if ($urandom === true) {
|
|
|
|
// Warning's will be output unles the error suppression operator is used. Errors such as
|
|
|
|
// "open_basedir restriction in effect", "Permission denied", "No such file or directory", etc.
|
|
|
|
$urandom = @fopen('/dev/urandom', 'rb');
|
|
|
|
}
|
|
|
|
if (!is_bool($urandom)) {
|
2011-02-08 06:16:11 +01:00
|
|
|
extract(unpack('Nrandom', fread($urandom, 4)));
|
2010-02-19 23:39:44 +01:00
|
|
|
|
|
|
|
// say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this:
|
|
|
|
// -4 % 3 + 0 = -1, even though -1 < $min
|
|
|
|
return abs($random) % ($max - $min) + $min;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called.
|
|
|
|
Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here:
|
|
|
|
|
|
|
|
http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/
|
|
|
|
|
|
|
|
The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:
|
|
|
|
|
2011-11-24 19:46:12 +01:00
|
|
|
http://svn.php.net/viewvc/php/php-src/tags/php_5_3_2/ext/standard/php_rand.h?view=markup */
|
2010-02-19 23:39:44 +01:00
|
|
|
if (version_compare(PHP_VERSION, '5.2.5', '<=')) {
|
|
|
|
static $seeded;
|
|
|
|
if (!isset($seeded)) {
|
|
|
|
$seeded = true;
|
|
|
|
mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static $crypto;
|
|
|
|
|
|
|
|
// The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5
|
|
|
|
// in the browser and reloading the page.
|
2007-07-02 06:19:55 +02:00
|
|
|
|
2010-02-19 23:39:44 +01:00
|
|
|
if (!isset($crypto)) {
|
2010-02-28 06:28:38 +01:00
|
|
|
$key = $iv = '';
|
|
|
|
for ($i = 0; $i < 8; $i++) {
|
|
|
|
$key.= pack('n', mt_rand(0, 0xFFFF));
|
|
|
|
$iv .= pack('n', mt_rand(0, 0xFFFF));
|
|
|
|
}
|
2010-02-19 23:39:44 +01:00
|
|
|
switch (true) {
|
|
|
|
case class_exists('Crypt_AES'):
|
|
|
|
$crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
|
|
|
|
break;
|
|
|
|
case class_exists('Crypt_TripleDES'):
|
2010-02-28 06:28:38 +01:00
|
|
|
$crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
|
|
|
|
break;
|
2010-02-19 23:39:44 +01:00
|
|
|
case class_exists('Crypt_DES'):
|
2010-02-28 06:28:38 +01:00
|
|
|
$crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
|
2010-02-19 23:39:44 +01:00
|
|
|
break;
|
|
|
|
case class_exists('Crypt_RC4'):
|
|
|
|
$crypto = new Crypt_RC4();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));
|
|
|
|
return abs($random) % ($max - $min) + $min;
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
2010-02-28 06:28:38 +01:00
|
|
|
$crypto->setKey($key);
|
|
|
|
$crypto->setIV($iv);
|
2010-04-24 08:40:49 +02:00
|
|
|
$crypto->enableContinuousBuffer();
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2010-02-19 23:39:44 +01:00
|
|
|
extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));
|
|
|
|
return abs($random) % ($max - $min) + $min;
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|