- crypt_random() used /dev/urandom in a rather slow way (thanks, TangiX!)

- revamped the logging system.  now, you can profile SSH2.php / SFTP.php without capturing full logs


git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@99 21d32557-59b3-4da0-833f-c5933fad653e
This commit is contained in:
Jim Wigginton 2010-04-07 03:50:54 +00:00
parent 9a3c2e836b
commit 39c97d3373
3 changed files with 67 additions and 34 deletions

View File

@ -35,7 +35,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: Random.php,v 1.6 2010-02-28 05:28:38 terrafrost Exp $ * @version $Id: Random.php,v 1.7 2010-04-07 03:50:54 terrafrost Exp $
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
@ -62,9 +62,11 @@ function crypt_random($min = 0, $max = 0x7FFFFFFF)
// see http://en.wikipedia.org/wiki//dev/random // see http://en.wikipedia.org/wiki//dev/random
if (file_exists('/dev/urandom')) { if (file_exists('/dev/urandom')) {
$fp = fopen('/dev/urandom', 'rb'); static $fp;
if (!$fp) {
$fp = fopen('/dev/urandom', 'rb');
}
extract(unpack('Nrandom', fread($fp, 4))); extract(unpack('Nrandom', fread($fp, 4)));
fclose($fp);
// say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this: // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this:
// -4 % 3 + 0 = -1, even though -1 < $min // -4 % 3 + 0 = -1, even though -1 < $min

View File

@ -48,7 +48,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMIX Jim Wigginton * @copyright MMIX Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: SFTP.php,v 1.18 2010-04-04 00:20:03 terrafrost Exp $ * @version $Id: SFTP.php,v 1.19 2010-04-07 03:50:54 terrafrost Exp $
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
@ -1288,7 +1288,9 @@ class Net_SFTP extends Net_SSH2 {
if (defined('NET_SFTP_LOGGING')) { if (defined('NET_SFTP_LOGGING')) {
$this->packet_type_log[] = '-> ' . $this->packet_types[$type] . $this->packet_type_log[] = '-> ' . $this->packet_types[$type] .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->packet_log[] = $data; if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
$this->packet_log[] = $data;
}
} }
return $result; return $result;
@ -1353,7 +1355,9 @@ class Net_SFTP extends Net_SSH2 {
if (defined('NET_SFTP_LOGGING')) { if (defined('NET_SFTP_LOGGING')) {
$this->packet_type_log[] = '<- ' . $this->packet_types[$this->packet_type] . $this->packet_type_log[] = '<- ' . $this->packet_types[$this->packet_type] .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->packet_log[] = $packet; if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
$this->packet_log[] = $packet;
}
} }
return $packet; return $packet;
@ -1362,26 +1366,25 @@ class Net_SFTP extends Net_SSH2 {
/** /**
* Returns a log of the packets that have been sent and received. * Returns a log of the packets that have been sent and received.
* *
* $type can be either NET_SFTP_LOG_SIMPLE or NET_SFTP_LOG_COMPLEX. Enable by defining NET_SFTP_LOGGING. * Returns a string if NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX, an array if NET_SFTP_LOGGING == NET_SFTP_LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING')
* *
* @param Integer $type
* @access public * @access public
* @return String or Array * @return String or Array
*/ */
function getSFTPLog($type = NET_SFTP_LOG_COMPLEX) function getSFTPLog()
{ {
$message_number_log = $this->message_number_log; if (!defined('NET_SFTP_LOGGING')) {
$message_log = $this->message_log; return false;
}
$this->message_number_log = $this->packet_type_log; switch (NET_SFTP_LOGGING) {
$this->message_log = $this->packet_log; case NET_SFTP_LOG_COMPLEX:
return $this->_format_log($this->packet_log, $this->packet_type_log);
$return = $this->getLog($type); break;
//case NET_SFTP_LOG_SIMPLE:
$this->message_number_log = $message_number_log; default:
$this->message_log = $message_log; return $this->packet_type_log;
}
return $return;
} }
/** /**

View File

@ -60,7 +60,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: SSH2.php,v 1.41 2010-04-04 00:20:03 terrafrost Exp $ * @version $Id: SSH2.php,v 1.42 2010-04-07 03:50:54 terrafrost Exp $
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
@ -661,9 +661,12 @@ class Net_SSH2 {
if (defined('NET_SSH2_LOGGING')) { if (defined('NET_SSH2_LOGGING')) {
$this->message_number_log[] = '<-'; $this->message_number_log[] = '<-';
$this->message_log[] = $temp;
$this->message_number_log[] = '->'; $this->message_number_log[] = '->';
$this->message_log[] = $this->identifier . "\r\n";
if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
$this->message_log[] = $temp;
$this->message_log[] = $this->identifier . "\r\n";
}
} }
$this->server_identifier = trim($temp); $this->server_identifier = trim($temp);
@ -1382,7 +1385,7 @@ class Net_SSH2 {
} }
// remove the username and password from the last logged packet // remove the username and password from the last logged packet
if (defined('NET_SSH2_LOGGING')) { if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
$packet = pack('CNa*Na*Na*CNa*', $packet = pack('CNa*Na*Na*CNa*',
NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection', NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection',
strlen('password'), 'password', 0, strlen('password'), 'password' strlen('password'), 'password', 0, strlen('password'), 'password'
@ -1662,7 +1665,9 @@ class Net_SSH2 {
$temp = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN'; $temp = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN';
$this->message_number_log[] = '<- ' . $temp . $this->message_number_log[] = '<- ' . $temp .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->message_log[] = $payload; if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
$this->message_log[] = $payload;
}
} }
return $this->_filter($payload); return $this->_filter($payload);
@ -1927,7 +1932,9 @@ class Net_SSH2 {
$temp = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN'; $temp = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN';
$this->message_number_log[] = '-> ' . $temp . $this->message_number_log[] = '-> ' . $temp .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->message_log[] = $data; if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
$this->message_log[] = $data;
}
} }
return $result; return $result;
@ -2047,24 +2054,45 @@ class Net_SSH2 {
/** /**
* Returns a log of the packets that have been sent and received. * Returns a log of the packets that have been sent and received.
* *
* $type can be either NET_SSH2_LOG_SIMPLE or NET_SSH2_LOG_COMPLEX. Enable by defining NET_SSH2_LOGGING. * Returns a string if NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX, an array if NET_SSH2_LOGGING == NET_SSH2_LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING')
* *
* @param Integer $type
* @access public * @access public
* @return String or Array * @return String or Array
*/ */
function getLog($type = NET_SSH2_LOG_COMPLEX) function getLog()
{ {
if ($type == NET_SSH2_LOG_SIMPLE) { if (!defined('NET_SSH2_LOGGING')) {
return $this->message_number_log; return false;
} }
switch (NET_SSH2_LOGGING) {
case NET_SSH2_LOG_SIMPLE:
return $this->message_number_log;
break;
case NET_SSH2_LOG_COMPLEX:
return $this->_format_log($this->message_log, $this->message_number_log);
break;
default:
return false;
}
}
/**
* Formats a log for printing
*
* @param Array $message_log
* @param Array $message_number_log
* @access private
* @return String
*/
function _format_log($message_log, $message_number_log)
{
static $boundary = ':', $long_width = 65, $short_width = 15; static $boundary = ':', $long_width = 65, $short_width = 15;
$output = ''; $output = '';
for ($i = 0; $i < count($this->message_log); $i++) { for ($i = 0; $i < count($message_log); $i++) {
$output.= $this->message_number_log[$i] . "\r\n"; $output.= $message_number_log[$i] . "\r\n";
$current_log = $this->message_log[$i]; $current_log = $message_log[$i];
do { do {
$fragment = $this->_string_shift($current_log, $short_width); $fragment = $this->_string_shift($current_log, $short_width);
$hex = substr( $hex = substr(