PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Math/Rand.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 171 lines | 98 code | 13 blank | 60 comment | 19 complexity | 8cb782aa9d3c061efbbf1ad49012e444 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Math;
  10. /**
  11. * Pseudorandom number generator (PRNG)
  12. */
  13. abstract class Rand
  14. {
  15. /**
  16. * Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback
  17. *
  18. * @param integer $length
  19. * @param bool $strong true if you need a strong random generator (cryptography)
  20. * @return string
  21. * @throws Exception\RuntimeException
  22. */
  23. public static function getBytes($length, $strong = false)
  24. {
  25. if ($length <= 0) {
  26. return false;
  27. }
  28. if (extension_loaded('openssl')) {
  29. $rand = openssl_random_pseudo_bytes($length, $secure);
  30. if ($secure === true) {
  31. return $rand;
  32. }
  33. }
  34. if (extension_loaded('mcrypt')) {
  35. // PHP bug #55169
  36. // @see https://bugs.php.net/bug.php?id=55169
  37. if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' ||
  38. version_compare(PHP_VERSION, '5.3.7') >= 0) {
  39. $rand = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  40. if ($rand !== false && strlen($rand) === $length) {
  41. return $rand;
  42. }
  43. }
  44. }
  45. if ($strong) {
  46. throw new Exception\RuntimeException(
  47. 'This PHP environment doesn\'t support secure random number generation. ' .
  48. 'Please consider to install the OpenSSL and/or Mcrypt extensions'
  49. );
  50. }
  51. $rand = '';
  52. for ($i = 0; $i < $length; $i++) {
  53. $rand .= chr(mt_rand(0, 255));
  54. }
  55. return $rand;
  56. }
  57. /**
  58. * Generate random boolean
  59. *
  60. * @param bool $strong true if you need a strong random generator (cryptography)
  61. * @return bool
  62. */
  63. public static function getBoolean($strong = false)
  64. {
  65. $byte = static::getBytes(1, $strong);
  66. return (bool) (ord($byte) % 2);
  67. }
  68. /**
  69. * Generate a random integer between $min and $max
  70. *
  71. * @param integer $min
  72. * @param integer $max
  73. * @param bool $strong true if you need a strong random generator (cryptography)
  74. * @return integer
  75. * @throws Exception\DomainException
  76. */
  77. public static function getInteger($min, $max, $strong = false)
  78. {
  79. if ($min > $max) {
  80. throw new Exception\DomainException(
  81. 'The min parameter must be lower than max parameter'
  82. );
  83. }
  84. $range = $max - $min;
  85. if ($range == 0) {
  86. return $max;
  87. } elseif ($range > PHP_INT_MAX || is_float($range)) {
  88. throw new Exception\DomainException(
  89. 'The supplied range is too great to generate'
  90. );
  91. }
  92. $log = log($range, 2);
  93. $bytes = (int) ($log / 8) + 1;
  94. $bits = (int) $log + 1;
  95. $filter = (int) (1 << $bits) - 1;
  96. do {
  97. $rnd = hexdec(bin2hex(self::getBytes($bytes, $strong)));
  98. $rnd = $rnd & $filter;
  99. } while ($rnd > $range);
  100. return ($min + $rnd);
  101. }
  102. /**
  103. * Generate random float (0..1)
  104. * This function generates floats with platform-dependent precision
  105. *
  106. * PHP uses double precision floating-point format (64-bit) which has
  107. * 52-bits of significand precision. We gather 7 bytes of random data,
  108. * and we fix the exponent to the bias (1023). In this way we generate
  109. * a float of 1.mantissa.
  110. *
  111. * @param bool $strong true if you need a strong random generator (cryptography)
  112. * @return float
  113. */
  114. public static function getFloat($strong = false)
  115. {
  116. $bytes = static::getBytes(7, $strong);
  117. $bytes[6] = $bytes[6] | chr(0xF0);
  118. $bytes .= chr(63); // exponent bias (1023)
  119. list(, $float) = unpack('d', $bytes);
  120. return ($float - 1);
  121. }
  122. /**
  123. * Generate a random string of specified length.
  124. *
  125. * Uses supplied character list for generating the new string.
  126. * If no character list provided - uses Base 64 character set.
  127. *
  128. * @param integer $length
  129. * @param string|null $charlist
  130. * @param bool $strong true if you need a strong random generator (cryptography)
  131. * @return string
  132. * @throws Exception\DomainException
  133. */
  134. public static function getString($length, $charlist = null, $strong = false)
  135. {
  136. if ($length < 1) {
  137. throw new Exception\DomainException('Length should be >= 1');
  138. }
  139. // charlist is empty or not provided
  140. if (empty($charlist)) {
  141. $numBytes = ceil($length * 0.75);
  142. $bytes = static::getBytes($numBytes, $strong);
  143. return substr(rtrim(base64_encode($bytes), '='), 0, $length);
  144. }
  145. $listLen = strlen($charlist);
  146. if ($listLen == 1) {
  147. return str_repeat($charlist, $length);
  148. }
  149. $bytes = static::getBytes($length, $strong);
  150. $pos = 0;
  151. $result = '';
  152. for ($i = 0; $i < $length; $i++) {
  153. $pos = ($pos + ord($bytes[$i])) % $listLen;
  154. $result .= $charlist[$pos];
  155. }
  156. return $result;
  157. }
  158. }