BigInteger: optimize getLength()

This commit is contained in:
terrafrost 2024-02-24 13:41:06 -06:00
parent c114503b5d
commit a922309855
3 changed files with 16 additions and 4 deletions

View File

@ -619,7 +619,7 @@ abstract class Engine implements \JsonSerializable
*/ */
public function getLengthInBytes() public function getLengthInBytes()
{ {
return strlen($this->toBytes()); return (int) ceil($this->getLength() / 8);
} }
/** /**

View File

@ -1341,4 +1341,17 @@ abstract class PHP extends Engine
} }
return false; return false;
} }
/**
* Return the size of a BigInteger in bits
*
* @return int
*/
public function getLength()
{
$max = count($this->value) - 1;
return $max != -1 ?
$max * static::BASE + intval(ceil(log($this->value[$max] + 1, 2))) :
0;
}
} }

View File

@ -455,9 +455,8 @@ class ASN1Test extends PhpseclibTestCase
{ {
$cert = file_get_contents(dirname(__FILE__) . '/ASN1/mal-cert-02.der'); $cert = file_get_contents(dirname(__FILE__) . '/ASN1/mal-cert-02.der');
$asn1 = new ASN1(); $decoded = ASN1::decodeBER($cert);
$decoded = $asn1->decodeBER($cert); $this->assertNull($decoded);
$this->assertFalse($decoded[0]);
//$x509 = new X509(); //$x509 = new X509();
//$x509->loadX509($cert); //$x509->loadX509($cert);