From b242259d17b0f56614a3fa7736b7834d1129c3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-J=C3=BCrgen=20Petrich?= Date: Mon, 13 May 2013 12:41:52 +0700 Subject: [PATCH] optimizations --- phpseclib/Crypt/RSA.php | 15 ++-- phpseclib/File/ASN1.php | 18 +++-- phpseclib/File/X509.php | 4 +- phpseclib/Net/SFTP.php | 2 +- phpseclib/Net/SFTP/Stream.php | 23 ++++--- phpseclib/Net/SSH1.php | 68 ++++++------------ phpseclib/Net/SSH2.php | 126 ++++++++++++++++++---------------- 7 files changed, 125 insertions(+), 131 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 4df0b918..8eedc0bb 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -69,13 +69,6 @@ * @link http://phpseclib.sourceforge.net */ -/** - * Include Math_BigInteger - */ -if (!class_exists('Math_BigInteger')) { - require_once('Math/BigInteger.php'); -} - /** * Include Crypt_Random */ @@ -84,14 +77,14 @@ if (!class_exists('Math_BigInteger')) { // call function_exists() a second time to stop the require_once from being called outside // of the auto loader if (!function_exists('crypt_random_string')) { - require_once('Crypt/Random.php'); + require_once('Random.php'); } /** * Include Crypt_Hash */ if (!class_exists('Crypt_Hash')) { - require_once('Crypt/Hash.php'); + require_once('Hash.php'); } /**#@+ @@ -469,6 +462,10 @@ class Crypt_RSA { */ function Crypt_RSA() { + if (!class_exists('Math_BigInteger')) { + require_once('Math/BigInteger.php'); + } + $this->configFile = CRYPT_RSA_OPENSSL_CONFIG; if ( !defined('CRYPT_RSA_MODE') ) { diff --git a/phpseclib/File/ASN1.php b/phpseclib/File/ASN1.php index 80f26fff..69a2ee91 100644 --- a/phpseclib/File/ASN1.php +++ b/phpseclib/File/ASN1.php @@ -41,13 +41,6 @@ * @link http://phpseclib.sourceforge.net */ -/** - * Include Math_BigInteger - */ -if (!class_exists('Math_BigInteger')) { - require_once('Math/BigInteger.php'); -} - /**#@+ * Tag Classes * @@ -249,6 +242,17 @@ class File_ASN1 { FILE_ASN1_TYPE_VISIBLE_STRING => 1, ); + function File_ASN1() + { + static $static_init = null; + if (!$static_init) { + $static_init = true; + if (!class_exists('Math_BigInteger')) { + require_once('Math/BigInteger.php'); + } + } + } + /** * Parse BER-encoding * diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 4212f37e..ee461aab 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -48,7 +48,7 @@ * Include File_ASN1 */ if (!class_exists('File_ASN1')) { - require_once('File/ASN1.php'); + require_once('ASN1.php'); } /** @@ -4345,4 +4345,4 @@ class File_X509 { $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false; return $temp != false ? $temp : $str; } -} \ No newline at end of file +} diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index f2c083fe..f9ae7441 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -58,7 +58,7 @@ * Include Net_SSH2 */ if (!class_exists('Net_SSH2')) { - require_once('Net/SSH2.php'); + require_once('SSH2.php'); } /**#@+ diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index 43febd4e..23448570 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -34,13 +34,6 @@ * @link http://phpseclib.sourceforge.net */ -/** - * Include Net_SSH2 - */ -if (!class_exists('Net_SFTP')) { - require_once('Net/SFTP.php'); -} - /** * SFTP Stream Wrapper * @@ -134,6 +127,18 @@ class Net_SFTP_Stream { */ var $notification; + /** + * The Constructor + * + * @access public + */ + function Net_SFTP_Stream() + { + if (!class_exists('Net_SFTP')) { + require_once('Net/SFTP.php'); + } + } + /** * Path Parser * @@ -756,4 +761,6 @@ class Net_SFTP_Stream { } } -stream_wrapper_register('sftp', 'Net_SFTP_Stream'); +if (function_exists('stream_wrapper_register')) { + stream_wrapper_register('sftp', 'Net_SFTP_Stream'); +} diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index d80e2ba9..89b5aa2c 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -66,52 +66,6 @@ * @link http://phpseclib.sourceforge.net */ -/** - * Include Math_BigInteger - * - * Used to do RSA encryption. - */ -if (!class_exists('Math_BigInteger')) { - require_once('Math/BigInteger.php'); -} - -/** - * Include Crypt_Null - */ -//require_once('Crypt/Null.php'); - -/** - * Include Crypt_DES - */ -if (!class_exists('Crypt_DES')) { - require_once('Crypt/DES.php'); -} - -/** - * Include Crypt_TripleDES - */ -if (!class_exists('Crypt_TripleDES')) { - require_once('Crypt/TripleDES.php'); -} - -/** - * Include Crypt_RC4 - */ -if (!class_exists('Crypt_RC4')) { - require_once('Crypt/RC4.php'); -} - -/** - * Include Crypt_Random - */ -// 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 -// call function_exists() a second time to stop the require_once from being called outside -// of the auto loader -if (!function_exists('crypt_random_string') && !class_exists('Crypt_Random') && !function_exists('crypt_random_string')) { - require_once('Crypt/Random.php'); -} - /**#@+ * Encryption Methods * @@ -495,6 +449,19 @@ class Net_SSH1 { */ function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES) { + if (!class_exists('Math_BigInteger')) { + require_once('Math/BigInteger.php'); + } + + // Include Crypt_Random + // 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 + // call function_exists() a second time to stop the require_once from being called outside + // of the auto loader + if (!function_exists('crypt_random_string') && !class_exists('Crypt_Random') && !function_exists('crypt_random_string')) { + require_once('Crypt/Random.php'); + } + $this->protocol_flags = array( 1 => 'NET_SSH1_MSG_DISCONNECT', 2 => 'NET_SSH1_SMSG_PUBLIC_KEY', @@ -636,18 +603,27 @@ class Net_SSH1 { // $this->crypto = new Crypt_Null(); // break; case NET_SSH1_CIPHER_DES: + if (!class_exists('Crypt_DES')) { + require_once('Crypt/DES.php'); + } $this->crypto = new Crypt_DES(); $this->crypto->disablePadding(); $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 8)); break; case NET_SSH1_CIPHER_3DES: + if (!class_exists('Crypt_TripleDES')) { + require_once('Crypt/TripleDES.php'); + } $this->crypto = new Crypt_TripleDES(CRYPT_DES_MODE_3CBC); $this->crypto->disablePadding(); $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 24)); break; //case NET_SSH1_CIPHER_RC4: + // if (!class_exists('Crypt_RC4')) { + // require_once('Crypt/RC4.php'); + // } // $this->crypto = new Crypt_RC4(); // $this->crypto->enableContinuousBuffer(); // $this->crypto->setKey(substr($session_key, 0, 16)); diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 7243447a..d3688818 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -68,64 +68,6 @@ * @link http://phpseclib.sourceforge.net */ -/** - * Include Math_BigInteger - * - * Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. - */ -if (!class_exists('Math_BigInteger')) { - require_once('Math/BigInteger.php'); -} - -/** - * Include Crypt_Random - */ -if (!function_exists('crypt_random_string')) { - require_once('Crypt/Random.php'); -} - -/** - * Include Crypt_Hash - */ -if (!class_exists('Crypt_Hash')) { - require_once('Crypt/Hash.php'); -} - -/** - * Include Crypt_TripleDES - */ -if (!class_exists('Crypt_TripleDES')) { - require_once('Crypt/TripleDES.php'); -} - -/** - * Include Crypt_RC4 - */ -if (!class_exists('Crypt_RC4')) { - require_once('Crypt/RC4.php'); -} - -/** - * Include Crypt_AES - */ -if (!class_exists('Crypt_AES')) { - require_once('Crypt/AES.php'); -} - -/** - * Include Crypt_Twofish - */ -if (!class_exists('Crypt_Twofish')) { - require_once('Crypt/Twofish.php'); -} - -/** - * Include Crypt_Blowfish - */ -if (!class_exists('Crypt_Blowfish')) { - require_once('Crypt/Blowfish.php'); -} - /**#@+ * Execution Bitmap Masks * @@ -791,6 +733,20 @@ class Net_SSH2 { */ 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')) { + require_once('Math/BigInteger.php'); + } + + if (!function_exists('crypt_random_string')) { + require_once('Crypt/Random.php'); + } + + if (!class_exists('Crypt_Hash')) { + require_once('Crypt/Hash.php'); + } + $this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5 $this->message_numbers = array( 1 => 'NET_SSH2_MSG_DISCONNECT', @@ -1334,30 +1290,48 @@ class Net_SSH2 { switch ($encrypt) { case '3des-cbc': + if (!class_exists('Crypt_TripleDES')) { + require_once('Crypt/TripleDES.php'); + } $this->encrypt = new Crypt_TripleDES(); // $this->encrypt_block_size = 64 / 8 == the default break; case '3des-ctr': + if (!class_exists('Crypt_TripleDES')) { + require_once('Crypt/TripleDES.php'); + } $this->encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR); // $this->encrypt_block_size = 64 / 8 == the default break; case 'aes256-cbc': case 'aes192-cbc': case 'aes128-cbc': + if (!class_exists('Crypt_AES')) { + require_once('Crypt/AES.php'); + } $this->encrypt = new Crypt_AES(); $this->encrypt_block_size = 16; // eg. 128 / 8 break; case 'aes256-ctr': case 'aes192-ctr': case 'aes128-ctr': + if (!class_exists('Crypt_AES')) { + require_once('Crypt/AES.php'); + } $this->encrypt = new Crypt_AES(CRYPT_AES_MODE_CTR); $this->encrypt_block_size = 16; // eg. 128 / 8 break; case 'blowfish-cbc': + if (!class_exists('Crypt_Blowfish')) { + require_once('Crypt/Blowfish.php'); + } $this->encrypt = new Crypt_Blowfish(); $this->encrypt_block_size = 8; break; case 'blowfish-ctr': + if (!class_exists('Crypt_Blowfish')) { + require_once('Crypt/Blowfish.php'); + } $this->encrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR); $this->encrypt_block_size = 8; break; @@ -1365,18 +1339,27 @@ class Net_SSH2 { case 'twofish192-cbc': case 'twofish256-cbc': case 'twofish-cbc': + if (!class_exists('Crypt_Twofish')) { + require_once('Crypt/Twofish.php'); + } $this->encrypt = new Crypt_Twofish(); $this->encrypt_block_size = 16; break; case 'twofish128-ctr': case 'twofish192-ctr': case 'twofish256-ctr': + if (!class_exists('Crypt_Twofish')) { + require_once('Crypt/Twofish.php'); + } $this->encrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR); $this->encrypt_block_size = 16; break; case 'arcfour': case 'arcfour128': case 'arcfour256': + if (!class_exists('Crypt_RC4')) { + require_once('Crypt/RC4.php'); + } $this->encrypt = new Crypt_RC4(); break; case 'none'; @@ -1385,28 +1368,46 @@ class Net_SSH2 { switch ($decrypt) { case '3des-cbc': + if (!class_exists('Crypt_TripleDES')) { + require_once('Crypt/TripleDES.php'); + } $this->decrypt = new Crypt_TripleDES(); break; case '3des-ctr': + if (!class_exists('Crypt_TripleDES')) { + require_once('Crypt/TripleDES.php'); + } $this->decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR); break; case 'aes256-cbc': case 'aes192-cbc': case 'aes128-cbc': + if (!class_exists('Crypt_AES')) { + require_once('Crypt/AES.php'); + } $this->decrypt = new Crypt_AES(); $this->decrypt_block_size = 16; break; case 'aes256-ctr': case 'aes192-ctr': case 'aes128-ctr': + if (!class_exists('Crypt_AES')) { + require_once('Crypt/AES.php'); + } $this->decrypt = new Crypt_AES(CRYPT_AES_MODE_CTR); $this->decrypt_block_size = 16; break; case 'blowfish-cbc': + if (!class_exists('Crypt_Blowfish')) { + require_once('Crypt/Blowfish.php'); + } $this->decrypt = new Crypt_Blowfish(); $this->decrypt_block_size = 8; break; case 'blowfish-ctr': + if (!class_exists('Crypt_Blowfish')) { + require_once('Crypt/Blowfish.php'); + } $this->decrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR); $this->decrypt_block_size = 8; break; @@ -1414,18 +1415,27 @@ class Net_SSH2 { case 'twofish192-cbc': case 'twofish256-cbc': case 'twofish-cbc': + if (!class_exists('Crypt_Twofish')) { + require_once('Crypt/Twofish.php'); + } $this->decrypt = new Crypt_Twofish(); $this->decrypt_block_size = 16; break; case 'twofish128-ctr': case 'twofish192-ctr': case 'twofish256-ctr': + if (!class_exists('Crypt_Twofish')) { + require_once('Crypt/Twofish.php'); + } $this->decrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR); $this->decrypt_block_size = 16; break; case 'arcfour': case 'arcfour128': case 'arcfour256': + if (!class_exists('Crypt_RC4')) { + require_once('Crypt/RC4.php'); + } $this->decrypt = new Crypt_RC4(); break; case 'none';