From c70702afbba8765376efb081530e652017824100 Mon Sep 17 00:00:00 2001 From: Clint Nelissen Date: Tue, 2 Dec 2014 08:41:30 -0800 Subject: [PATCH] Namespaced Crypt\Random --- phpseclib/Crypt/RSA.php | 17 +++++++++-------- phpseclib/Crypt/Random.php | 8 +++++--- phpseclib/File/X509.php | 2 +- phpseclib/Math/BigInteger.php | 8 ++++---- phpseclib/Net/SSH1.php | 11 ++++++----- phpseclib/Net/SSH2.php | 7 ++++--- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 56460b50..782c9b4b 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -67,12 +67,13 @@ * @link http://phpseclib.sourceforge.net */ +use \phpseclib\Crypt\Random; use \phpseclib\Math\BigInteger; /** - * Include Crypt_Random + * Include Crypt\Random */ -if (!class_exists('Crypt_Random')) { +if (!class_exists('phpseclib\Crypt\Random')) { include_once 'Random.php'; } @@ -794,7 +795,7 @@ class Crypt_RSA $source.= pack('Na*', strlen($private), $private); $hashkey = 'putty-private-key-file-mac-key'; } else { - $private.= Crypt_Random::crypt_random_string(16 - (strlen($private) & 15)); + $private.= Random::crypt_random_string(16 - (strlen($private) & 15)); $source.= pack('Na*', strlen($private), $private); if (!class_exists('Crypt_AES')) { include_once 'Crypt/AES.php'; @@ -860,7 +861,7 @@ class Crypt_RSA ); $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey); if (!empty($this->password) || is_string($this->password)) { - $salt = Crypt_Random::crypt_random_string(8); + $salt = Random::crypt_random_string(8); $iterationCount = 2048; if (!class_exists('Crypt_DES')) { @@ -900,7 +901,7 @@ class Crypt_RSA } if (!empty($this->password) || is_string($this->password)) { - $iv = Crypt_Random::crypt_random_string(8); + $iv = Random::crypt_random_string(8); $symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key $symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8); if (!class_exists('Crypt_TripleDES')) { @@ -2302,7 +2303,7 @@ class Crypt_RSA $lHash = $this->hash->hash($l); $ps = str_repeat(chr(0), $this->k - $mLen - 2 * $this->hLen - 2); $db = $lHash . $ps . chr(1) . $m; - $seed = Crypt_Random::crypt_random_string($this->hLen); + $seed = Random::crypt_random_string($this->hLen); $dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1); $maskedDB = $db ^ $dbMask; $seedMask = $this->_mgf1($maskedDB, $this->hLen); @@ -2420,7 +2421,7 @@ class Crypt_RSA $psLen = $this->k - $mLen - 3; $ps = ''; while (strlen($ps) != $psLen) { - $temp = Crypt_Random::crypt_random_string($psLen - strlen($ps)); + $temp = Random::crypt_random_string($psLen - strlen($ps)); $temp = str_replace("\x00", '', $temp); $ps.= $temp; } @@ -2526,7 +2527,7 @@ class Crypt_RSA return false; } - $salt = Crypt_Random::crypt_random_string($sLen); + $salt = Random::crypt_random_string($sLen); $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; $h = $this->hash->hash($m2); $ps = str_repeat(chr(0), $emLen - $sLen - $this->hLen - 2); diff --git a/phpseclib/Crypt/Random.php b/phpseclib/Crypt/Random.php index 70035977..be229721 100644 --- a/phpseclib/Crypt/Random.php +++ b/phpseclib/Crypt/Random.php @@ -36,21 +36,23 @@ * THE SOFTWARE. * * @category Crypt - * @package Crypt_Random + * @package Random * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link http://phpseclib.sourceforge.net */ +namespace phpseclib\Crypt; + /** * Pure-PHP Random Number Generator * - * @package Crypt_Random + * @package Random * @author Jim Wigginton * @access public */ -class Crypt_Random +class Random { /** * Generate a random string. diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index e108c373..bd2187d0 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -3475,7 +3475,7 @@ class File_X509 // "A challenge string that is submitted along with the public key. Defaults to an empty string if not specified." // both Firefox and OpenSSL ("openssl spkac -key private.key") behave this way // we could alternatively do this instead if we ignored the specs: - // Crypt_Random::crypt_random_string(8) & str_repeat("\x7F", 8) + // Random::crypt_random_string(8) & str_repeat("\x7F", 8) 'challenge' => !empty($this->challenge) ? $this->challenge : '' ), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index f3ea5cb6..9b5e658f 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -68,7 +68,7 @@ namespace phpseclib\Math; -use Crypt_Random; +use \phpseclib\Crypt\Random; /**#@+ * Reduction constants @@ -3023,7 +3023,7 @@ class BigInteger /** * Generates a random BigInteger * - * Byte length is equal to $length. Uses Crypt_Random if it's loaded and mt_rand if it's not. + * Byte length is equal to $length. Uses Crypt\Random if it's loaded and mt_rand if it's not. * * @param Integer $length * @return \phpseclib\Math\BigInteger @@ -3031,8 +3031,8 @@ class BigInteger */ function _random_number_helper($size) { - if (class_exists('Crypt_Random')) { - $random = Crypt_Random::crypt_random_string($size); + if (class_exists('phpseclib\Crypt\Random')) { + $random = Random::crypt_random_string($size); } else { $random = ''; diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index bb5f3cc1..111e2a64 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -64,6 +64,7 @@ * @link http://phpseclib.sourceforge.net */ +use \phpseclib\Crypt\Random; use \phpseclib\Math\BigInteger; /**#@+ @@ -519,8 +520,8 @@ class Net_SSH1 */ function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES) { - // Include Crypt_Random - if (!class_exists('Crypt_Random')) { + // Include Crypt\Random + if (!class_exists('phpseclib\Crypt\Random')) { include_once 'Crypt/Random.php'; } @@ -631,7 +632,7 @@ class Net_SSH1 $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie)); - $session_key = Crypt_Random::crypt_random_string(32); + $session_key = Random::crypt_random_string(32); $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0)); if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) { @@ -1179,7 +1180,7 @@ class Net_SSH1 $length = strlen($data) + 4; - $padding = Crypt_Random::crypt_random_string(8 - ($length & 7)); + $padding = Random::crypt_random_string(8 - ($length & 7)); $orig = $data; $data = $padding . $data; @@ -1365,7 +1366,7 @@ class Net_SSH1 $length = strlen($modulus) - strlen($m) - 3; $random = ''; while (strlen($random) != $length) { - $block = Crypt_Random::crypt_random_string($length - strlen($random)); + $block = Random::crypt_random_string($length - strlen($random)); $block = str_replace("\x00", '', $block); $random.= $block; } diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 84414207..3cf0da81 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -66,6 +66,7 @@ * @link http://phpseclib.sourceforge.net */ +use \phpseclib\Crypt\Random; // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. use \phpseclib\Math\BigInteger; @@ -855,7 +856,7 @@ class Net_SSH2 */ function __construct($host, $port = 22, $timeout = 10) { - if (!class_exists('Crypt_Random')) { + if (!class_exists('phpseclib\Crypt\Random')) { include_once 'Crypt/Random.php'; } @@ -1202,7 +1203,7 @@ class Net_SSH2 $compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms); } - $client_cookie = Crypt_Random::crypt_random_string(16); + $client_cookie = Random::crypt_random_string(16); $response = $kexinit_payload_server; $this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT) @@ -3154,7 +3155,7 @@ class Net_SSH2 $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size; // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length $padding_length = $packet_length - strlen($data) - 5; - $padding = Crypt_Random::crypt_random_string($padding_length); + $padding = Random::crypt_random_string($padding_length); // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);