]> git.mxchange.org Git - friendica.git/blobdiff - library/phpsec/Crypt/Random.php
bug #96 move libraries to library - better alignment of like rotator
[friendica.git] / library / phpsec / Crypt / Random.php
diff --git a/library/phpsec/Crypt/Random.php b/library/phpsec/Crypt/Random.php
new file mode 100644 (file)
index 0000000..a91c4d3
--- /dev/null
@@ -0,0 +1,130 @@
+<?php\r
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
+\r
+/**\r
+ * Random Number Generator\r
+ *\r
+ * PHP versions 4 and 5\r
+ *\r
+ * Here's a short example of how to use this library:\r
+ * <code>\r
+ * <?php\r
+ *    include('Crypt/Random.php');\r
+ *\r
+ *    echo crypt_random();\r
+ * ?>\r
+ * </code>\r
+ *\r
+ * LICENSE: This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
+ * MA  02111-1307  USA\r
+ *\r
+ * @category   Crypt\r
+ * @package    Crypt_Random\r
+ * @author     Jim Wigginton <terrafrost@php.net>\r
+ * @copyright  MMVII Jim Wigginton\r
+ * @license    http://www.gnu.org/licenses/lgpl.txt\r
+ * @version    $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $\r
+ * @link       http://phpseclib.sourceforge.net\r
+ */\r
+\r
+/**\r
+ * Generate a random value.\r
+ *\r
+ * On 32-bit machines, the largest distance that can exist between $min and $max is 2**31.\r
+ * If $min and $max are farther apart than that then the last ($max - range) numbers.\r
+ *\r
+ * Depending on how this is being used, it may be worth while to write a replacement.  For example,\r
+ * a PHP-based web app that stores its data in an SQL database can collect more entropy than this function\r
+ * can.\r
+ *\r
+ * @param optional Integer $min\r
+ * @param optional Integer $max\r
+ * @return Integer\r
+ * @access public\r
+ */\r
+function crypt_random($min = 0, $max = 0x7FFFFFFF)\r
+{\r
+    if ($min == $max) {\r
+        return $min;\r
+    }\r
+\r
+    // see http://en.wikipedia.org/wiki//dev/random\r
+    // if open_basedir is enabled file_exists() will ouput an "open_basedir restriction in effect" warning,\r
+    // so we suppress it.\r
+    if (@file_exists('/dev/urandom')) {\r
+        static $fp;\r
+        if (!$fp) {\r
+            $fp = fopen('/dev/urandom', 'rb');\r
+        }\r
+        extract(unpack('Nrandom', fread($fp, 4)));\r
+\r
+        // say $min = 0 and $max = 3.  if we didn't do abs() then we could have stuff like this:\r
+        // -4 % 3 + 0 = -1, even though -1 < $min\r
+        return abs($random) % ($max - $min) + $min;\r
+    }\r
+\r
+    /* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called.\r
+       Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here:\r
+\r
+       http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/\r
+\r
+       The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:\r
+\r
+       http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */\r
+    if (version_compare(PHP_VERSION, '5.2.5', '<=')) { \r
+        static $seeded;\r
+        if (!isset($seeded)) {\r
+            $seeded = true;\r
+            mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));\r
+        }\r
+    }\r
+\r
+    static $crypto;\r
+\r
+    // The CSPRNG's Yarrow and Fortuna periodically reseed.  This function can be reseeded by hitting F5\r
+    // in the browser and reloading the page.\r
+\r
+    if (!isset($crypto)) {\r
+        $key = $iv = '';\r
+        for ($i = 0; $i < 8; $i++) {\r
+            $key.= pack('n', mt_rand(0, 0xFFFF));\r
+            $iv .= pack('n', mt_rand(0, 0xFFFF));\r
+        }\r
+        switch (true) {\r
+            case class_exists('Crypt_AES'):\r
+                $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);\r
+                break;\r
+            case class_exists('Crypt_TripleDES'):\r
+                $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);\r
+                break;\r
+            case class_exists('Crypt_DES'):\r
+                $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);\r
+                break;\r
+            case class_exists('Crypt_RC4'):\r
+                $crypto = new Crypt_RC4();\r
+                break;\r
+            default:\r
+                extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));\r
+                return abs($random) % ($max - $min) + $min;\r
+        }\r
+        $crypto->setKey($key);\r
+        $crypto->setIV($iv);\r
+        $crypto->enableContinuousBuffer();\r
+    }\r
+\r
+    extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));\r
+    return abs($random) % ($max - $min) + $min;\r
+}\r
+?>
\ No newline at end of file