Math.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-2009 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-2009 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. echo $frandom;
  50. return fread($frandom, strlen($maximum) - 1);
  51. }
  52. }
  53. if (strlen($maximum) < 4) {
  54. return mt_rand($minimum, $maximum - 1);
  55. }
  56. $rand = '';
  57. $i2 = strlen($maximum) - 1;
  58. for ($i = 1;$i < $i2;$i++) {
  59. $rand .= mt_rand(0,9);
  60. }
  61. $rand .= mt_rand(0,9);
  62. return $rand;
  63. }
  64. /**
  65. * Get the big endian two's complement of a given big integer in
  66. * binary notation
  67. *
  68. * @param string $long
  69. * @return string
  70. */
  71. public function btwoc($long) {
  72. if (ord($long[0]) > 127) {
  73. return "\x00" . $long;
  74. }
  75. return $long;
  76. }
  77. /**
  78. * Translate a binary form into a big integer string
  79. *
  80. * @param string $binary
  81. * @return string
  82. */
  83. public function fromBinary($binary) {
  84. return $this->_math->binaryToInteger($binary);
  85. }
  86. /**
  87. * Translate a big integer string into a binary form
  88. *
  89. * @param string $integer
  90. * @return string
  91. */
  92. public function toBinary($integer)
  93. {
  94. return $this->_math->integerToBinary($integer);
  95. }
  96. }