2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
\r
5 * Random Number Generator
\r
7 * PHP versions 4 and 5
\r
9 * Here's a short example of how to use this library:
\r
12 * include('Crypt/Random.php');
\r
14 * echo crypt_random();
\r
18 * LICENSE: This library is free software; you can redistribute it and/or
\r
19 * modify it under the terms of the GNU Lesser General Public
\r
20 * License as published by the Free Software Foundation; either
\r
21 * version 2.1 of the License, or (at your option) any later version.
\r
23 * This library is distributed in the hope that it will be useful,
\r
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
26 * Lesser General Public License for more details.
\r
28 * You should have received a copy of the GNU Lesser General Public
\r
29 * License along with this library; if not, write to the Free Software
\r
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
\r
34 * @package Crypt_Random
\r
35 * @author Jim Wigginton <terrafrost@php.net>
\r
36 * @copyright MMVII Jim Wigginton
\r
37 * @license http://www.gnu.org/licenses/lgpl.txt
\r
38 * @version $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $
\r
39 * @link http://phpseclib.sourceforge.net
\r
43 * Generate a random value.
\r
45 * On 32-bit machines, the largest distance that can exist between $min and $max is 2**31.
\r
46 * If $min and $max are farther apart than that then the last ($max - range) numbers.
\r
48 * Depending on how this is being used, it may be worth while to write a replacement. For example,
\r
49 * a PHP-based web app that stores its data in an SQL database can collect more entropy than this function
\r
52 * @param optional Integer $min
\r
53 * @param optional Integer $max
\r
57 function crypt_random($min = 0, $max = 0x7FFFFFFF)
\r
63 // see http://en.wikipedia.org/wiki//dev/random
\r
64 // if open_basedir is enabled file_exists() will ouput an "open_basedir restriction in effect" warning,
\r
65 // so we suppress it.
\r
66 if (@file_exists('/dev/urandom')) {
\r
69 $fp = fopen('/dev/urandom', 'rb');
\r
71 extract(unpack('Nrandom', fread($fp, 4)));
\r
73 // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this:
\r
74 // -4 % 3 + 0 = -1, even though -1 < $min
\r
75 return abs($random) % ($max - $min) + $min;
\r
78 /* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called.
\r
79 Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here:
\r
81 http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/
\r
83 The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:
\r
85 http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */
\r
86 if (version_compare(PHP_VERSION, '5.2.5', '<=')) {
\r
88 if (!isset($seeded)) {
\r
90 mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));
\r
96 // The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5
\r
97 // in the browser and reloading the page.
\r
99 if (!isset($crypto)) {
\r
101 for ($i = 0; $i < 8; $i++) {
\r
102 $key.= pack('n', mt_rand(0, 0xFFFF));
\r
103 $iv .= pack('n', mt_rand(0, 0xFFFF));
\r
106 case class_exists('Crypt_AES'):
\r
107 $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
\r
109 case class_exists('Crypt_TripleDES'):
\r
110 $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
\r
112 case class_exists('Crypt_DES'):
\r
113 $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
\r
115 case class_exists('Crypt_RC4'):
\r
116 $crypto = new Crypt_RC4();
\r
119 extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));
\r
120 return abs($random) % ($max - $min) + $min;
\r
122 $crypto->setKey($key);
\r
123 $crypto->setIV($iv);
\r
124 $crypto->enableContinuousBuffer();
\r
127 extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));
\r
128 return abs($random) % ($max - $min) + $min;
\r