diff --git a/phpseclib/Math/BigInteger/Engines/Engine.php b/phpseclib/Math/BigInteger/Engines/Engine.php index e46e9609..474abe10 100644 --- a/phpseclib/Math/BigInteger/Engines/Engine.php +++ b/phpseclib/Math/BigInteger/Engines/Engine.php @@ -619,7 +619,7 @@ abstract class Engine implements \JsonSerializable */ public function getLengthInBytes() { - return strlen($this->toBytes()); + return (int) ceil($this->getLength() / 8); } /** diff --git a/phpseclib/Math/BigInteger/Engines/PHP.php b/phpseclib/Math/BigInteger/Engines/PHP.php index 7e85783e..2d895952 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP.php +++ b/phpseclib/Math/BigInteger/Engines/PHP.php @@ -1341,4 +1341,17 @@ abstract class PHP extends Engine } 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; + } } diff --git a/tests/Unit/File/ASN1Test.php b/tests/Unit/File/ASN1Test.php index b39ce46d..0dd40edc 100644 --- a/tests/Unit/File/ASN1Test.php +++ b/tests/Unit/File/ASN1Test.php @@ -455,9 +455,8 @@ class ASN1Test extends PhpseclibTestCase { $cert = file_get_contents(dirname(__FILE__) . '/ASN1/mal-cert-02.der'); - $asn1 = new ASN1(); - $decoded = $asn1->decodeBER($cert); - $this->assertFalse($decoded[0]); + $decoded = ASN1::decodeBER($cert); + $this->assertNull($decoded); //$x509 = new X509(); //$x509->loadX509($cert);