]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/extlib/Crypt/Random.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / OStatus / extlib / Crypt / Random.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5  * Random Number Generator
6  *
7  * PHP versions 4 and 5
8  *
9  * Here's a short example of how to use this library:
10  * <code>
11  * <?php
12  *    include('Crypt/Random.php');
13  *
14  *    echo crypt_random();
15  * ?>
16  * </code>
17  *
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.
22  *
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.
27  *
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,
31  * MA  02111-1307  USA
32  *
33  * @category   Crypt
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
40  */
41
42 /**
43  * Generate a random value.
44  *
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.
47  *
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
50  * can.
51  *
52  * @param optional Integer $min
53  * @param optional Integer $max
54  * @return Integer
55  * @access public
56  */
57 function crypt_random($min = 0, $max = 0x7FFFFFFF)
58 {
59     if ($min == $max) {
60         return $min;
61     }
62
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)));
67         fclose($fp);
68
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;
72     }
73
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:
76
77        http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/
78
79        The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:
80
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', '<=')) { 
83         static $seeded;
84         if (!isset($seeded)) {
85             $seeded = true;
86             mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));
87         }
88     }
89
90     static $crypto;
91
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.
94
95     if (!isset($crypto)) {
96         $key = $iv = '';
97         for ($i = 0; $i < 8; $i++) {
98             $key.= pack('n', mt_rand(0, 0xFFFF));
99             $iv .= pack('n', mt_rand(0, 0xFFFF));
100         }
101         switch (true) {
102             case class_exists('Crypt_AES'):
103                 $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
104                 break;
105             case class_exists('Crypt_TripleDES'):
106                 $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
107                 break;
108             case class_exists('Crypt_DES'):
109                 $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
110                 break;
111             case class_exists('Crypt_RC4'):
112                 $crypto = new Crypt_RC4();
113                 break;
114             default:
115                 extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));
116                 return abs($random) % ($max - $min) + $min;
117         }
118         $crypto->setKey($key);
119         $crypto->setIV($iv);
120     }
121
122     extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));
123     return abs($random) % ($max - $min) + $min;
124 }
125 ?>