PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Crypt/Math.php

https://bitbucket.org/gkawka/zend-framework
PHP | 102 lines | 37 code | 7 blank | 58 comment | 5 complexity | 4d9a9c28e16bb4fc602ffba36d5744bf 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_Crypt
  17. * @subpackage Math
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Math.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. * Get the big endian two's complement of a given big integer in
  65. * binary notation
  66. *
  67. * @param string $long
  68. * @return string
  69. */
  70. public function btwoc($long) {
  71. if (ord($long[0]) > 127) {
  72. return "\x00" . $long;
  73. }
  74. return $long;
  75. }
  76. /**
  77. * Translate a binary form into a big integer string
  78. *
  79. * @param string $binary
  80. * @return string
  81. */
  82. public function fromBinary($binary) {
  83. return $this->_math->binaryToInteger($binary);
  84. }
  85. /**
  86. * Translate a big integer string into a binary form
  87. *
  88. * @param string $integer
  89. * @return string
  90. */
  91. public function toBinary($integer)
  92. {
  93. return $this->_math->integerToBinary($integer);
  94. }
  95. }