PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Math/Math.php

https://github.com/necrogami/zf2
PHP | 136 lines | 71 code | 5 blank | 60 comment | 14 complexity | eb060a5ab5cbb19da3bd6ccc83ea3ebe MD5 | raw file
  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_Math
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. namespace Zend\Math;
  21. /**
  22. * @category Zend
  23. * @package Zend_Math
  24. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Math extends BigInteger
  28. {
  29. /**
  30. * Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback
  31. *
  32. * @param integer $length
  33. * @param boolean $strong true if you need a strong random generator (cryptography)
  34. * @return string
  35. */
  36. public static function randBytes($length, $strong = false)
  37. {
  38. if ($length <= 0) {
  39. return false;
  40. }
  41. if (extension_loaded('openssl')) {
  42. $rand = openssl_random_pseudo_bytes($length, $secure);
  43. if ($secure === true) {
  44. return $rand;
  45. }
  46. }
  47. if (extension_loaded('mcrypt')) {
  48. // PHP bug #55169
  49. // @see https://bugs.php.net/bug.php?id=55169
  50. if (strtoupper(substr(PHP_OS, 0, 3)) ==! 'WIN' ||
  51. version_compare(PHP_VERSION, '5.3.7') >= 0) {
  52. $rand = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  53. if ($rand !== false && strlen($rand) === $length) {
  54. return $rand;
  55. }
  56. }
  57. }
  58. if ($strong) {
  59. throw new Exception\RuntimeException(
  60. 'This PHP environment doesn\'t support secure random number generation. ' .
  61. 'Please consider to install the OpenSSL and/or Mcrypt extensions'
  62. );
  63. }
  64. $rand = '';
  65. for ($i = 0; $i < $length; $i++) {
  66. $rand .= chr(mt_rand(0, 255));
  67. }
  68. return $rand;
  69. }
  70. /**
  71. * Generate a random number between $min and $max
  72. *
  73. * @param integer $min
  74. * @param integer $max
  75. * @param boolean $strong true if you need a strong random generator (cryptography)
  76. * @return integer
  77. */
  78. public static function rand($min, $max, $strong = false)
  79. {
  80. if ($min > $max) {
  81. throw new Exception\InvalidArgumentException(
  82. 'The min parameter must be lower than max parameter'
  83. );
  84. }
  85. $range = $max - $min;
  86. if ($range == 0) {
  87. return $max;
  88. } elseif ($range > PHP_INT_MAX || is_float($range)) {
  89. throw new Exception\InvalidArgumentException(
  90. 'The supplied range is too great to generate'
  91. );
  92. }
  93. $bytes = (int) max(log($range,2) / 8, 1);
  94. $rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
  95. return $min + $rnd % ($range+1);
  96. }
  97. /**
  98. * Get the big endian two's complement of a given big integer in
  99. * binary notation
  100. *
  101. * @param string $long
  102. * @return string
  103. */
  104. public function btwoc($long)
  105. {
  106. if (ord($long[0]) > 127) {
  107. return "\x00" . $long;
  108. }
  109. return $long;
  110. }
  111. /**
  112. * Translate a binary form into a big integer string
  113. *
  114. * @param string $binary
  115. * @return string
  116. */
  117. public function fromBinary($binary)
  118. {
  119. return $this->_math->binaryToInteger($binary);
  120. }
  121. /**
  122. * Translate a big integer string into a binary form
  123. *
  124. * @param string $integer
  125. * @return string
  126. */
  127. public function toBinary($integer)
  128. {
  129. return $this->_math->integerToBinary($integer);
  130. }
  131. }