diff options
Diffstat (limited to 'library/vendor/Zend/Crypt/Math')
-rw-r--r-- | library/vendor/Zend/Crypt/Math/BigInteger.php | 113 | ||||
-rw-r--r-- | library/vendor/Zend/Crypt/Math/BigInteger/Bcmath.php | 226 | ||||
-rw-r--r-- | library/vendor/Zend/Crypt/Math/BigInteger/Exception.php | 35 | ||||
-rw-r--r-- | library/vendor/Zend/Crypt/Math/BigInteger/Gmp.php | 219 | ||||
-rw-r--r-- | library/vendor/Zend/Crypt/Math/BigInteger/Interface.php | 51 | ||||
-rw-r--r-- | library/vendor/Zend/Crypt/Math/Exception.php | 35 |
6 files changed, 679 insertions, 0 deletions
diff --git a/library/vendor/Zend/Crypt/Math/BigInteger.php b/library/vendor/Zend/Crypt/Math/BigInteger.php new file mode 100644 index 0000000..0b98cec --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/BigInteger.php @@ -0,0 +1,113 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * Support for arbitrary precision mathematics in PHP. + * + * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp + * and big_int. Since each offer similar functionality, but availability of + * each differs across installations of PHP, this wrapper attempts to select + * the fastest option available and encapsulate a subset of its functionality + * which all extensions share in common. + * + * This class requires one of the three extensions to be available. BCMATH + * while the slowest, is available by default under Windows, and under Unix + * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp + * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp" + * flag. BIG_INT support is available from a big_int PHP library available from + * only from PECL (a Windows port is not available). + * + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Crypt_Math_BigInteger +{ + + /** + * Holds an instance of one of the three arbitrary precision wrappers. + * + * @var Zend_Crypt_Math_BigInteger_Interface + */ + protected $_math = null; + + /** + * Constructor; a Factory which detects a suitable PHP extension for + * arbitrary precision math and instantiates the suitable wrapper + * object. + * + * @param string $extension + * @throws Zend_Crypt_Math_BigInteger_Exception + */ + public function __construct($extension = null) + { + if ($extension !== null && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) { + throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint'); + } + $this->_loadAdapter($extension); + } + + /** + * Redirect all public method calls to the wrapped extension object. + * + * @param string $methodName + * @param array $args + * @return mixed + * @throws Zend_Crypt_Math_BigInteger_Exception + */ + public function __call($methodName, $args) + { + if(!method_exists($this->_math, $methodName)) { + throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist'); + } + return call_user_func_array(array($this->_math, $methodName), $args); + } + + /** + * @param string $extension + * @throws Zend_Crypt_Math_BigInteger_Exception + */ + protected function _loadAdapter($extension = null) + { + if ($extension === null) { + if (extension_loaded('gmp')) { + $extension = 'gmp'; + //} elseif (extension_loaded('big_int')) { + // $extension = 'big_int'; + } else { + $extension = 'bcmath'; + } + } + if($extension == 'gmp' && extension_loaded('gmp')) { + $this->_math = new Zend_Crypt_Math_BigInteger_Gmp(); + //} elseif($extension == 'bigint' && extension_loaded('big_int')) { + // require_once 'Zend/Crypt_Math/BigInteger/Bigint.php'; + // $this->_math = new Zend_Crypt_Math_BigInteger_Bigint(); + } elseif ($extension == 'bcmath' && extension_loaded('bcmath')) { + $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath(); + } else { + throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected'); + } + } + +} diff --git a/library/vendor/Zend/Crypt/Math/BigInteger/Bcmath.php b/library/vendor/Zend/Crypt/Math/BigInteger/Bcmath.php new file mode 100644 index 0000000..2fb4a67 --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/BigInteger/Bcmath.php @@ -0,0 +1,226 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * @see Zend_Crypt_Math_BigInteger_Interface + */ + +/** + * Support for arbitrary precision mathematics in PHP. + * + * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath + * extension. + * + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface +{ + + /** + * Initialise a big integer into an extension specific type. This is not + * applicable to BCMath. + * + * @param string $operand + * @param int $base + * @return string + */ + public function init($operand, $base = 10) + { + return $operand; + } + + /** + * Adds two arbitrary precision numbers + * + * @param string $left_operand + * @param string $right_operand + * @return string + */ + public function add($left_operand, $right_operand) + { + return bcadd($left_operand, $right_operand); + } + + /** + * Subtract one arbitrary precision number from another + * + * @param string $left_operand + * @param string $right_operand + * @return string + */ + public function subtract($left_operand, $right_operand) + { + return bcsub($left_operand, $right_operand); + } + + /** + * Compare two big integers and returns result as an integer where 0 means + * both are identical, 1 that left_operand is larger, or -1 that + * right_operand is larger. + * + * @param string $left_operand + * @param string $right_operand + * @return int + */ + public function compare($left_operand, $right_operand) + { + return bccomp($left_operand, $right_operand); + } + + /** + * Divide two big integers and return result or NULL if the denominator + * is zero. + * + * @param string $left_operand + * @param string $right_operand + * @return string|null + */ + public function divide($left_operand, $right_operand) + { + return bcdiv($left_operand, $right_operand); + } + + /** + * Get modulus of an arbitrary precision number + * + * @param string $left_operand + * @param string $modulus + * @return string + */ + public function modulus($left_operand, $modulus) + { + return bcmod($left_operand, $modulus); + } + + /** + * Multiply two arbitrary precision numbers + * + * @param string $left_operand + * @param string $right_operand + * @return string + */ + public function multiply($left_operand, $right_operand) + { + return bcmul($left_operand, $right_operand); + } + + /** + * Raise an arbitrary precision number to another + * + * @param string $left_operand + * @param string $right_operand + * @return string + */ + public function pow($left_operand, $right_operand) + { + return bcpow($left_operand, $right_operand); + } + + /** + * Raise an arbitrary precision number to another, reduced by a specified + * modulus + * + * @param string $left_operand + * @param string $right_operand + * @param string $modulus + * @return string + */ + public function powmod($left_operand, $right_operand, $modulus) + { + return bcpowmod($left_operand, $right_operand, $modulus); + } + + /** + * Get the square root of an arbitrary precision number + * + * @param string $operand + * @return string + */ + public function sqrt($operand) + { + return bcsqrt($operand); + } + + /** + * @param string $operand + * @return string + */ + public function binaryToInteger($operand) + { + $result = '0'; + while (strlen($operand)) { + $ord = ord(substr($operand, 0, 1)); + $result = bcadd(bcmul($result, 256), $ord); + $operand = substr($operand, 1); + } + return $result; + } + + /** + * @param string $operand + * @return string + */ + public function integerToBinary($operand) + { + $cmp = bccomp($operand, 0); + $return = ''; + if ($cmp == 0) { + return "\0"; + } + while (bccomp($operand, 0) > 0) { + $return = chr(bcmod($operand, 256)) . $return; + $operand = bcdiv($operand, 256); + } + if (ord($return[0]) > 127) { + $return = "\0" . $return; + } + return $return; + } + + /**public function integerToBinary($operand) + { + $return = ''; + while(bccomp($operand, '0')) { + $return .= chr(bcmod($operand, '256')); + $operand = bcdiv($operand, '256'); + } + return $return; + }**/ // Prior version for referenced offset + + /** + * @param string $operand + * @return string + */ + public function hexToDecimal($operand) + { + $return = '0'; + while(strlen($hex)) { + $hex = hexdec(substr($operand, 0, 4)); + $dec = bcadd(bcmul($return, 65536), $hex); + $operand = substr($operand, 4); + } + return $return; + } +} diff --git a/library/vendor/Zend/Crypt/Math/BigInteger/Exception.php b/library/vendor/Zend/Crypt/Math/BigInteger/Exception.php new file mode 100644 index 0000000..c1c865d --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/BigInteger/Exception.php @@ -0,0 +1,35 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * @see Zend_Crypt_Math_Exception + */ + +/** + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Crypt_Math_BigInteger_Exception extends Zend_Crypt_Math_Exception +{ +} diff --git a/library/vendor/Zend/Crypt/Math/BigInteger/Gmp.php b/library/vendor/Zend/Crypt/Math/BigInteger/Gmp.php new file mode 100644 index 0000000..1912be7 --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/BigInteger/Gmp.php @@ -0,0 +1,219 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * @see Zend_Crypt_Math_BigInteger_Interface + */ + +/** + * Support for arbitrary precision mathematics in PHP. + * + * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath + * extension. + * + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface +{ + + /** + * Initialise a big integer into an extension specific type. + * @param string $operand + * @param int $base + * @return string + */ + public function init($operand, $base = 10) + { + return $operand; + } + + /** + * Adds two arbitrary precision numbers + * + * @param resource $left_operand + * @param resource $right_operand + * @return string + */ + public function add($left_operand, $right_operand) + { + $result = gmp_add($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Subtract numbers + * + * @param resource $left_operand + * @param resource $right_operand + * @return string + */ + public function subtract($left_operand, $right_operand) + { + $result = gmp_sub($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Compare two big integers and returns result as an integer where 0 means + * both are identical, 1 that left_operand is larger, or -1 that + * right_operand is larger. + * + * @param resource $left_operand + * @param resource $right_operand + * @return int + */ + public function compare($left_operand, $right_operand) + { + $result = gmp_cmp($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Divide two big integers and return result or NULL if the denominator + * is zero. + * + * @param resource $left_operand + * @param resource $right_operand + * @return string|null + */ + public function divide($left_operand, $right_operand) + { + $result = gmp_div($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Modulo operation + * + * @param resource $left_operand + * @param resource $modulus + * @internal param string $right_operand + * @return string + */ + public function modulus($left_operand, $modulus) + { + $result = gmp_mod($left_operand, $modulus); + return gmp_strval($result); + } + + /** + * Multiply numbers + * + * @param resource $left_operand + * @param resource $right_operand + * @return string + */ + public function multiply($left_operand, $right_operand) + { + $result = gmp_mul($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Raise number into power + * + * @param resource $left_operand + * @param int $right_operand + * @return string + */ + public function pow($left_operand, $right_operand) + { + $result = gmp_pow($left_operand, $right_operand); + return gmp_strval($result); + } + + /** + * Raise number into power with modulo + * + * @param resource $left_operand + * @param resource $right_operand + * @param resource $modulus + * @return string + */ + public function powmod($left_operand, $right_operand, $modulus) + { + $result = gmp_powm($left_operand, $right_operand, $modulus); + return gmp_strval($result); + } + + /** + * Calculate square root + * + * @param $operand + * @return string + */ + public function sqrt($operand) + { + $result = gmp_sqrt($operand); + return gmp_strval($result); + } + + /** + * @param string $operand + * @return string + */ + public function binaryToInteger($operand) + { + $result = '0'; + while (strlen($operand)) { + $ord = ord(substr($operand, 0, 1)); + $result = gmp_add(gmp_mul($result, 256), $ord); + $operand = substr($operand, 1); + } + return gmp_strval($result); + } + + /** + * @param resource $operand GMP number resource + * @return string + */ + public function integerToBinary($operand) + { + $bigInt = gmp_strval($operand, 16); + if (strlen($bigInt) % 2 != 0) { + $bigInt = '0' . $bigInt; + } else if ($bigInt[0] > '7') { + $bigInt = '00' . $bigInt; + } + $return = pack("H*", $bigInt); + return $return; + } + + /** + * @param string $operand + * @return string + */ + public function hexToDecimal($operand) + { + $return = '0'; + while(strlen($hex)) { + $hex = hexdec(substr($operand, 0, 4)); + $dec = gmp_add(gmp_mul($return, 65536), $hex); + $operand = substr($operand, 4); + } + return $return; + } + +} diff --git a/library/vendor/Zend/Crypt/Math/BigInteger/Interface.php b/library/vendor/Zend/Crypt/Math/BigInteger/Interface.php new file mode 100644 index 0000000..9fa9281 --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/BigInteger/Interface.php @@ -0,0 +1,51 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * Support for arbitrary precision mathematics in PHP. + * + * Interface for a wrapper across any PHP extension supporting arbitrary + * precision maths. + * + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +interface Zend_Crypt_Math_BigInteger_Interface +{ + + public function init($operand, $base = 10); + public function add($left_operand, $right_operand); + public function subtract($left_operand, $right_operand); + public function compare($left_operand, $right_operand); + public function divide($left_operand, $right_operand); + public function modulus($left_operand, $modulus); + public function multiply($left_operand, $right_operand); + public function pow($left_operand, $right_operand); + public function powmod($left_operand, $right_operand, $modulus); + public function sqrt($operand); + public function binaryToInteger($operand); + public function integerToBinary($operand); + public function hexToDecimal($operand); + +} diff --git a/library/vendor/Zend/Crypt/Math/Exception.php b/library/vendor/Zend/Crypt/Math/Exception.php new file mode 100644 index 0000000..066da03 --- /dev/null +++ b/library/vendor/Zend/Crypt/Math/Exception.php @@ -0,0 +1,35 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Crypt + * @subpackage Math + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * @see Zend_Crypt_Exception + */ + +/** + * @category Zend + * @package Zend_Crypt + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Crypt_Math_Exception extends Zend_Crypt_Exception +{ +} |