本文整理汇总了PHP中Math_BigInteger::compare方法的典型用法代码示例。如果您正苦于以下问题:PHP Math_BigInteger::compare方法的具体用法?PHP Math_BigInteger::compare怎么用?PHP Math_BigInteger::compare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Math_BigInteger
的用法示例。
在下文中一共展示了Math_BigInteger::compare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: verifySigKey
static function verifySigKey($hash, $sig, $pubkey)
{
$rsa = new rsaMyExts();
$rsa->loadKey($pubkey);
$hashBI = new Math_BigInteger($hash, 16);
$sigBI = new Math_BigInteger($sig, 16);
if ($sigBI->compare($rsa->zero) < 0 || $sigBI->compare($rsa->modulus) > 0) {
return false;
}
// the signature has more bits than the modulus --> the signature cannot has been created by the given key and the exponentiation cannot be performed.
$verify = $rsa->_rsaep($sigBI);
if ($hashBI->equals($verify)) {
return true;
}
WrongRequestException::throwException(1001, "Error: signature verifcation failed", "verifySig: given hash: {$hash}, calculated hash: " . $verify->toHex());
}
示例2: compareTo
public function compareTo($id)
{
$logid = $id;
$val1 = new Math_BigInteger($this->mInt);
$val2 = new Math_BigInteger($logid->mInt);
if ($val1->compare($val2) < 0) {
return -1;
} else {
if ($val1->compare($val2) > 0) {
return 1;
} else {
if (strcmp($this->mSessionId, $logid->mSessionId) < 0) {
return -1;
} else {
if (strcmp($this->mSessionId, $logid->mSessionId) > 0) {
return 1;
}
}
}
}
return 0;
}
示例3: ssh1_connect
function ssh1_connect($host, $port)
{
$identifier = 'SSH-1.5-' . basename(__FILE__);
$fsock = fsockopen($host, $port, $errno, $errstr, 10);
if (!$fsock) {
die("Error {$errno}: {$errstr}");
}
$init_line = fgets($fsock, 255);
if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
die('Not an SSH server on the other side.');
}
if ($parts[1][0] != 1) {
die("SSH version {$parts[1]} is not supported!");
}
echo "Connecting to {$init_line}\r\n";
fputs($fsock, "{$identifier}\n");
$packet = get_binary_packet($fsock);
if ($packet['type'] != SSH_SMSG_PUBLIC_KEY) {
die('Expected SSH_SMSG_PUBLIC_KEY!');
}
$anti_spoofing_cookie = string_shift($packet['data'], 8);
string_shift($packet['data'], 4);
$temp = unpack('nlen', string_shift($packet['data'], 2));
$server_key_public_exponent = new Math_BigInteger(string_shift($packet['data'], ceil($temp['len'] / 8)), 256);
$temp = unpack('nlen', string_shift($packet['data'], 2));
$server_key_public_modulus = new Math_BigInteger(string_shift($packet['data'], ceil($temp['len'] / 8)), 256);
$temp = unpack('nlen', string_shift($packet['data'], 2));
$host_key_public_exponent = new Math_BigInteger(string_shift($packet['data'], ceil($temp['len'] / 8)), 256);
$temp = unpack('nlen', string_shift($packet['data'], 2));
$host_key_public_modulus = new Math_BigInteger(string_shift($packet['data'], ceil($temp['len'] / 8)), 256);
$session_id = pack('H*', md5($host_key_public_modulus . $server_key_public_modulus . $anti_spoofing_cookie));
// ought to use a cryptographically secure random number generator (which mt_srand is not)
list($sec, $usec) = explode(' ', microtime());
mt_srand((double) $sec + (double) $usec * 100000);
$session_key = '';
for ($i = 0; $i < 32; $i++) {
$session_key .= chr(mt_rand(0, 255));
}
$double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
echo "starting rsa encryption\r\n\r\n";
if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
$prepped_key = prep_session_key($double_encrypted_session_key, $server_key_public_modulus);
rsa_crypt($prepped_key, array($server_key_public_exponent, $server_key_public_modulus));
rsa_crypt2($prepped_key, array($server_key_public_exponent, $server_key_public_modulus));
} else {
$prepped_key = prep_session_key($double_encrypted_session_key, $host_key_public_modulus);
rsa_crypt($prepped_key, array($host_key_public_exponent, $host_key_public_modulus));
rsa_crypt2($prepped_key, array($host_key_public_exponent, $host_key_public_modulus));
}
}
示例4: randomPrime
/**
* Generate a random prime number.
*
* If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed,
* give up and return false.
*
* @param optional Integer $min
* @param optional Integer $max
* @param optional Integer $timeout
* @return Math_BigInteger
* @access public
* @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}.
*/
function randomPrime($min = false, $max = false, $timeout = false)
{
if ($min === false) {
$min = new Math_BigInteger(0);
}
if ($max === false) {
$max = new Math_BigInteger(0x7fffffff);
}
$compare = $max->compare($min);
if (!$compare) {
return $min->isPrime() ? $min : false;
} else {
if ($compare < 0) {
// if $min is bigger then $max, swap $min and $max
$temp = $max;
$max = $min;
$min = $temp;
}
}
static $one, $two;
if (!isset($one)) {
$one = new Math_BigInteger(1);
$two = new Math_BigInteger(2);
}
$start = time();
$x = $this->random($min, $max);
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
if ($x->compare($max) > 0) {
// if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range
if ($min->equals($max)) {
return false;
}
$x = $min->copy();
$x->_make_odd();
}
$initial_x = $x->copy();
while (true) {
if ($timeout !== false && time() - $start > $timeout) {
return false;
}
if ($x->isPrime()) {
return $x;
}
$x = $x->add($two);
if ($x->compare($max) > 0) {
$x = $min->copy();
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
}
if ($x->equals($initial_x)) {
return false;
}
}
}
示例5: getLogootPosition
/**
* generation of a position, logoot algorithm
* @param <Object> $start is the previous logootPosition
* @param <Object> $end is the next logootPosition
* @param <Integer> $N number of positions generated (should be 1 in our case)
* @param <Object> $sid session id
* @return <Object> a logootPosition between $start and $end
*/
private function getLogootPosition($start, $end, $N, $sid)
{
$result = array();
$Id_Max = LogootId::IdMax();
$Id_Min = LogootId::IdMin();
$i = 0;
$pos = array();
$currentPosition = new LogootPosition($pos);
// voir constructeur
$inf = new Math_BigInteger("0");
$sup = new Math_BigInteger("0");
$isInf = false;
while (true) {
$inf = new Math_BigInteger($start->get($i)->getInt());
if ($isInf == true) {
$sup = new Math_BigInteger(INT_MAX);
} else {
$sup = new Math_BigInteger($end->get($i)->getInt());
}
$tmpVal = $sup->subtract($inf);
$tmpVal1 = $tmpVal->subtract(new Math_BigInteger("1"));
if ($tmpVal1->compare($N) > 0) {
// inf = start.get(i).getInteger();
// sup = end.get(i).getInteger();
break;
}
$currentPosition->add($start->get($i));
$i++;
if ($i == $start->size()) {
$start->add($Id_Min);
}
if ($i == $end->size()) {
$end->add($Id_Max);
}
if ($inf->compare($sup) < 0) {
$isInf = true;
}
}
$binf = $inf->add(new Math_BigInteger("1"));
$bsup = $sup->subtract(new Math_BigInteger("1"));
$slot = $bsup->subtract($binf);
$stepTmp = $slot->divide($N);
$step = $stepTmp[0];
// quotient, [1] is the remainder
$old = clone $currentPosition;
if ($step->compare(new Math_BigInteger(INT_MAX)) > 0) {
$lstep = new Math_BigInteger(INT_MAX);
$r = clone $currentPosition;
$tmpVal2 = $inf->random($inf, $sup);
$r->set($i, $tmpVal2->toString(), $sid);
$result[] = $r;
// result est une arraylist<Position>
return $result;
} else {
$lstep = $step;
}
if ($lstep->compare(new Math_BigInteger("0")) == 0) {
$lstep = new Math_BigInteger("1");
}
$p = clone $currentPosition;
$p->set($i, $inf->toString(), $sid);
$tmpVal3 = (int) $N->toString();
for ($j = 0; $j < $tmpVal3; $j++) {
$r = clone $p;
if (!($lstep->compare(new Math_BigInteger("1")) == 0)) {
$tmpVal4 = new Math_BigInteger($p->get($i)->getInt());
$tmpVal5 = $tmpVal4->add($lstep);
// max
$tmpVal6 = new Math_BigInteger($p->get($i)->getInt());
// min
$add = $tmpVal6->random($tmpVal6, $tmpVal5);
$r->set($i, $add->toString(), $sid);
} else {
$r->add1($i, new Math_BigInteger("1"), $sid);
}
$result[] = clone $r;
// voir
$old = clone $r;
$tmpVal7 = new Math_BigInteger($p->get($i)->getInt());
$tmpVal7 = $tmpVal7->add($lstep);
$p->set($i, $tmpVal7->toString(), $sid);
}
return $result;
}
示例6: random
/**
* Generate a random number
*
* $generator should be the name of a random number generating function whose first parameter is the minimum
* value and whose second parameter is the maximum value. If this function needs to be seeded, it should be
* done before this function is called.
*
* @param optional Integer $min
* @param optional Integer $max
* @param optional String $generator
* @return Math_BigInteger
* @access public
*/
function random(Math_BigInteger $min = NULL, Math_BigInteger $max = NULL, $generator = 'mt_rand')
{
if ($min === NULL) {
$min = new Math_BigInteger(0);
}
/*
* @author muller jean-philippe
* This condition is used to exclude the min value from the possible
*values returned by this random
*/
/*else {
$min = $min->add(new Math_BigInteger(1));
}*/
// end of modification
if ($max === NULL) {
$max = new Math_BigInteger(0x7fffffff);
}
$compare = $max->compare($min);
if (!$compare) {
return $min;
} else {
if ($compare < 0) {
// if $min is bigger then $max, swap $min and $max
$temp = $max;
$max = $min;
$min = $temp;
}
}
$max = $max->subtract($min);
$max = ltrim($max->toBytes(), chr(0));
$size = strlen($max) - 1;
$random = '';
$bytes = $size & 3;
for ($i = 0; $i < $bytes; $i++) {
$random .= chr($generator(0, 255));
}
$blocks = $size >> 2;
for ($i = 0; $i < $blocks; $i++) {
$random .= pack('N', $generator(-2147483648.0, 0x7fffffff));
}
$temp = new Math_BigInteger($random, 256);
if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) {
$random = chr($generator(0, ord($max[0]) - 1)) . $random;
} else {
$random = chr($generator(0, ord($max[0]))) . $random;
}
$random = new Math_BigInteger($random, 256);
return $random->add($min);
}
示例7: randomPrime
/**
* Generate a random prime number.
*
* If there's not a prime within the given range, false will be returned.
* If more than $timeout seconds have elapsed, give up and return false.
*
* @param Math_BigInteger $arg1
* @param Math_BigInteger $arg2
* @param int $timeout
* @return Math_BigInteger|false
* @access public
* @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}.
*/
function randomPrime($arg1, $arg2 = false, $timeout = false)
{
if ($arg1 === false) {
return false;
}
if ($arg2 === false) {
$max = $arg1;
$min = $this;
} else {
$min = $arg1;
$max = $arg2;
}
$compare = $max->compare($min);
if (!$compare) {
return $min->isPrime() ? $min : false;
} elseif ($compare < 0) {
// if $min is bigger then $max, swap $min and $max
$temp = $max;
$max = $min;
$min = $temp;
}
static $one, $two;
if (!isset($one)) {
$one = new Math_BigInteger(1);
$two = new Math_BigInteger(2);
}
$start = time();
$x = $this->random($min, $max);
// gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>.
if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && extension_loaded('gmp') && version_compare(PHP_VERSION, '5.2.0', '>=')) {
$p = new Math_BigInteger();
$p->value = gmp_nextprime($x->value);
if ($p->compare($max) <= 0) {
return $p;
}
if (!$min->equals($x)) {
$x = $x->subtract($one);
}
return $x->randomPrime($min, $x);
}
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
if ($x->compare($max) > 0) {
// if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range
if ($min->equals($max)) {
return false;
}
$x = $min->copy();
$x->_make_odd();
}
$initial_x = $x->copy();
while (true) {
if ($timeout !== false && time() - $start > $timeout) {
return false;
}
if ($x->isPrime()) {
return $x;
}
$x = $x->add($two);
if ($x->compare($max) > 0) {
$x = $min->copy();
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
}
if ($x->equals($initial_x)) {
return false;
}
}
}
示例8: fgets
/**
* Connect to an SSHv1 server
*
* @return Boolean
* @access private
*/
function _connect()
{
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->connectionTimeout);
if (!$this->fsock) {
user_error(rtrim("Cannot connect to {$this->host}:{$this->port}. Error {$errno}. {$errstr}"));
return false;
}
$this->server_identification = $init_line = fgets($this->fsock, 255);
if (defined('NET_SSH1_LOGGING')) {
$this->_append_log('<-', $this->server_identification);
$this->_append_log('->', $this->identifier . "\r\n");
}
if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
user_error('Can only connect to SSH servers');
return false;
}
if ($parts[1][0] != 1) {
user_error("Cannot connect to SSH {$parts['1']} servers");
return false;
}
fputs($this->fsock, $this->identifier . "\r\n");
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
user_error('Expected SSH_SMSG_PUBLIC_KEY');
return false;
}
$anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->server_key_public_exponent = $server_key_public_exponent;
$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);
$this->server_key_public_modulus = $server_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->host_key_public_exponent = $host_key_public_exponent;
$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);
$this->host_key_public_modulus = $host_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
// get a list of the supported ciphers
extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
foreach ($this->supported_ciphers as $mask => $name) {
if (($supported_ciphers_mask & 1 << $mask) == 0) {
unset($this->supported_ciphers[$mask]);
}
}
// get a list of the supported authentications
extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
foreach ($this->supported_authentications as $mask => $name) {
if (($supported_authentications_mask & 1 << $mask) == 0) {
unset($this->supported_authentications[$mask]);
}
}
$session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
$session_key = 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) {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
} else {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
}
$cipher = isset($this->supported_ciphers[$this->cipher]) ? $this->cipher : NET_SSH1_CIPHER_3DES;
$data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
if (!$this->_send_binary_packet($data)) {
user_error('Error sending SSH_CMSG_SESSION_KEY');
return false;
}
switch ($cipher) {
//case NET_SSH1_CIPHER_NONE:
// $this->crypto = new Crypt_Null();
// break;
case NET_SSH1_CIPHER_DES:
if (!class_exists('Crypt_DES')) {
include_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')) {
include_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;
//.........这里部分代码省略.........
示例9: count
/**
* Barrett Modular Reduction
*
* See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} /
* {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly,
* so as not to require negative numbers (initially, this script didn't support negative numbers).
*
* @see _slidingWindow()
* @access private
* @param Math_BigInteger
* @return Math_BigInteger
*/
function _barrett($n)
{
static $cache;
$n_length = count($n->value);
if (!isset($cache[MATH_BIGINTEGER_VARIABLE]) || $n->compare($cache[MATH_BIGINTEGER_VARIABLE])) {
$cache[MATH_BIGINTEGER_VARIABLE] = $n;
$temp = new Math_BigInteger();
$temp->value = $this->_array_repeat(0, 2 * $n_length);
$temp->value[] = 1;
list($cache[MATH_BIGINTEGER_DATA], ) = $temp->divide($n);
}
$temp = new Math_BigInteger();
$temp->value = array_slice($this->value, $n_length - 1);
$temp = $temp->multiply($cache[MATH_BIGINTEGER_DATA]);
$temp->value = array_slice($temp->value, $n_length + 1);
$result = new Math_BigInteger();
$result->value = array_slice($this->value, 0, $n_length + 1);
$temp = $temp->multiply($n);
$temp->value = array_slice($temp->value, 0, $n_length + 1);
if ($result->compare($temp) < 0) {
$corrector = new Math_BigInteger();
$corrector->value = $this->_array_repeat(0, $n_length + 1);
$corrector->value[] = 1;
$result = $result->add($corrector);
}
$result = $result->subtract($temp);
while ($result->compare($n) > 0) {
$result = $result->subtract($n);
}
return $result;
}
示例10: foreach
/**
* Get the index of a revoked certificate.
*
* @param array $rclist
* @param String $serial
* @param Boolean $create
* optional
* @access private
* @return Integer or false
*/
function _revokedCertificate(&$rclist, $serial, $create = false)
{
$serial = new Math_BigInteger($serial);
foreach ($rclist as $i => $rc) {
if (!$serial->compare($rc['userCertificate'])) {
return $i;
}
}
if (!$create) {
return false;
}
$i = count($rclist);
$rclist[] = array('userCertificate' => $serial, 'revocationDate' => $this->_timeField(@date('D, d M Y H:i:s O')));
return $i;
}
示例11: fgets
/**
* Default Constructor.
*
* Connects to an SSHv1 server
*
* @param String $host
* @param optional Integer $port
* @param optional Integer $timeout
* @param optional Integer $cipher
* @return Net_SSH1
* @access public
*/
function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
{
$this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->fsock) {
user_error(rtrim("Cannot connect to {$host}. Error {$errno}. {$errstr}"), E_USER_NOTICE);
return;
}
$this->server_identification = $init_line = fgets($this->fsock, 255);
if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
user_error('Can only connect to SSH servers', E_USER_NOTICE);
return;
}
if ($parts[1][0] != 1) {
user_error("Cannot connect to SSH {$parts['1']} servers", E_USER_NOTICE);
return;
}
fputs($this->fsock, $this->identifier . "\r\n");
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
user_error('Expected SSH_SMSG_PUBLIC_KEY', E_USER_NOTICE);
return;
}
$anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->server_key_public_exponent = $server_key_public_exponent;
$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);
$this->server_key_public_modulus = $server_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->host_key_public_exponent = $host_key_public_exponent;
$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);
$this->host_key_public_modulus = $host_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
// get a list of the supported ciphers
list(, $supported_ciphers_mask) = unpack('N', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4));
foreach ($this->supported_ciphers as $mask => $name) {
if (($supported_ciphers_mask & 1 << $mask) == 0) {
unset($this->supported_ciphers[$mask]);
}
}
// get a list of the supported authentications
list(, $supported_authentications_mask) = unpack('N', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4));
foreach ($this->supported_authentications as $mask => $name) {
if (($supported_authentications_mask & 1 << $mask) == 0) {
unset($this->supported_authentications[$mask]);
}
}
$session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
$session_key = '';
for ($i = 0; $i < 32; $i++) {
$session_key .= chr(crypt_random(0, 255));
}
$double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
} else {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
}
$cipher = isset($this->supported_ciphers[$cipher]) ? $cipher : NET_SSH1_CIPHER_3DES;
$data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
if (!$this->_send_binary_packet($data)) {
user_error('Error sending SSH_CMSG_SESSION_KEY', E_USER_NOTICE);
return;
}
switch ($cipher) {
//case NET_SSH1_CIPHER_NONE:
// $this->crypto = new Crypt_Null();
// break;
case NET_SSH1_CIPHER_DES:
$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:
$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:
//.........这里部分代码省略.........
示例12: array
/**
* Default Constructor.
*
* Connects to an SSHv1 server
*
* @param String $host
* @param optional Integer $port
* @param optional Integer $timeout
* @param optional Integer $cipher
* @return Net_SSH1
* @access public
*/
function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
{
if (!class_exists('Math_BigInteger')) {
include_once EASYWIDIR . '/third_party/phpseclib/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 include_once from being called outside
// of the auto loader
if (!function_exists('crypt_random_string') && !class_exists('Crypt_Random') && !function_exists('crypt_random_string')) {
include_once EASYWIDIR . '/third_party/phpseclib/Crypt/Random.php';
}
$this->protocol_flags = array(1 => 'NET_SSH1_MSG_DISCONNECT', 2 => 'NET_SSH1_SMSG_PUBLIC_KEY', 3 => 'NET_SSH1_CMSG_SESSION_KEY', 4 => 'NET_SSH1_CMSG_USER', 9 => 'NET_SSH1_CMSG_AUTH_PASSWORD', 10 => 'NET_SSH1_CMSG_REQUEST_PTY', 12 => 'NET_SSH1_CMSG_EXEC_SHELL', 13 => 'NET_SSH1_CMSG_EXEC_CMD', 14 => 'NET_SSH1_SMSG_SUCCESS', 15 => 'NET_SSH1_SMSG_FAILURE', 16 => 'NET_SSH1_CMSG_STDIN_DATA', 17 => 'NET_SSH1_SMSG_STDOUT_DATA', 18 => 'NET_SSH1_SMSG_STDERR_DATA', 19 => 'NET_SSH1_CMSG_EOF', 20 => 'NET_SSH1_SMSG_EXITSTATUS', 33 => 'NET_SSH1_CMSG_EXIT_CONFIRMATION');
$this->_define_array($this->protocol_flags);
$this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->fsock) {
user_error(rtrim("Cannot connect to {$host}. Error {$errno}. {$errstr}"));
return;
}
$this->server_identification = $init_line = fgets($this->fsock, 255);
if (defined('NET_SSH1_LOGGING')) {
$this->_append_log('<-', $this->server_identification);
$this->_append_log('->', $this->identifier . "\r\n");
}
if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
user_error('Can only connect to SSH servers');
return;
}
if ($parts[1][0] != 1) {
user_error("Cannot connect to SSH {$parts['1']} servers");
return;
}
fputs($this->fsock, $this->identifier . "\r\n");
$response = $this->_get_binary_packet();
if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
user_error('Expected SSH_SMSG_PUBLIC_KEY');
return;
}
$anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->server_key_public_exponent = $server_key_public_exponent;
$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);
$this->server_key_public_modulus = $server_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
$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);
$this->host_key_public_exponent = $host_key_public_exponent;
$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);
$this->host_key_public_modulus = $host_key_public_modulus;
$this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
// get a list of the supported ciphers
extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
foreach ($this->supported_ciphers as $mask => $name) {
if (($supported_ciphers_mask & 1 << $mask) == 0) {
unset($this->supported_ciphers[$mask]);
}
}
// get a list of the supported authentications
extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
foreach ($this->supported_authentications as $mask => $name) {
if (($supported_authentications_mask & 1 << $mask) == 0) {
unset($this->supported_authentications[$mask]);
}
}
$session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
$session_key = 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) {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
} else {
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
$double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
}
$cipher = isset($this->supported_ciphers[$cipher]) ? $cipher : NET_SSH1_CIPHER_3DES;
$data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
if (!$this->_send_binary_packet($data)) {
user_error('Error sending SSH_CMSG_SESSION_KEY');
return;
}
switch ($cipher) {
//case NET_SSH1_CIPHER_NONE:
// $this->crypto = new Crypt_Null();
//.........这里部分代码省略.........
示例13: cmpAbs
/**
* Compares abs($num1) to abs($num2).
* Returns:
* -1, if abs($num1) < abs($num2)
* 0, if abs($num1) == abs($num2)
* 1, if abs($num1) > abs($num2)
*
* @param string $num1
* @param string $num2
* @return int
* @access public
*/
function cmpAbs($num1, $num2)
{
//strip minus
$num1 = $this->strip_minus($num1);
$num2 = $this->strip_minus($num2);
$num1 = new Math_BigInteger($num1, 10);
$num2 = new Math_BigInteger($num2, 10);
return $num1->compare($num2);
/* $this->strip_minus($num1);
$num2 = $this->strip_minus($num2);
$strlen1 = _byte_strlen($num1);
$strlen2 = _byte_strlen($num2);
if($strlen1 < $strlen2)
{
return -1;
}
else if($strlen1 > $strlen2)
{
return 1;
}
else
{
for($i =0; $i < $strlen1; $i+)
{
if($num1[$i] < $num2[$i])
{
return -1;
}
else if($num1[$i] > $num2[$i])
{
return 1;
}
}
return 0;
}
*/
}
示例14: randomPrime
public function randomPrime($min = false, $max = false, $timeout = false)
{
if ($min === false) {
$min = new Math_BigInteger(0);
}
if ($max === false) {
$max = new Math_BigInteger(2147483647);
}
$compare = $max->compare($min);
if (!$compare) {
return $min->isPrime() ? $min : false;
} else {
if ($compare < 0) {
$temp = $max;
$max = $min;
$min = $temp;
}
}
static $one;
static $two;
if (!isset($one)) {
$one = new Math_BigInteger(1);
$two = new Math_BigInteger(2);
}
$start = time();
$x = $this->random($min, $max);
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
if (0 < $x->compare($max)) {
if ($min->equals($max)) {
return false;
}
$x = $min->copy();
$x->_make_odd();
}
$initial_x = $x->copy();
while (true) {
if ($timeout !== false && $timeout < time() - $start) {
return false;
}
if ($x->isPrime()) {
return $x;
}
$x = $x->add($two);
if (0 < $x->compare($max)) {
$x = $min->copy();
if ($x->equals($two)) {
return $x;
}
$x->_make_odd();
}
if ($x->equals($initial_x)) {
return false;
}
}
}
示例15: cmp
/**
* Compare two arbitrary precision numbers
*
* @param string $a The left operand, as a string.
* @param string $b The right operand, as a string.
* @access public
* @return int Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand, -1 otherwise.
*/
public function cmp($a, $b)
{
$a = new Math_BigInteger($a);
$b = new Math_BigInteger($b);
$c = $a->compare($b);
return $c;
}