]> git.mxchange.org Git - friendica.git/blob - library/phpsec/Crypt/Random.php
Merge pull request #3277 from annando/issue-3142
[friendica.git] / library / phpsec / Crypt / Random.php
1 <?php\r
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
3 \r
4 /**\r
5  * Random Number Generator\r
6  *\r
7  * PHP versions 4 and 5\r
8  *\r
9  * Here's a short example of how to use this library:\r
10  * <code>\r
11  * <?php\r
12  *    include('Crypt/Random.php');\r
13  *\r
14  *    echo crypt_random();\r
15  * ?>\r
16  * </code>\r
17  *\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
22  *\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
27  *\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
31  * MA  02111-1307  USA\r
32  *\r
33  * @category   Crypt\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
40  */\r
41 \r
42 /**\r
43  * Generate a random value.\r
44  *\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
47  *\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
50  * can.\r
51  *\r
52  * @param optional Integer $min\r
53  * @param optional Integer $max\r
54  * @return Integer\r
55  * @access public\r
56  */\r
57 function crypt_random($min = 0, $max = 0x7FFFFFFF)\r
58 {\r
59     if ($min == $max) {\r
60         return $min;\r
61     }\r
62 \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
67         static $fp;\r
68         if (!$fp) {\r
69             $fp = fopen('/dev/urandom', 'rb');\r
70         }\r
71         extract(unpack('Nrandom', fread($fp, 4)));\r
72 \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
76     }\r
77 \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
80 \r
81        http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/\r
82 \r
83        The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:\r
84 \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
87         static $seeded;\r
88         if (!isset($seeded)) {\r
89             $seeded = true;\r
90             mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));\r
91         }\r
92     }\r
93 \r
94     static $crypto;\r
95 \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
98 \r
99     if (!isset($crypto)) {\r
100         $key = $iv = '';\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
104         }\r
105         switch (true) {\r
106             case class_exists('Crypt_AES'):\r
107                 $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);\r
108                 break;\r
109             case class_exists('Crypt_TripleDES'):\r
110                 $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);\r
111                 break;\r
112             case class_exists('Crypt_DES'):\r
113                 $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);\r
114                 break;\r
115             case class_exists('Crypt_RC4'):\r
116                 $crypto = new Crypt_RC4();\r
117                 break;\r
118             default:\r
119                 extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));\r
120                 return abs($random) % ($max - $min) + $min;\r
121         }\r
122         $crypto->setKey($key);\r
123         $crypto->setIV($iv);\r
124         $crypto->enableContinuousBuffer();\r
125     }\r
126 \r
127     extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));\r
128     return abs($random) % ($max - $min) + $min;\r
129 }\r
130 ?>