Random.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Random Number Generator
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * Here's a short example of how to use this library:
  9. * <code>
  10. * <?php
  11. * include('Crypt/Random.php');
  12. *
  13. * echo crypt_random();
  14. * ?>
  15. * </code>
  16. *
  17. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in
  25. * all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. * THE SOFTWARE.
  34. *
  35. * @category Crypt
  36. * @package Crypt_Random
  37. * @author Jim Wigginton <terrafrost@php.net>
  38. * @copyright MMVII Jim Wigginton
  39. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  40. * @version $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $
  41. * @link http://phpseclib.sourceforge.net
  42. */
  43. /**
  44. * Generate a random value.
  45. *
  46. * On 32-bit machines, the largest distance that can exist between $min and $max is 2**31.
  47. * If $min and $max are farther apart than that then the last ($max - range) numbers.
  48. *
  49. * Depending on how this is being used, it may be worth while to write a replacement. For example,
  50. * a PHP-based web app that stores its data in an SQL database can collect more entropy than this function
  51. * can.
  52. *
  53. * @param optional Integer $min
  54. * @param optional Integer $max
  55. * @return Integer
  56. * @access public
  57. */
  58. function crypt_random($min = 0, $max = 0x7FFFFFFF)
  59. {
  60. if ($min == $max) {
  61. return $min;
  62. }
  63. // see http://en.wikipedia.org/wiki//dev/random
  64. static $urandom = true;
  65. if ($urandom === true) {
  66. // Warning's will be output unles the error suppression operator is used. Errors such as
  67. // "open_basedir restriction in effect", "Permission denied", "No such file or directory", etc.
  68. $urandom = @fopen('/dev/urandom', 'rb');
  69. }
  70. if (!is_bool($urandom)) {
  71. extract(unpack('Nrandom', fread($urandom, 4)));
  72. // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this:
  73. // -4 % 3 + 0 = -1, even though -1 < $min
  74. return abs($random) % ($max - $min) + $min;
  75. }
  76. /* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called.
  77. Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here:
  78. 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:
  80. http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */
  81. if (version_compare(PHP_VERSION, '5.2.5', '<=')) {
  82. static $seeded;
  83. if (!isset($seeded)) {
  84. $seeded = true;
  85. mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));
  86. }
  87. }
  88. static $crypto;
  89. // The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5
  90. // in the browser and reloading the page.
  91. if (!isset($crypto)) {
  92. $key = $iv = '';
  93. for ($i = 0; $i < 8; $i++) {
  94. $key.= pack('n', mt_rand(0, 0xFFFF));
  95. $iv .= pack('n', mt_rand(0, 0xFFFF));
  96. }
  97. switch (true) {
  98. case class_exists('Crypt_AES'):
  99. $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
  100. break;
  101. case class_exists('Crypt_TripleDES'):
  102. $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
  103. break;
  104. case class_exists('Crypt_DES'):
  105. $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
  106. break;
  107. case class_exists('Crypt_RC4'):
  108. $crypto = new Crypt_RC4();
  109. break;
  110. default:
  111. extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));
  112. return abs($random) % ($max - $min) + $min;
  113. }
  114. $crypto->setKey($key);
  115. $crypto->setIV($iv);
  116. $crypto->enableContinuousBuffer();
  117. }
  118. extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));
  119. return abs($random) % ($max - $min) + $min;
  120. }
  121. ?>