Math.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage Math
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Crypt_Math_BigInteger
  24. */
  25. #require_once 'Zend/Crypt/Math/BigInteger.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Crypt
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Crypt_Math extends Zend_Crypt_Math_BigInteger
  33. {
  34. /**
  35. * Generate a pseudorandom number within the given range.
  36. * Will attempt to read from a systems RNG if it exists or else utilises
  37. * a simple random character to maximum length process. Simplicity
  38. * is a factor better left for development...
  39. *
  40. * @param string|int $minimum
  41. * @param string|int $maximum
  42. * @return string
  43. */
  44. public function rand($minimum, $maximum)
  45. {
  46. if (file_exists('/dev/urandom')) {
  47. $frandom = fopen('/dev/urandom', 'r');
  48. if ($frandom !== false) {
  49. return fread($frandom, strlen($maximum) - 1);
  50. }
  51. }
  52. if (strlen($maximum) < 4) {
  53. return mt_rand($minimum, $maximum - 1);
  54. }
  55. $rand = '';
  56. $i2 = strlen($maximum) - 1;
  57. for ($i = 1; $i < $i2; $i++) {
  58. $rand .= mt_rand(0, 9);
  59. }
  60. $rand .= mt_rand(0, 9);
  61. return $rand;
  62. }
  63. /**
  64. * Return a random strings of $length bytes
  65. *
  66. * @param integer $length
  67. * @param boolean $strong
  68. * @return string
  69. */
  70. public static function randBytes($length, $strong = false)
  71. {
  72. $length = (int) $length;
  73. if ($length <= 0) {
  74. return false;
  75. }
  76. if (function_exists('random_bytes')) { // available in PHP 7
  77. return random_bytes($length);
  78. }
  79. if (function_exists('mcrypt_create_iv')) {
  80. $bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  81. if ($bytes !== false && strlen($bytes) === $length) {
  82. return $bytes;
  83. }
  84. }
  85. if (file_exists('/dev/urandom') && is_readable('/dev/urandom')) {
  86. $frandom = fopen('/dev/urandom', 'r');
  87. if ($frandom !== false) {
  88. return fread($frandom, $length);
  89. }
  90. }
  91. if (true === $strong) {
  92. require_once 'Zend/Crypt/Exception.php';
  93. throw new Zend_Crypt_Exception(
  94. 'This PHP environment doesn\'t support secure random number generation. ' .
  95. 'Please consider installing the OpenSSL and/or Mcrypt extensions'
  96. );
  97. }
  98. $rand = '';
  99. for ($i = 0; $i < $length; $i++) {
  100. $rand .= chr(mt_rand(0, 255));
  101. }
  102. return $rand;
  103. }
  104. /**
  105. * Return a random integer between $min and $max
  106. *
  107. * @param integer $min
  108. * @param integer $max
  109. * @param boolean $strong
  110. * @return integer
  111. */
  112. public static function randInteger($min, $max, $strong = false)
  113. {
  114. if ($min > $max) {
  115. require_once 'Zend/Crypt/Exception.php';
  116. throw new Zend_Crypt_Exception(
  117. 'The min parameter must be lower than max parameter'
  118. );
  119. }
  120. $range = $max - $min;
  121. if ($range == 0) {
  122. return $max;
  123. } elseif ($range > PHP_INT_MAX || is_float($range)) {
  124. require_once 'Zend/Crypt/Exception.php';
  125. throw new Zend_Crypt_Exception(
  126. 'The supplied range is too great to generate'
  127. );
  128. }
  129. if (function_exists('random_int')) { // available in PHP 7
  130. return random_int($min, $max);
  131. }
  132. // calculate number of bits required to store range on this machine
  133. $r = $range;
  134. $bits = 0;
  135. while ($r) {
  136. $bits++;
  137. $r >>= 1;
  138. }
  139. $bits = (int) max($bits, 1);
  140. $bytes = (int) max(ceil($bits / 8), 1);
  141. $filter = (int) ((1 << $bits) - 1);
  142. do {
  143. $rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
  144. $rnd &= $filter;
  145. } while ($rnd > $range);
  146. return ($min + $rnd);
  147. }
  148. /**
  149. * Get the big endian two's complement of a given big integer in
  150. * binary notation
  151. *
  152. * @param string $long
  153. * @return string
  154. */
  155. public function btwoc($long)
  156. {
  157. if (ord($long[0]) > 127) {
  158. return "\x00" . $long;
  159. }
  160. return $long;
  161. }
  162. /**
  163. * Translate a binary form into a big integer string
  164. *
  165. * @param string $binary
  166. * @return string
  167. */
  168. public function fromBinary($binary)
  169. {
  170. return $this->_math->binaryToInteger($binary);
  171. }
  172. /**
  173. * Translate a big integer string into a binary form
  174. *
  175. * @param string $integer
  176. * @return string
  177. */
  178. public function toBinary($integer)
  179. {
  180. return $this->_math->integerToBinary($integer);
  181. }
  182. }