PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Crypt/Math/BigInteger/Gmp.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 196 lines | 85 code | 19 blank | 92 comment | 5 complexity | 961c30bc8a8a1fa5c0639bae35c96c8b 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Crypt_Math_BigInteger_Interface
  23. */
  24. require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
  25. /**
  26. * Support for arbitrary precision mathematics in PHP.
  27. *
  28. * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
  29. * extension.
  30. *
  31. * @category Zend
  32. * @package Zend_Crypt
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
  37. {
  38. /**
  39. * Initialise a big integer into an extension specific type.
  40. * @param string $operand
  41. * @param int $base
  42. * @return string
  43. */
  44. public function init($operand, $base = 10)
  45. {
  46. return $operand;
  47. }
  48. /**
  49. * Adds two arbitrary precision numbers
  50. *
  51. * @param string $left_operand
  52. * @param string $right_operand
  53. * @return string
  54. */
  55. public function add($left_operand, $right_operand)
  56. {
  57. $result = gmp_add($left_operand, $right_operand);
  58. return gmp_strval($result);
  59. }
  60. /**
  61. * @param string $left_operand
  62. * @param string $right_operand
  63. * @return string
  64. */
  65. public function subtract($left_operand, $right_operand)
  66. {
  67. $result = gmp_sub($left_operand, $right_operand);
  68. return gmp_strval($result);
  69. }
  70. /**
  71. * Compare two big integers and returns result as an integer where 0 means
  72. * both are identical, 1 that left_operand is larger, or -1 that
  73. * right_operand is larger.
  74. * @param string $left_operand
  75. * @param string $right_operand
  76. * @return int
  77. */
  78. public function compare($left_operand, $right_operand)
  79. {
  80. $result = gmp_cmp($left_operand, $right_operand);
  81. return gmp_strval($result);
  82. }
  83. /**
  84. * Divide two big integers and return result or NULL if the denominator
  85. * is zero.
  86. * @param string $left_operand
  87. * @param string $right_operand
  88. * @return string|null
  89. */
  90. public function divide($left_operand, $right_operand)
  91. {
  92. $result = gmp_div($left_operand, $right_operand);
  93. return gmp_strval($result);
  94. }
  95. /**
  96. * @param string $left_operand
  97. * @param string $right_operand
  98. * @return string
  99. */
  100. public function modulus($left_operand, $modulus)
  101. {
  102. $result = gmp_mod($left_operand, $modulus);
  103. return gmp_strval($result);
  104. }
  105. /**
  106. * @param string $left_operand
  107. * @param string $right_operand
  108. * @return string
  109. */
  110. public function multiply($left_operand, $right_operand)
  111. {
  112. $result = gmp_mul($left_operand, $right_operand);
  113. return gmp_strval($result);
  114. }
  115. /**
  116. * @param string $left_operand
  117. * @param string $right_operand
  118. * @return string
  119. */
  120. public function pow($left_operand, $right_operand)
  121. {
  122. $result = gmp_pow($left_operand, $right_operand);
  123. return gmp_strval($result);
  124. }
  125. /**
  126. * @param string $left_operand
  127. * @param string $right_operand
  128. * @return string
  129. */
  130. public function powmod($left_operand, $right_operand, $modulus)
  131. {
  132. $result = gmp_powm($left_operand, $right_operand, $modulus);
  133. return gmp_strval($result);
  134. }
  135. /**
  136. * @param string $left_operand
  137. * @param string $right_operand
  138. * @return string
  139. */
  140. public function sqrt($operand)
  141. {
  142. $result = gmp_sqrt($operand);
  143. return gmp_strval($result);
  144. }
  145. public function binaryToInteger($operand)
  146. {
  147. $result = '0';
  148. while (strlen($operand)) {
  149. $ord = ord(substr($operand, 0, 1));
  150. $result = gmp_add(gmp_mul($result, 256), $ord);
  151. $operand = substr($operand, 1);
  152. }
  153. return gmp_strval($result);
  154. }
  155. public function integerToBinary($operand)
  156. {
  157. $bigInt = gmp_strval($operand, 16);
  158. if (strlen($bigInt) % 2 != 0) {
  159. $bigInt = '0' . $bigInt;
  160. } else if ($bigInt[0] > '7') {
  161. $bigInt = '00' . $bigInt;
  162. }
  163. $return = pack("H*", $bigInt);
  164. return $return;
  165. }
  166. public function hexToDecimal($operand)
  167. {
  168. $return = '0';
  169. while(strlen($hex)) {
  170. $hex = hexdec(substr($operand, 0, 4));
  171. $dec = gmp_add(gmp_mul($return, 65536), $hex);
  172. $operand = substr($operand, 4);
  173. }
  174. return $return;
  175. }
  176. }