File indexing completed on 2024-12-29 05:27:32

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Crypt
0017  * @subpackage Math
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Crypt_Math_BigInteger_Interface
0025  */
0026 // require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
0027 
0028 /**
0029  * Support for arbitrary precision mathematics in PHP.
0030  *
0031  * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath
0032  * extension.
0033  *
0034  * @category   Zend
0035  * @package    Zend_Crypt
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
0040 {
0041 
0042     /**
0043      * Initialise a big integer into an extension specific type.
0044      * @param string $operand
0045      * @param int $base
0046      * @return string
0047      */
0048     public function init($operand, $base = 10)
0049     {
0050         return $operand;
0051     }
0052 
0053     /**
0054      * Adds two arbitrary precision numbers
0055      *
0056      * @param resource $left_operand
0057      * @param resource $right_operand
0058      * @return string
0059      */
0060     public function add($left_operand, $right_operand)
0061     {
0062         $result = gmp_add($left_operand, $right_operand);
0063         return gmp_strval($result);
0064     }
0065 
0066     /**
0067      * Subtract numbers
0068      *
0069      * @param resource $left_operand
0070      * @param resource $right_operand
0071      * @return string
0072      */
0073     public function subtract($left_operand, $right_operand)
0074     {
0075         $result = gmp_sub($left_operand, $right_operand);
0076         return gmp_strval($result);
0077     }
0078 
0079     /**
0080      * Compare two big integers and returns result as an integer where 0 means
0081      * both are identical, 1 that left_operand is larger, or -1 that
0082      * right_operand is larger.
0083      *
0084      * @param resource $left_operand
0085      * @param resource $right_operand
0086      * @return int
0087      */
0088     public function compare($left_operand, $right_operand)
0089     {
0090         $result = gmp_cmp($left_operand, $right_operand);
0091         return gmp_strval($result);
0092     }
0093 
0094     /**
0095      * Divide two big integers and return result or NULL if the denominator
0096      * is zero.
0097      *
0098      * @param resource $left_operand
0099      * @param resource $right_operand
0100      * @return string|null
0101      */
0102     public function divide($left_operand, $right_operand)
0103     {
0104         $result = gmp_div($left_operand, $right_operand);
0105         return gmp_strval($result);
0106     }
0107 
0108     /**
0109      * Modulo operation
0110      *
0111      * @param resource $left_operand
0112      * @param resource $modulus
0113      * @internal param string $right_operand
0114      * @return string
0115      */
0116     public function modulus($left_operand, $modulus)
0117     {
0118         $result = gmp_mod($left_operand, $modulus);
0119         return gmp_strval($result);
0120     }
0121 
0122     /**
0123      * Multiply numbers
0124      *
0125      * @param resource $left_operand
0126      * @param resource $right_operand
0127      * @return string
0128      */
0129     public function multiply($left_operand, $right_operand)
0130     {
0131         $result = gmp_mul($left_operand, $right_operand);
0132         return gmp_strval($result);
0133     }
0134 
0135     /**
0136      * Raise number into power
0137      *
0138      * @param resource $left_operand
0139      * @param int      $right_operand
0140      * @return string
0141      */
0142     public function pow($left_operand, $right_operand)
0143     {
0144         $result = gmp_pow($left_operand, $right_operand);
0145         return gmp_strval($result);
0146     }
0147 
0148     /**
0149      * Raise number into power with modulo
0150      *
0151      * @param resource $left_operand
0152      * @param resource $right_operand
0153      * @param resource $modulus
0154      * @return string
0155      */
0156     public function powmod($left_operand, $right_operand, $modulus)
0157     {
0158         $result = gmp_powm($left_operand, $right_operand, $modulus);
0159         return gmp_strval($result);
0160     }
0161 
0162     /**
0163      * Calculate square root
0164      *
0165      * @param $operand
0166      * @return string
0167      */
0168     public function sqrt($operand)
0169     {
0170         $result = gmp_sqrt($operand);
0171         return gmp_strval($result);
0172     }
0173 
0174     /**
0175      * @param string $operand
0176      * @return string
0177      */
0178     public function binaryToInteger($operand)
0179     {
0180         $result = '0';
0181         while (strlen($operand)) {
0182             $ord = ord(substr($operand, 0, 1));
0183             $result = gmp_add(gmp_mul($result, 256), $ord);
0184             $operand = substr($operand, 1);
0185         }
0186         return gmp_strval($result);
0187     }
0188 
0189     /**
0190      * @param resource $operand GMP number resource
0191      * @return string
0192      */
0193     public function integerToBinary($operand)
0194     {
0195         $bigInt = gmp_strval($operand, 16);
0196         if (strlen($bigInt) % 2 != 0) {
0197             $bigInt = '0' . $bigInt;
0198         } else if ($bigInt[0] > '7') {
0199             $bigInt = '00' . $bigInt;
0200         }
0201         $return = pack("H*", $bigInt);
0202         return $return;
0203     }
0204 
0205     /**
0206      * @param string $operand
0207      * @return string
0208      */
0209     public function hexToDecimal($operand)
0210     {
0211         $return = '0';
0212         while(strlen($hex)) {
0213             $hex = hexdec(substr($operand, 0, 4));
0214             $dec = gmp_add(gmp_mul($return, 65536), $hex);
0215             $operand = substr($operand, 4);
0216         }
0217         return $return;
0218     }
0219 
0220 }