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_Bcmath 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_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
0040 {
0041 
0042     /**
0043      * Initialise a big integer into an extension specific type. This is not
0044      * applicable to BCMath.
0045      *
0046      * @param string $operand
0047      * @param int $base
0048      * @return string
0049      */
0050     public function init($operand, $base = 10)
0051     {
0052         return $operand;
0053     }
0054 
0055     /**
0056      * Adds two arbitrary precision numbers
0057      *
0058      * @param string $left_operand
0059      * @param string $right_operand
0060      * @return string
0061      */
0062     public function add($left_operand, $right_operand)
0063     {
0064         return bcadd($left_operand, $right_operand);
0065     }
0066 
0067     /**
0068      * Subtract one arbitrary precision number from another
0069      *
0070      * @param string $left_operand
0071      * @param string $right_operand
0072      * @return string
0073      */
0074     public function subtract($left_operand, $right_operand)
0075     {
0076         return bcsub($left_operand, $right_operand);
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 string $left_operand
0085      * @param string $right_operand
0086      * @return int
0087      */
0088     public function compare($left_operand, $right_operand)
0089     {
0090         return bccomp($left_operand, $right_operand);
0091     }
0092 
0093     /**
0094      * Divide two big integers and return result or NULL if the denominator
0095      * is zero.
0096      *
0097      * @param string $left_operand
0098      * @param string $right_operand
0099      * @return string|null
0100      */
0101     public function divide($left_operand, $right_operand)
0102     {
0103         return bcdiv($left_operand, $right_operand);
0104     }
0105 
0106     /**
0107      * Get modulus of an arbitrary precision number
0108      *
0109      * @param string $left_operand
0110      * @param string $modulus
0111      * @return string
0112      */
0113     public function modulus($left_operand, $modulus)
0114     {
0115         return bcmod($left_operand, $modulus);
0116     }
0117 
0118     /**
0119      * Multiply two arbitrary precision numbers
0120      *
0121      * @param string $left_operand
0122      * @param string $right_operand
0123      * @return string
0124      */
0125     public function multiply($left_operand, $right_operand)
0126     {
0127         return bcmul($left_operand, $right_operand);
0128     }
0129 
0130     /**
0131      * Raise an arbitrary precision number to another
0132      *
0133      * @param string $left_operand
0134      * @param string $right_operand
0135      * @return string
0136      */
0137     public function pow($left_operand, $right_operand)
0138     {
0139         return bcpow($left_operand, $right_operand);
0140     }
0141 
0142     /**
0143      * Raise an arbitrary precision number to another, reduced by a specified
0144      * modulus
0145      *
0146      * @param string $left_operand
0147      * @param string $right_operand
0148      * @param string $modulus
0149      * @return string
0150      */
0151     public function powmod($left_operand, $right_operand, $modulus)
0152     {
0153         return bcpowmod($left_operand, $right_operand, $modulus);
0154     }
0155 
0156     /**
0157      * Get the square root of an arbitrary precision number
0158      *
0159      * @param string $operand
0160      * @return string
0161      */
0162     public function sqrt($operand)
0163     {
0164         return bcsqrt($operand);
0165     }
0166 
0167     /**
0168      * @param string $operand
0169      * @return string
0170      */
0171     public function binaryToInteger($operand)
0172     {
0173         $result = '0';
0174         while (strlen($operand)) {
0175             $ord = ord(substr($operand, 0, 1));
0176             $result = bcadd(bcmul($result, 256), $ord);
0177             $operand = substr($operand, 1);
0178         }
0179         return $result;
0180     }
0181 
0182     /**
0183      * @param string $operand
0184      * @return string
0185      */
0186     public function integerToBinary($operand)
0187     {
0188         $cmp = bccomp($operand, 0);
0189         $return = '';
0190         if ($cmp == 0) {
0191             return "\0";
0192         }
0193         while (bccomp($operand, 0) > 0) {
0194             $return = chr(bcmod($operand, 256)) . $return;
0195             $operand = bcdiv($operand, 256);
0196         }
0197         if (ord($return[0]) > 127) {
0198             $return = "\0" . $return;
0199         }
0200         return $return;
0201     }
0202 
0203     /**public function integerToBinary($operand)
0204     {
0205         $return = '';
0206         while(bccomp($operand, '0')) {
0207             $return .= chr(bcmod($operand, '256'));
0208             $operand = bcdiv($operand, '256');
0209         }
0210         return $return;
0211     }**/ // Prior version for referenced offset
0212 
0213     /**
0214      * @param string $operand
0215      * @return string
0216      */
0217     public function hexToDecimal($operand)
0218     {
0219         $return = '0';
0220         while(strlen($hex)) {
0221             $hex = hexdec(substr($operand, 0, 4));
0222             $dec = bcadd(bcmul($return, 65536), $hex);
0223             $operand = substr($operand, 4);
0224         }
0225         return $return;
0226     }
0227 }