[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Zend/Crypt/Math/BigInteger/ -> Gmp.php (source)

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


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1