Merge pull request #361 from bantu/namespacify-biginteger

Namespacify BigInteger

* bantu/namespacify-biginteger:
  Correct the remaining case by using instanceof.
  Update users of Math_BigInteger.
  Some more documentation polishing in Math/BigInteger.php.
  Adjust test cases using Math_BigInteger.
  Replace new Math_BigInteger with new static.
  Reference to instances as BigInteger instead of Math_BigInteger.
  Use namespace in example code.
  Remove any include statement.
  Add namespace statement. Rename class.
  Use __construct.
  Add PSR4 namespace to composer.json.
This commit is contained in:
Andreas Fischer 2014-06-04 12:08:36 +02:00
commit 95a99a840b
12 changed files with 309 additions and 359 deletions

View File

@ -66,6 +66,9 @@
"Net": "phpseclib/", "Net": "phpseclib/",
"System": "phpseclib/" "System": "phpseclib/"
}, },
"psr-4": {
"phpseclib\\": "phpseclib/"
},
"files": [ "files": [
"phpseclib/Crypt/Random.php" "phpseclib/Crypt/Random.php"
] ]

2
composer.lock generated
View File

@ -3,7 +3,7 @@
"This file locks the dependencies of your project to a known state", "This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
], ],
"hash": "9550e007a60e2baceebf1185d508dd00", "hash": "1c5f986d03274ed0b7d509768d1bf277",
"packages": [ "packages": [
], ],

View File

@ -54,6 +54,8 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Math\BigInteger;
/**#@+ /**#@+
* @access private * @access private
* @see Crypt_Hash::Crypt_Hash() * @see Crypt_Hash::Crypt_Hash()
@ -579,10 +581,6 @@ class Crypt_Hash
*/ */
function _sha512($m) function _sha512($m)
{ {
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
static $init384, $init512, $k; static $init384, $init512, $k;
if (!isset($k)) { if (!isset($k)) {
@ -597,9 +595,9 @@ class Crypt_Hash
); );
for ($i = 0; $i < 8; $i++) { for ($i = 0; $i < 8; $i++) {
$init384[$i] = new Math_BigInteger($init384[$i], 16); $init384[$i] = new BigInteger($init384[$i], 16);
$init384[$i]->setPrecision(64); $init384[$i]->setPrecision(64);
$init512[$i] = new Math_BigInteger($init512[$i], 16); $init512[$i] = new BigInteger($init512[$i], 16);
$init512[$i]->setPrecision(64); $init512[$i]->setPrecision(64);
} }
@ -629,7 +627,7 @@ class Crypt_Hash
); );
for ($i = 0; $i < 80; $i++) { for ($i = 0; $i < 80; $i++) {
$k[$i] = new Math_BigInteger($k[$i], 16); $k[$i] = new BigInteger($k[$i], 16);
} }
} }
@ -648,7 +646,7 @@ class Crypt_Hash
foreach ($chunks as $chunk) { foreach ($chunks as $chunk) {
$w = array(); $w = array();
for ($i = 0; $i < 16; $i++) { for ($i = 0; $i < 16; $i++) {
$temp = new Math_BigInteger($this->_string_shift($chunk, 8), 256); $temp = new BigInteger($this->_string_shift($chunk, 8), 256);
$temp->setPrecision(64); $temp->setPrecision(64);
$w[] = $temp; $w[] = $temp;
} }
@ -802,7 +800,7 @@ class Crypt_Hash
* Add * Add
* *
* _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the * _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the
* possibility of overflow exists, care has to be taken. Math_BigInteger() could be used but this should be faster. * possibility of overflow exists, care has to be taken. BigInteger could be used but this should be faster.
* *
* @param Integer $... * @param Integer $...
* @return Integer * @return Integer

View File

@ -67,6 +67,8 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Math\BigInteger;
/** /**
* Include Crypt_Random * Include Crypt_Random
*/ */
@ -201,7 +203,7 @@ define('CRYPT_RSA_PRIVATE_FORMAT_XML', 2);
/** /**
* Raw public key * Raw public key
* *
* An array containing two Math_BigInteger objects. * An array containing two \phpseclib\Math\BigInteger objects.
* *
* The exponent can be indexed with any of the following: * The exponent can be indexed with any of the following:
* *
@ -280,7 +282,7 @@ class Crypt_RSA
/** /**
* Modulus (ie. n) * Modulus (ie. n)
* *
* @var Math_BigInteger * @var \phpseclib\Math\BigInteger
* @access private * @access private
*/ */
var $modulus; var $modulus;
@ -288,7 +290,7 @@ class Crypt_RSA
/** /**
* Modulus length * Modulus length
* *
* @var Math_BigInteger * @var \phpseclib\Math\BigInteger
* @access private * @access private
*/ */
var $k; var $k;
@ -296,7 +298,7 @@ class Crypt_RSA
/** /**
* Exponent (ie. e or d) * Exponent (ie. e or d)
* *
* @var Math_BigInteger * @var \phpseclib\Math\BigInteger
* @access private * @access private
*/ */
var $exponent; var $exponent;
@ -459,10 +461,6 @@ class Crypt_RSA
*/ */
function Crypt_RSA() function Crypt_RSA()
{ {
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
$this->configFile = CRYPT_RSA_OPENSSL_CONFIG; $this->configFile = CRYPT_RSA_OPENSSL_CONFIG;
if ( !defined('CRYPT_RSA_MODE') ) { if ( !defined('CRYPT_RSA_MODE') ) {
@ -507,8 +505,8 @@ class Crypt_RSA
} }
} }
$this->zero = new Math_BigInteger(); $this->zero = new BigInteger();
$this->one = new Math_BigInteger(1); $this->one = new BigInteger(1);
$this->hash = new Crypt_Hash('sha1'); $this->hash = new Crypt_Hash('sha1');
$this->hLen = $this->hash->getLength(); $this->hLen = $this->hash->getLength();
@ -529,7 +527,7 @@ class Crypt_RSA
* @access public * @access public
* @param optional Integer $bits * @param optional Integer $bits
* @param optional Integer $timeout * @param optional Integer $timeout
* @param optional Math_BigInteger $p * @param optional array $p
*/ */
function createKey($bits = 1024, $timeout = false, $partial = array()) function createKey($bits = 1024, $timeout = false, $partial = array())
{ {
@ -573,7 +571,7 @@ class Crypt_RSA
static $e; static $e;
if (!isset($e)) { if (!isset($e)) {
$e = new Math_BigInteger(CRYPT_RSA_EXPONENT); $e = new BigInteger(CRYPT_RSA_EXPONENT);
} }
extract($this->_generateMinMax($bits)); extract($this->_generateMinMax($bits));
@ -589,7 +587,7 @@ class Crypt_RSA
$finalMax = $max; $finalMax = $max;
extract($this->_generateMinMax($temp)); extract($this->_generateMinMax($temp));
$generator = new Math_BigInteger(); $generator = new BigInteger();
$n = $this->one->copy(); $n = $this->one->copy();
if (!empty($partial)) { if (!empty($partial)) {
@ -1100,10 +1098,10 @@ class Crypt_RSA
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$temp = $this->_string_shift($key, $length); $temp = $this->_string_shift($key, $length);
if (strlen($temp) != 1 || ord($temp) > 2) { if (strlen($temp) != 1 || ord($temp) > 2) {
$components['modulus'] = new Math_BigInteger($temp, 256); $components['modulus'] = new BigInteger($temp, 256);
$this->_string_shift($key); // skip over CRYPT_RSA_ASN1_INTEGER $this->_string_shift($key); // skip over CRYPT_RSA_ASN1_INTEGER
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components[$type == CRYPT_RSA_PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components[$type == CRYPT_RSA_PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
return $components; return $components;
} }
@ -1111,28 +1109,28 @@ class Crypt_RSA
return false; return false;
} }
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['modulus'] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['modulus'] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['publicExponent'] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['publicExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['privateExponent'] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['primes'] = array(1 => new Math_BigInteger($this->_string_shift($key, $length), 256)); $components['primes'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['primes'][] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['exponents'] = array(1 => new Math_BigInteger($this->_string_shift($key, $length), 256)); $components['exponents'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['exponents'][] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['coefficients'] = array(2 => new Math_BigInteger($this->_string_shift($key, $length), 256)); $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($key, $length), 256));
if (!empty($key)) { if (!empty($key)) {
if (ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE) { if (ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE) {
@ -1146,13 +1144,13 @@ class Crypt_RSA
$this->_decodeLength($key); $this->_decodeLength($key);
$key = substr($key, 1); $key = substr($key, 1);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['primes'][] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['exponents'][] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
$this->_string_shift($key); $this->_string_shift($key);
$length = $this->_decodeLength($key); $length = $this->_decodeLength($key);
$components['coefficients'][] = new Math_BigInteger($this->_string_shift($key, $length), 256); $components['coefficients'][] = new BigInteger($this->_string_shift($key, $length), 256);
} }
} }
@ -1173,19 +1171,19 @@ class Crypt_RSA
return false; return false;
} }
extract(unpack('Nlength', $this->_string_shift($key, 4))); extract(unpack('Nlength', $this->_string_shift($key, 4)));
$publicExponent = new Math_BigInteger($this->_string_shift($key, $length), -256); $publicExponent = new BigInteger($this->_string_shift($key, $length), -256);
if (strlen($key) <= 4) { if (strlen($key) <= 4) {
return false; return false;
} }
extract(unpack('Nlength', $this->_string_shift($key, 4))); extract(unpack('Nlength', $this->_string_shift($key, 4)));
$modulus = new Math_BigInteger($this->_string_shift($key, $length), -256); $modulus = new BigInteger($this->_string_shift($key, $length), -256);
if ($cleanup && strlen($key)) { if ($cleanup && strlen($key)) {
if (strlen($key) <= 4) { if (strlen($key) <= 4) {
return false; return false;
} }
extract(unpack('Nlength', $this->_string_shift($key, 4))); extract(unpack('Nlength', $this->_string_shift($key, 4)));
$realModulus = new Math_BigInteger($this->_string_shift($key, $length), -256); $realModulus = new BigInteger($this->_string_shift($key, $length), -256);
return strlen($key) ? false : array( return strlen($key) ? false : array(
'modulus' => $realModulus, 'modulus' => $realModulus,
'publicExponent' => $modulus, 'publicExponent' => $modulus,
@ -1229,9 +1227,9 @@ class Crypt_RSA
$public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength)))); $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
$public = substr($public, 11); $public = substr($public, 11);
extract(unpack('Nlength', $this->_string_shift($public, 4))); extract(unpack('Nlength', $this->_string_shift($public, 4)));
$components['publicExponent'] = new Math_BigInteger($this->_string_shift($public, $length), -256); $components['publicExponent'] = new BigInteger($this->_string_shift($public, $length), -256);
extract(unpack('Nlength', $this->_string_shift($public, 4))); extract(unpack('Nlength', $this->_string_shift($public, 4)));
$components['modulus'] = new Math_BigInteger($this->_string_shift($public, $length), -256); $components['modulus'] = new BigInteger($this->_string_shift($public, $length), -256);
$privateLength = trim(preg_replace('#Private-Lines: (\d+)#', '$1', $key[$publicLength + 4])); $privateLength = trim(preg_replace('#Private-Lines: (\d+)#', '$1', $key[$publicLength + 4]));
$private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength)))); $private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
@ -1264,17 +1262,17 @@ class Crypt_RSA
if (strlen($private) < $length) { if (strlen($private) < $length) {
return false; return false;
} }
$components['privateExponent'] = new Math_BigInteger($this->_string_shift($private, $length), -256); $components['privateExponent'] = new BigInteger($this->_string_shift($private, $length), -256);
extract(unpack('Nlength', $this->_string_shift($private, 4))); extract(unpack('Nlength', $this->_string_shift($private, 4)));
if (strlen($private) < $length) { if (strlen($private) < $length) {
return false; return false;
} }
$components['primes'] = array(1 => new Math_BigInteger($this->_string_shift($private, $length), -256)); $components['primes'] = array(1 => new BigInteger($this->_string_shift($private, $length), -256));
extract(unpack('Nlength', $this->_string_shift($private, 4))); extract(unpack('Nlength', $this->_string_shift($private, 4)));
if (strlen($private) < $length) { if (strlen($private) < $length) {
return false; return false;
} }
$components['primes'][] = new Math_BigInteger($this->_string_shift($private, $length), -256); $components['primes'][] = new BigInteger($this->_string_shift($private, $length), -256);
$temp = $components['primes'][1]->subtract($this->one); $temp = $components['primes'][1]->subtract($this->one);
$components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp)); $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp));
@ -1285,7 +1283,7 @@ class Crypt_RSA
if (strlen($private) < $length) { if (strlen($private) < $length) {
return false; return false;
} }
$components['coefficients'] = array(2 => new Math_BigInteger($this->_string_shift($private, $length), -256)); $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($private, $length), -256));
return $components; return $components;
} }
@ -1357,7 +1355,7 @@ class Crypt_RSA
function _stop_element_handler($parser, $name) function _stop_element_handler($parser, $name)
{ {
if (isset($this->current)) { if (isset($this->current)) {
$this->current = new Math_BigInteger(base64_decode($this->current), 256); $this->current = new BigInteger(base64_decode($this->current), 256);
unset($this->current); unset($this->current);
} }
} }
@ -1729,8 +1727,8 @@ class Crypt_RSA
} }
return array( return array(
'min' => new Math_BigInteger($min, 256), 'min' => new BigInteger($min, 256),
'max' => new Math_BigInteger($max, 256) 'max' => new BigInteger($max, 256)
); );
} }
@ -1894,7 +1892,7 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}. * See {@link http://tools.ietf.org/html/rfc3447#section-4.1 RFC3447#section-4.1}.
* *
* @access private * @access private
* @param Math_BigInteger $x * @param \phpseclib\Math\BigInteger $x
* @param Integer $xLen * @param Integer $xLen
* @return String * @return String
*/ */
@ -1915,11 +1913,11 @@ class Crypt_RSA
* *
* @access private * @access private
* @param String $x * @param String $x
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _os2ip($x) function _os2ip($x)
{ {
return new Math_BigInteger($x, 256); return new BigInteger($x, 256);
} }
/** /**
@ -1928,8 +1926,8 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}.
* *
* @access private * @access private
* @param Math_BigInteger $x * @param \phpseclib\Math\BigInteger $x
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _exponentiate($x) function _exponentiate($x)
{ {
@ -1969,7 +1967,7 @@ class Crypt_RSA
} }
} }
$one = new Math_BigInteger(1); $one = new BigInteger(1);
$r = $one->random($one, $smallest->subtract($one)); $r = $one->random($one, $smallest->subtract($one));
@ -2006,10 +2004,10 @@ class Crypt_RSA
* Returns $x->modPow($this->exponents[$i], $this->primes[$i]) * Returns $x->modPow($this->exponents[$i], $this->primes[$i])
* *
* @access private * @access private
* @param Math_BigInteger $x * @param \phpseclib\Math\BigInteger $x
* @param Math_BigInteger $r * @param \phpseclib\Math\BigInteger $r
* @param Integer $i * @param Integer $i
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _blind($x, $r, $i) function _blind($x, $r, $i)
{ {
@ -2057,8 +2055,8 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.1}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.1}.
* *
* @access private * @access private
* @param Math_BigInteger $m * @param \phpseclib\Math\BigInteger $m
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsaep($m) function _rsaep($m)
{ {
@ -2075,8 +2073,8 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}.
* *
* @access private * @access private
* @param Math_BigInteger $c * @param \phpseclib\Math\BigInteger $c
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsadp($c) function _rsadp($c)
{ {
@ -2093,8 +2091,8 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}.
* *
* @access private * @access private
* @param Math_BigInteger $m * @param \phpseclib\Math\BigInteger $m
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsasp1($m) function _rsasp1($m)
{ {
@ -2111,8 +2109,8 @@ class Crypt_RSA
* See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}.
* *
* @access private * @access private
* @param Math_BigInteger $s * @param \phpseclib\Math\BigInteger $s
* @return Math_BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsavp1($s) function _rsavp1($s)
{ {

View File

@ -39,6 +39,8 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Math\BigInteger;
/**#@+ /**#@+
* Tag Classes * Tag Classes
* *
@ -240,22 +242,6 @@ class File_ASN1
FILE_ASN1_TYPE_VISIBLE_STRING => 1, FILE_ASN1_TYPE_VISIBLE_STRING => 1,
); );
/**
* Default Constructor.
*
* @access public
*/
function File_ASN1()
{
static $static_init = null;
if (!$static_init) {
$static_init = true;
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
}
}
/** /**
* Parse BER-encoding * Parse BER-encoding
* *
@ -377,7 +363,7 @@ class File_ASN1
break; break;
case FILE_ASN1_TYPE_INTEGER: case FILE_ASN1_TYPE_INTEGER:
case FILE_ASN1_TYPE_ENUMERATED: case FILE_ASN1_TYPE_ENUMERATED:
$current['content'] = new Math_BigInteger($content, -256); $current['content'] = new BigInteger($content, -256);
break; break;
case FILE_ASN1_TYPE_REAL: // not currently supported case FILE_ASN1_TYPE_REAL: // not currently supported
return false; return false;
@ -773,7 +759,7 @@ class File_ASN1
case FILE_ASN1_TYPE_ENUMERATED: case FILE_ASN1_TYPE_ENUMERATED:
$temp = $decoded['content']; $temp = $decoded['content'];
if (isset($mapping['implicit'])) { if (isset($mapping['implicit'])) {
$temp = new Math_BigInteger($decoded['content'], -256); $temp = new BigInteger($decoded['content'], -256);
} }
if (isset($mapping['mapping'])) { if (isset($mapping['mapping'])) {
$temp = (int) $temp->toString(); $temp = (int) $temp->toString();
@ -951,7 +937,7 @@ class File_ASN1
case FILE_ASN1_TYPE_ENUMERATED: case FILE_ASN1_TYPE_ENUMERATED:
if (!isset($mapping['mapping'])) { if (!isset($mapping['mapping'])) {
if (is_numeric($source)) { if (is_numeric($source)) {
$source = new Math_BigInteger($source); $source = new BigInteger($source);
} }
$value = $source->toBytes(true); $value = $source->toBytes(true);
} else { } else {
@ -959,7 +945,7 @@ class File_ASN1
if ($value === false) { if ($value === false) {
return false; return false;
} }
$value = new Math_BigInteger($value); $value = new BigInteger($value);
$value = $value->toBytes(true); $value = $value->toBytes(true);
} }
if (!strlen($value)) { if (!strlen($value)) {
@ -1044,7 +1030,7 @@ class File_ASN1
case !isset($source): case !isset($source):
return $this->_encode_der(null, array('type' => FILE_ASN1_TYPE_NULL) + $mapping, null, $special); return $this->_encode_der(null, array('type' => FILE_ASN1_TYPE_NULL) + $mapping, null, $special);
case is_int($source): case is_int($source):
case is_object($source) && strtolower(get_class($source)) == 'math_biginteger': case $source instanceof \phpseclib\Math\BigInteger:
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping, null, $special); return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping, null, $special);
case is_float($source): case is_float($source):
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping, null, $special); return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping, null, $special);

View File

@ -305,10 +305,6 @@ class File_X509
*/ */
function File_X509() function File_X509()
{ {
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
// Explicitly Tagged Module, 1988 Syntax // Explicitly Tagged Module, 1988 Syntax
// http://tools.ietf.org/html/rfc5280#appendix-A.1 // http://tools.ietf.org/html/rfc5280#appendix-A.1
@ -971,7 +967,7 @@ class File_X509
'constant' => 0, 'constant' => 0,
'optional' => true, 'optional' => true,
'implicit' => true, 'implicit' => true,
'default' => new Math_BigInteger(0) 'default' => new BigInteger(0)
) + $BaseDistance, ) + $BaseDistance,
'maximum' => array( 'maximum' => array(
'constant' => 1, 'constant' => 1,
@ -3197,7 +3193,7 @@ class File_X509
$startDate = !empty($this->startDate) ? $this->startDate : @date('D, d M Y H:i:s O'); $startDate = !empty($this->startDate) ? $this->startDate : @date('D, d M Y H:i:s O');
$endDate = !empty($this->endDate) ? $this->endDate : @date('D, d M Y H:i:s O', strtotime('+1 year')); $endDate = !empty($this->endDate) ? $this->endDate : @date('D, d M Y H:i:s O', strtotime('+1 year'));
$serialNumber = !empty($this->serialNumber) ? $this->serialNumber : new Math_BigInteger(); $serialNumber = !empty($this->serialNumber) ? $this->serialNumber : new BigInteger();
$this->currentCert = array( $this->currentCert = array(
'tbsCertificate' => 'tbsCertificate' =>
@ -3421,7 +3417,7 @@ class File_X509
$crlNumber = $this->serialNumber; $crlNumber = $this->serialNumber;
} else { } else {
$crlNumber = $this->getExtension('id-ce-cRLNumber'); $crlNumber = $this->getExtension('id-ce-cRLNumber');
$crlNumber = $crlNumber !== false ? $crlNumber->add(new Math_BigInteger(1)) : null; $crlNumber = $crlNumber !== false ? $crlNumber->add(new BigInteger(1)) : null;
} }
$this->removeExtension('id-ce-authorityKeyIdentifier'); $this->removeExtension('id-ce-authorityKeyIdentifier');
@ -3572,7 +3568,7 @@ class File_X509
*/ */
function setSerialNumber($serial, $base = -256) function setSerialNumber($serial, $base = -256)
{ {
$this->serialNumber = new Math_BigInteger($serial, $base); $this->serialNumber = new BigInteger($serial, $base);
} }
/** /**
@ -4219,7 +4215,7 @@ class File_X509
*/ */
function _revokedCertificate(&$rclist, $serial, $create = false) function _revokedCertificate(&$rclist, $serial, $create = false)
{ {
$serial = new Math_BigInteger($serial); $serial = new BigInteger($serial);
foreach ($rclist as $i => $rc) { foreach ($rclist as $i => $rc) {
if (!($serial->compare($rc['userCertificate']))) { if (!($serial->compare($rc['userCertificate']))) {

File diff suppressed because it is too large Load Diff

View File

@ -2267,7 +2267,7 @@ class Net_SFTP extends Net_SSH2
switch ($flags & $key) { switch ($flags & $key) {
case NET_SFTP_ATTR_SIZE: // 0x00000001 case NET_SFTP_ATTR_SIZE: // 0x00000001
// size is represented by a 64-bit integer, so we perhaps ought to be doing the following: // size is represented by a 64-bit integer, so we perhaps ought to be doing the following:
// $attr['size'] = new Math_BigInteger($this->_string_shift($response, 8), 256); // $attr['size'] = new \phpseclib\Math\BigInteger($this->_string_shift($response, 8), 256);
// of course, you shouldn't be using Net_SFTP to transfer files that are in excess of 4GB // of course, you shouldn't be using Net_SFTP to transfer files that are in excess of 4GB
// (0xFFFFFFFF bytes), anyway. as such, we'll just represent all file sizes that are bigger than // (0xFFFFFFFF bytes), anyway. as such, we'll just represent all file sizes that are bigger than
// 4GB as being 4GB. // 4GB as being 4GB.

View File

@ -64,6 +64,8 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Math\BigInteger;
/**#@+ /**#@+
* Encryption Methods * Encryption Methods
* *
@ -517,10 +519,6 @@ class Net_SSH1
*/ */
function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES) function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
{ {
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
// Include Crypt_Random // Include Crypt_Random
// the class_exists() will only be called if the crypt_random_string function hasn't been defined and // the class_exists() will only be called if the crypt_random_string function hasn't been defined and
// will trigger a call to __autoload() if you're wanting to auto-load classes // will trigger a call to __autoload() if you're wanting to auto-load classes
@ -600,21 +598,21 @@ class Net_SSH1
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
$server_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $server_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
$this->server_key_public_exponent = $server_key_public_exponent; $this->server_key_public_exponent = $server_key_public_exponent;
$temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
$server_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $server_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
$this->server_key_public_modulus = $server_key_public_modulus; $this->server_key_public_modulus = $server_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
$host_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $host_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
$this->host_key_public_exponent = $host_key_public_exponent; $this->host_key_public_exponent = $host_key_public_exponent;
$temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
$host_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $host_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
$this->host_key_public_modulus = $host_key_public_modulus; $this->host_key_public_modulus = $host_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
@ -1377,7 +1375,7 @@ class Net_SSH1
} }
$temp = chr(0) . chr(2) . $random . chr(0) . $m; $temp = chr(0) . chr(2) . $random . chr(0) . $m;
$m = new Math_BigInteger($temp, 256); $m = new BigInteger($temp, 256);
$m = $m->modPow($key[0], $key[1]); $m = $m->modPow($key[0], $key[1]);
return $m->toBytes(); return $m->toBytes();

View File

@ -66,6 +66,9 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
// Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
use \phpseclib\Math\BigInteger;
/**#@+ /**#@+
* Execution Bitmap Masks * Execution Bitmap Masks
* *
@ -830,12 +833,6 @@ class Net_SSH2
*/ */
function Net_SSH2($host, $port = 22, $timeout = 10) function Net_SSH2($host, $port = 22, $timeout = 10)
{ {
// Include Math_BigInteger
// Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
if (!class_exists('Math_BigInteger')) {
include_once 'Math/BigInteger.php';
}
if (!function_exists('crypt_random_string')) { if (!function_exists('crypt_random_string')) {
include_once 'Crypt/Random.php'; include_once 'Crypt/Random.php';
} }
@ -1353,8 +1350,8 @@ class Net_SSH2
// For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1 // For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1
// the generator field element is 2 (decimal) and the hash function is sha1. // the generator field element is 2 (decimal) and the hash function is sha1.
$g = new Math_BigInteger(2); $g = new BigInteger(2);
$prime = new Math_BigInteger($prime, 16); $prime = new BigInteger($prime, 16);
$kexHash = new Crypt_Hash('sha1'); $kexHash = new Crypt_Hash('sha1');
//$q = $p->bitwise_rightShift(1); //$q = $p->bitwise_rightShift(1);
@ -1365,7 +1362,7 @@ class Net_SSH2
[VAN-OORSCHOT]. [VAN-OORSCHOT].
-- http://tools.ietf.org/html/rfc4419#section-6.2 */ -- http://tools.ietf.org/html/rfc4419#section-6.2 */
$one = new Math_BigInteger(1); $one = new BigInteger(1);
$keyLength = min($keyLength, $kexHash->getLength()); $keyLength = min($keyLength, $kexHash->getLength());
$max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength $max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength
$max = $max->subtract($one); $max = $max->subtract($one);
@ -1401,7 +1398,7 @@ class Net_SSH2
$temp = unpack('Nlength', $this->_string_shift($response, 4)); $temp = unpack('Nlength', $this->_string_shift($response, 4));
$fBytes = $this->_string_shift($response, $temp['length']); $fBytes = $this->_string_shift($response, $temp['length']);
$f = new Math_BigInteger($fBytes, -256); $f = new BigInteger($fBytes, -256);
$temp = unpack('Nlength', $this->_string_shift($response, 4)); $temp = unpack('Nlength', $this->_string_shift($response, 4));
$this->signature = $this->_string_shift($response, $temp['length']); $this->signature = $this->_string_shift($response, $temp['length']);
@ -3641,19 +3638,19 @@ class Net_SSH2
switch ($this->signature_format) { switch ($this->signature_format) {
case 'ssh-dss': case 'ssh-dss':
$zero = new Math_BigInteger(); $zero = new BigInteger();
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$p = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $p = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$q = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $q = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$g = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $g = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$y = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $y = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
/* The value for 'dss_signature_blob' is encoded as a string containing /* The value for 'dss_signature_blob' is encoded as a string containing
r, followed by s (which are 160-bit integers, without lengths or r, followed by s (which are 160-bit integers, without lengths or
@ -3664,8 +3661,8 @@ class Net_SSH2
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
} }
$r = new Math_BigInteger($this->_string_shift($signature, 20), 256); $r = new BigInteger($this->_string_shift($signature, 20), 256);
$s = new Math_BigInteger($this->_string_shift($signature, 20), 256); $s = new BigInteger($this->_string_shift($signature, 20), 256);
switch (true) { switch (true) {
case $r->equals($zero): case $r->equals($zero):
@ -3678,7 +3675,7 @@ class Net_SSH2
$w = $s->modInverse($q); $w = $s->modInverse($q);
$u1 = $w->multiply(new Math_BigInteger(sha1($this->exchange_hash), 16)); $u1 = $w->multiply(new BigInteger(sha1($this->exchange_hash), 16));
list(, $u1) = $u1->divide($q); list(, $u1) = $u1->divide($q);
$u2 = $w->multiply($r); $u2 = $w->multiply($r);
@ -3699,10 +3696,10 @@ class Net_SSH2
break; break;
case 'ssh-rsa': case 'ssh-rsa':
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$e = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $e = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4)); $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
$n = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256); $n = new BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
$nLength = $temp['length']; $nLength = $temp['length'];
/* /*
@ -3723,7 +3720,7 @@ class Net_SSH2
*/ */
$temp = unpack('Nlength', $this->_string_shift($signature, 4)); $temp = unpack('Nlength', $this->_string_shift($signature, 4));
$s = new Math_BigInteger($this->_string_shift($signature, $temp['length']), 256); $s = new BigInteger($this->_string_shift($signature, $temp['length']), 256);
// validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the // validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the
// following URL: // following URL:
@ -3731,7 +3728,7 @@ class Net_SSH2
// also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source. // also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source.
if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) { if ($s->compare(new BigInteger()) < 0 || $s->compare($n->subtract(new BigInteger(1))) > 0) {
user_error('Invalid signature'); user_error('Invalid signature');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED); return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
} }

View File

@ -17,8 +17,8 @@ class Math_BigInteger_InternalTest extends Unit_Math_BigInteger_TestCase
public function testInternalRepresentation() public function testInternalRepresentation()
{ {
$x = new Math_BigInteger('FFFFFFFFFFFFFFFFC90FDA', 16); $x = new \phpseclib\Math\BigInteger('FFFFFFFFFFFFFFFFC90FDA', 16);
$y = new Math_BigInteger("$x"); $y = new \phpseclib\Math\BigInteger("$x");
$this->assertSame($x->value, $y->value); $this->assertSame($x->value, $y->value);
} }
} }

View File

@ -18,7 +18,7 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
public function getInstance($x = 0, $base = 10) public function getInstance($x = 0, $base = 10)
{ {
return new Math_BigInteger($x, $base); return new \phpseclib\Math\BigInteger($x, $base);
} }
public function testConstructorBase2() public function testConstructorBase2()