PHPMath.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * A class for arbitrary precision math functions implemented in PHP
  4. *
  5. * PHP version 5.3
  6. *
  7. * @category PHPPasswordLib
  8. * @package Core
  9. * @subpackage BigMath
  10. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  11. * @copyright 2011 The Authors
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version Build @@version@@
  14. */
  15. namespace SecurityLib\BigMath;
  16. use SecurityLib\BaseConverter;
  17. /**
  18. * A class for arbitrary precision math functions implemented in PHP
  19. *
  20. * @category PHPPasswordLib
  21. * @package Core
  22. * @subpackage BigMath
  23. */
  24. class PHPMath extends \SecurityLib\BigMath {
  25. /**
  26. * Add two numbers together
  27. *
  28. * @param string $left The left argument
  29. * @param string $right The right argument
  30. *
  31. * @return A base-10 string of the sum of the two arguments
  32. */
  33. public function add($left, $right) {
  34. if (empty($left)) {
  35. return $right;
  36. } elseif (empty($right)) {
  37. return $left;
  38. }
  39. $negative = '';
  40. if ($left[0] == '-' && $right[0] == '-') {
  41. $negative = '-';
  42. $left = substr($left, 1);
  43. $right = substr($right, 1);
  44. } elseif ($left[0] == '-') {
  45. return $this->subtract($right, substr($left, 1));
  46. } elseif ($right[0] == '-') {
  47. return $this->subtract($left, substr($right, 1));
  48. }
  49. $left = $this->normalize($left);
  50. $right = $this->normalize($right);
  51. $result = BaseConverter::convertFromBinary(
  52. $this->addBinary($left, $right),
  53. '0123456789'
  54. );
  55. return $negative . $result;
  56. }
  57. /**
  58. * Subtract two numbers
  59. *
  60. * @param string $left The left argument
  61. * @param string $right The right argument
  62. *
  63. * @return A base-10 string of the difference of the two arguments
  64. */
  65. public function subtract($left, $right) {
  66. if (empty($left)) {
  67. return $right;
  68. } elseif (empty($right)) {
  69. return $left;
  70. } elseif ($right[0] == '-') {
  71. return $this->add($left, substr($right, 1));
  72. } elseif ($left[0] == '-') {
  73. return '-' . $this->add(ltrim($left, '-'), $right);
  74. }
  75. $left = $this->normalize($left);
  76. $right = $this->normalize($right);
  77. $results = $this->subtractBinary($left, $right);
  78. $result = BaseConverter::convertFromBinary($results[1], '0123456789');
  79. return $results[0] . $result;
  80. }
  81. /**
  82. * Add two binary strings together
  83. *
  84. * @param string $left The left argument
  85. * @param string $right The right argument
  86. *
  87. * @return string The binary result
  88. */
  89. protected function addBinary($left, $right) {
  90. $len = max(strlen($left), strlen($right));
  91. $left = str_pad($left, $len, chr(0), STR_PAD_LEFT);
  92. $right = str_pad($right, $len, chr(0), STR_PAD_LEFT);
  93. $result = '';
  94. $carry = 0;
  95. for ($i = 0; $i < $len; $i++) {
  96. $sum = ord($left[$len - $i - 1])
  97. + ord($right[$len - $i - 1])
  98. + $carry;
  99. $result .= chr($sum % 256);
  100. $carry = $sum >> 8;
  101. }
  102. while ($carry) {
  103. $result .= chr($carry % 256);
  104. $carry >>= 8;
  105. }
  106. return strrev($result);
  107. }
  108. /**
  109. * Subtract two binary strings using 256's compliment
  110. *
  111. * @param string $left The left argument
  112. * @param string $right The right argument
  113. *
  114. * @return string The binary result
  115. */
  116. protected function subtractBinary($left, $right) {
  117. $len = max(strlen($left), strlen($right));
  118. $left = str_pad($left, $len, chr(0), STR_PAD_LEFT);
  119. $right = str_pad($right, $len, chr(0), STR_PAD_LEFT);
  120. $right = $this->compliment($right);
  121. $result = $this->addBinary($left, $right);
  122. if (strlen($result) > $len) {
  123. // Positive Result
  124. $carry = substr($result, 0, -1 * $len);
  125. $result = substr($result, strlen($carry));
  126. return array(
  127. '',
  128. $this->addBinary($result, $carry)
  129. );
  130. }
  131. return array('-', $this->compliment($result));
  132. }
  133. /**
  134. * Take the 256 base compliment
  135. *
  136. * @param string $string The binary string to compliment
  137. *
  138. * @return string The complimented string
  139. */
  140. protected function compliment($string) {
  141. $result = '';
  142. $len = strlen($string);
  143. for ($i = 0; $i < $len; $i++) {
  144. $result .= chr(255 - ord($string[$i]));
  145. }
  146. return $result;
  147. }
  148. /**
  149. * Transform a string number into a binary string using base autodetection
  150. *
  151. * @param string $string The string to transform
  152. *
  153. * @return string The binary transformed number
  154. */
  155. protected function normalize($string) {
  156. return BaseConverter::convertToBinary(
  157. $string,
  158. '0123456789'
  159. );
  160. }
  161. }