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