@@ -14,29 +14,29 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UniversalString extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - protected function _validateString(string $string): bool |
|
| 35 | - { |
|
| 36 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 37 | - if (strlen($string) % 4 != 0) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + protected function _validateString(string $string): bool |
|
| 35 | + { |
|
| 36 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 37 | + if (strlen($string) % 4 != 0) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -14,25 +14,25 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UTF8String extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UTF8_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UTF8_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - protected function _validateString(string $string): bool |
|
| 35 | - { |
|
| 36 | - return mb_check_encoding($string, "UTF-8"); |
|
| 37 | - } |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + protected function _validateString(string $string): bool |
|
| 35 | + { |
|
| 36 | + return mb_check_encoding($string, "UTF-8"); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -12,27 +12,27 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class T61String extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_T61_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_T61_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - // allow everything since there's literally |
|
| 35 | - // thousands of allowed characters (16 bit composed characters) |
|
| 36 | - return true; |
|
| 37 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + // allow everything since there's literally |
|
| 35 | + // thousands of allowed characters (16 bit composed characters) |
|
| 36 | + return true; |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class VisibleString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -12,26 +12,26 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ObjectDescriptor extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $descriptor |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $descriptor) |
|
| 23 | - { |
|
| 24 | - $this->_string = $descriptor; |
|
| 25 | - $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $descriptor |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $descriptor) |
|
| 23 | + { |
|
| 24 | + $this->_string = $descriptor; |
|
| 25 | + $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Get the object descriptor. |
|
| 30 | - * |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function descriptor(): string |
|
| 34 | - { |
|
| 35 | - return $this->_string; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Get the object descriptor. |
|
| 30 | + * |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function descriptor(): string |
|
| 34 | + { |
|
| 35 | + return $this->_string; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class NumericString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -11,48 +11,48 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String value. |
|
| 16 | - * |
|
| 17 | - * @var string $_string |
|
| 18 | - */ |
|
| 19 | - protected $_string; |
|
| 14 | + /** |
|
| 15 | + * String value. |
|
| 16 | + * |
|
| 17 | + * @var string $_string |
|
| 18 | + */ |
|
| 19 | + protected $_string; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $string |
|
| 25 | - * @throws \InvalidArgumentException |
|
| 26 | - */ |
|
| 27 | - public function __construct(string $string) |
|
| 28 | - { |
|
| 29 | - if (!$this->_validateString($string)) { |
|
| 30 | - throw new \InvalidArgumentException( |
|
| 31 | - sprintf("Not a valid %s string.", |
|
| 32 | - self::tagToName($this->_typeTag))); |
|
| 33 | - } |
|
| 34 | - $this->_string = $string; |
|
| 35 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $string |
|
| 25 | + * @throws \InvalidArgumentException |
|
| 26 | + */ |
|
| 27 | + public function __construct(string $string) |
|
| 28 | + { |
|
| 29 | + if (!$this->_validateString($string)) { |
|
| 30 | + throw new \InvalidArgumentException( |
|
| 31 | + sprintf("Not a valid %s string.", |
|
| 32 | + self::tagToName($this->_typeTag))); |
|
| 33 | + } |
|
| 34 | + $this->_string = $string; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the string value. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function string(): string |
|
| 43 | - { |
|
| 44 | - return $this->_string; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the string value. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function string(): string |
|
| 43 | + { |
|
| 44 | + return $this->_string; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Check whether string is valid for the concrete type. |
|
| 49 | - * |
|
| 50 | - * @param string $string |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - protected function _validateString(string $string): bool |
|
| 54 | - { |
|
| 55 | - // Override in derived classes |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 47 | + /** |
|
| 48 | + * Check whether string is valid for the concrete type. |
|
| 49 | + * |
|
| 50 | + * @param string $string |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + protected function _validateString(string $string): bool |
|
| 54 | + { |
|
| 55 | + // Override in derived classes |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -71,11 +71,11 @@ |
||
| 71 | 71 | $num = $this->_number->gmpObj(); |
| 72 | 72 | switch (gmp_sign($num)) { |
| 73 | 73 | // positive |
| 74 | - case 1: |
|
| 75 | - return self::_encodePositiveInteger($num); |
|
| 76 | - // negative |
|
| 77 | - case -1: |
|
| 78 | - return self::_encodeNegativeInteger($num); |
|
| 74 | + case 1: |
|
| 75 | + return self::_encodePositiveInteger($num); |
|
| 76 | + // negative |
|
| 77 | + case -1: |
|
| 78 | + return self::_encodeNegativeInteger($num); |
|
| 79 | 79 | } |
| 80 | 80 | // zero |
| 81 | 81 | return "\0"; |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -16,161 +16,161 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Integer extends Element |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The number. |
|
| 24 | - * |
|
| 25 | - * @var BigInt |
|
| 26 | - */ |
|
| 27 | - private $_number; |
|
| 22 | + /** |
|
| 23 | + * The number. |
|
| 24 | + * |
|
| 25 | + * @var BigInt |
|
| 26 | + */ |
|
| 27 | + private $_number; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param int|string $number Base 10 integer |
|
| 33 | - */ |
|
| 34 | - public function __construct($number) |
|
| 35 | - { |
|
| 36 | - $this->_typeTag = self::TYPE_INTEGER; |
|
| 37 | - if (!self::_validateNumber($number)) { |
|
| 38 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 39 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 40 | - } |
|
| 41 | - $this->_number = new BigInt($number); |
|
| 42 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param int|string $number Base 10 integer |
|
| 33 | + */ |
|
| 34 | + public function __construct($number) |
|
| 35 | + { |
|
| 36 | + $this->_typeTag = self::TYPE_INTEGER; |
|
| 37 | + if (!self::_validateNumber($number)) { |
|
| 38 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 39 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 40 | + } |
|
| 41 | + $this->_number = new BigInt($number); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get the number as a base 10. |
|
| 46 | - * |
|
| 47 | - * @return string Integer as a string |
|
| 48 | - */ |
|
| 49 | - public function number(): string |
|
| 50 | - { |
|
| 51 | - return $this->_number->base10(); |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * Get the number as a base 10. |
|
| 46 | + * |
|
| 47 | + * @return string Integer as a string |
|
| 48 | + */ |
|
| 49 | + public function number(): string |
|
| 50 | + { |
|
| 51 | + return $this->_number->base10(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get the number as an integer type. |
|
| 56 | - * |
|
| 57 | - * @return int |
|
| 58 | - */ |
|
| 59 | - public function intNumber(): int |
|
| 60 | - { |
|
| 61 | - return $this->_number->intVal(); |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Get the number as an integer type. |
|
| 56 | + * |
|
| 57 | + * @return int |
|
| 58 | + */ |
|
| 59 | + public function intNumber(): int |
|
| 60 | + { |
|
| 61 | + return $this->_number->intVal(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * |
|
| 66 | - * {@inheritdoc} |
|
| 67 | - */ |
|
| 68 | - protected function _encodedContentDER(): string |
|
| 69 | - { |
|
| 70 | - $num = $this->_number->gmpObj(); |
|
| 71 | - switch (gmp_sign($num)) { |
|
| 72 | - // positive |
|
| 73 | - case 1: |
|
| 74 | - return self::_encodePositiveInteger($num); |
|
| 75 | - // negative |
|
| 76 | - case -1: |
|
| 77 | - return self::_encodeNegativeInteger($num); |
|
| 78 | - } |
|
| 79 | - // zero |
|
| 80 | - return "\0"; |
|
| 81 | - } |
|
| 64 | + /** |
|
| 65 | + * |
|
| 66 | + * {@inheritdoc} |
|
| 67 | + */ |
|
| 68 | + protected function _encodedContentDER(): string |
|
| 69 | + { |
|
| 70 | + $num = $this->_number->gmpObj(); |
|
| 71 | + switch (gmp_sign($num)) { |
|
| 72 | + // positive |
|
| 73 | + case 1: |
|
| 74 | + return self::_encodePositiveInteger($num); |
|
| 75 | + // negative |
|
| 76 | + case -1: |
|
| 77 | + return self::_encodeNegativeInteger($num); |
|
| 78 | + } |
|
| 79 | + // zero |
|
| 80 | + return "\0"; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Encode positive integer to DER content. |
|
| 85 | - * |
|
| 86 | - * @param \GMP $num |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - private static function _encodePositiveInteger(\GMP $num): string |
|
| 90 | - { |
|
| 91 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 92 | - // if first bit is 1, prepend full zero byte |
|
| 93 | - // to represent positive two's complement |
|
| 94 | - if (ord($bin[0]) & 0x80) { |
|
| 95 | - $bin = chr(0x00) . $bin; |
|
| 96 | - } |
|
| 97 | - return $bin; |
|
| 98 | - } |
|
| 83 | + /** |
|
| 84 | + * Encode positive integer to DER content. |
|
| 85 | + * |
|
| 86 | + * @param \GMP $num |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + private static function _encodePositiveInteger(\GMP $num): string |
|
| 90 | + { |
|
| 91 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 92 | + // if first bit is 1, prepend full zero byte |
|
| 93 | + // to represent positive two's complement |
|
| 94 | + if (ord($bin[0]) & 0x80) { |
|
| 95 | + $bin = chr(0x00) . $bin; |
|
| 96 | + } |
|
| 97 | + return $bin; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Encode negative integer to DER content. |
|
| 102 | - * |
|
| 103 | - * @param \GMP $num |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
| 107 | - { |
|
| 108 | - $num = gmp_abs($num); |
|
| 109 | - // compute number of bytes required |
|
| 110 | - $width = 1; |
|
| 111 | - if ($num > 128) { |
|
| 112 | - $tmp = $num; |
|
| 113 | - do { |
|
| 114 | - $width++; |
|
| 115 | - $tmp >>= 8; |
|
| 116 | - } while ($tmp > 128); |
|
| 117 | - } |
|
| 118 | - // compute two's complement 2^n - x |
|
| 119 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
| 120 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 121 | - // if first bit is 0, prepend full inverted byte |
|
| 122 | - // to represent negative two's complement |
|
| 123 | - if (!(ord($bin[0]) & 0x80)) { |
|
| 124 | - $bin = chr(0xff) . $bin; |
|
| 125 | - } |
|
| 126 | - return $bin; |
|
| 127 | - } |
|
| 100 | + /** |
|
| 101 | + * Encode negative integer to DER content. |
|
| 102 | + * |
|
| 103 | + * @param \GMP $num |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
| 107 | + { |
|
| 108 | + $num = gmp_abs($num); |
|
| 109 | + // compute number of bytes required |
|
| 110 | + $width = 1; |
|
| 111 | + if ($num > 128) { |
|
| 112 | + $tmp = $num; |
|
| 113 | + do { |
|
| 114 | + $width++; |
|
| 115 | + $tmp >>= 8; |
|
| 116 | + } while ($tmp > 128); |
|
| 117 | + } |
|
| 118 | + // compute two's complement 2^n - x |
|
| 119 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
| 120 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 121 | + // if first bit is 0, prepend full inverted byte |
|
| 122 | + // to represent negative two's complement |
|
| 123 | + if (!(ord($bin[0]) & 0x80)) { |
|
| 124 | + $bin = chr(0xff) . $bin; |
|
| 125 | + } |
|
| 126 | + return $bin; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * |
|
| 131 | - * {@inheritdoc} |
|
| 132 | - * @return self |
|
| 133 | - */ |
|
| 134 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 135 | - string $data, int &$offset): ElementBase |
|
| 136 | - { |
|
| 137 | - $idx = $offset; |
|
| 138 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 139 | - $bytes = substr($data, $idx, $length); |
|
| 140 | - $idx += $length; |
|
| 141 | - $neg = ord($bytes[0]) & 0x80; |
|
| 142 | - // negative, apply inversion of two's complement |
|
| 143 | - if ($neg) { |
|
| 144 | - $len = strlen($bytes); |
|
| 145 | - for ($i = 0; $i < $len; $i++) { |
|
| 146 | - $bytes[$i] = ~$bytes[$i]; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - $num = gmp_init(bin2hex($bytes), 16); |
|
| 150 | - // negative, apply addition of two's complement |
|
| 151 | - // and produce negative result |
|
| 152 | - if ($neg) { |
|
| 153 | - $num = gmp_neg($num + 1); |
|
| 154 | - } |
|
| 155 | - $offset = $idx; |
|
| 156 | - // late static binding since enumerated extends integer type |
|
| 157 | - return new static(gmp_strval($num, 10)); |
|
| 158 | - } |
|
| 129 | + /** |
|
| 130 | + * |
|
| 131 | + * {@inheritdoc} |
|
| 132 | + * @return self |
|
| 133 | + */ |
|
| 134 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 135 | + string $data, int &$offset): ElementBase |
|
| 136 | + { |
|
| 137 | + $idx = $offset; |
|
| 138 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 139 | + $bytes = substr($data, $idx, $length); |
|
| 140 | + $idx += $length; |
|
| 141 | + $neg = ord($bytes[0]) & 0x80; |
|
| 142 | + // negative, apply inversion of two's complement |
|
| 143 | + if ($neg) { |
|
| 144 | + $len = strlen($bytes); |
|
| 145 | + for ($i = 0; $i < $len; $i++) { |
|
| 146 | + $bytes[$i] = ~$bytes[$i]; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + $num = gmp_init(bin2hex($bytes), 16); |
|
| 150 | + // negative, apply addition of two's complement |
|
| 151 | + // and produce negative result |
|
| 152 | + if ($neg) { |
|
| 153 | + $num = gmp_neg($num + 1); |
|
| 154 | + } |
|
| 155 | + $offset = $idx; |
|
| 156 | + // late static binding since enumerated extends integer type |
|
| 157 | + return new static(gmp_strval($num, 10)); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Test that number is valid for this context. |
|
| 162 | - * |
|
| 163 | - * @param mixed $num |
|
| 164 | - * @return bool |
|
| 165 | - */ |
|
| 166 | - private static function _validateNumber($num): bool |
|
| 167 | - { |
|
| 168 | - if (is_int($num)) { |
|
| 169 | - return true; |
|
| 170 | - } |
|
| 171 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 172 | - return true; |
|
| 173 | - } |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 160 | + /** |
|
| 161 | + * Test that number is valid for this context. |
|
| 162 | + * |
|
| 163 | + * @param mixed $num |
|
| 164 | + * @return bool |
|
| 165 | + */ |
|
| 166 | + private static function _validateNumber($num): bool |
|
| 167 | + { |
|
| 168 | + if (is_int($num)) { |
|
| 169 | + return true; |
|
| 170 | + } |
|
| 171 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 172 | + return true; |
|
| 173 | + } |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -6,106 +6,106 @@ |
||
| 6 | 6 | |
| 7 | 7 | class BigInt |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Number as a base10 integer string. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - private $_num; |
|
| 9 | + /** |
|
| 10 | + * Number as a base10 integer string. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + private $_num; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Number as an integer type. |
|
| 18 | - * |
|
| 19 | - * @internal Lazily initialized |
|
| 20 | - * @var int|null |
|
| 21 | - */ |
|
| 22 | - private $_intNum; |
|
| 16 | + /** |
|
| 17 | + * Number as an integer type. |
|
| 18 | + * |
|
| 19 | + * @internal Lazily initialized |
|
| 20 | + * @var int|null |
|
| 21 | + */ |
|
| 22 | + private $_intNum; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Constructor. |
|
| 26 | - * |
|
| 27 | - * @param string|int $num |
|
| 28 | - */ |
|
| 29 | - public function __construct($num) |
|
| 30 | - { |
|
| 31 | - $this->_num = strval($num); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Constructor. |
|
| 26 | + * |
|
| 27 | + * @param string|int $num |
|
| 28 | + */ |
|
| 29 | + public function __construct($num) |
|
| 30 | + { |
|
| 31 | + $this->_num = strval($num); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the number as a base10 integer string. |
|
| 36 | - * |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function base10(): string |
|
| 40 | - { |
|
| 41 | - return $this->_num; |
|
| 42 | - } |
|
| 34 | + /** |
|
| 35 | + * Get the number as a base10 integer string. |
|
| 36 | + * |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function base10(): string |
|
| 40 | + { |
|
| 41 | + return $this->_num; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get the number as an integer. |
|
| 46 | - * |
|
| 47 | - * @throws \RuntimeException If number overflows integer size |
|
| 48 | - * @return int |
|
| 49 | - */ |
|
| 50 | - public function intVal(): int |
|
| 51 | - { |
|
| 52 | - if (!isset($this->_intNum)) { |
|
| 53 | - $num = gmp_init($this->_num, 10); |
|
| 54 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 55 | - throw new \RuntimeException("Integer overflow."); |
|
| 56 | - } |
|
| 57 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 58 | - throw new \RuntimeException("Integer underflow."); |
|
| 59 | - } |
|
| 60 | - $this->_intNum = gmp_intval($num); |
|
| 61 | - } |
|
| 62 | - return $this->_intNum; |
|
| 63 | - } |
|
| 44 | + /** |
|
| 45 | + * Get the number as an integer. |
|
| 46 | + * |
|
| 47 | + * @throws \RuntimeException If number overflows integer size |
|
| 48 | + * @return int |
|
| 49 | + */ |
|
| 50 | + public function intVal(): int |
|
| 51 | + { |
|
| 52 | + if (!isset($this->_intNum)) { |
|
| 53 | + $num = gmp_init($this->_num, 10); |
|
| 54 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 55 | + throw new \RuntimeException("Integer overflow."); |
|
| 56 | + } |
|
| 57 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 58 | + throw new \RuntimeException("Integer underflow."); |
|
| 59 | + } |
|
| 60 | + $this->_intNum = gmp_intval($num); |
|
| 61 | + } |
|
| 62 | + return $this->_intNum; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Get the maximum integer value. |
|
| 67 | - * |
|
| 68 | - * @return \GMP |
|
| 69 | - */ |
|
| 70 | - private function _intMaxGmp(): \GMP |
|
| 71 | - { |
|
| 72 | - static $gmp; |
|
| 73 | - if (!isset($gmp)) { |
|
| 74 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 75 | - } |
|
| 76 | - return $gmp; |
|
| 77 | - } |
|
| 65 | + /** |
|
| 66 | + * Get the maximum integer value. |
|
| 67 | + * |
|
| 68 | + * @return \GMP |
|
| 69 | + */ |
|
| 70 | + private function _intMaxGmp(): \GMP |
|
| 71 | + { |
|
| 72 | + static $gmp; |
|
| 73 | + if (!isset($gmp)) { |
|
| 74 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 75 | + } |
|
| 76 | + return $gmp; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Get the minimum integer value. |
|
| 81 | - * |
|
| 82 | - * @return \GMP |
|
| 83 | - */ |
|
| 84 | - private function _intMinGmp(): \GMP |
|
| 85 | - { |
|
| 86 | - static $gmp; |
|
| 87 | - if (!isset($gmp)) { |
|
| 88 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 89 | - } |
|
| 90 | - return $gmp; |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Get the minimum integer value. |
|
| 81 | + * |
|
| 82 | + * @return \GMP |
|
| 83 | + */ |
|
| 84 | + private function _intMinGmp(): \GMP |
|
| 85 | + { |
|
| 86 | + static $gmp; |
|
| 87 | + if (!isset($gmp)) { |
|
| 88 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 89 | + } |
|
| 90 | + return $gmp; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get the number as a GMP object. |
|
| 95 | - * |
|
| 96 | - * @return \GMP |
|
| 97 | - */ |
|
| 98 | - public function gmpObj(): \GMP |
|
| 99 | - { |
|
| 100 | - return gmp_init($this->_num, 10); |
|
| 101 | - } |
|
| 93 | + /** |
|
| 94 | + * Get the number as a GMP object. |
|
| 95 | + * |
|
| 96 | + * @return \GMP |
|
| 97 | + */ |
|
| 98 | + public function gmpObj(): \GMP |
|
| 99 | + { |
|
| 100 | + return gmp_init($this->_num, 10); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function __toString() |
|
| 108 | - { |
|
| 109 | - return $this->base10(); |
|
| 110 | - } |
|
| 103 | + /** |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function __toString() |
|
| 108 | + { |
|
| 109 | + return $this->base10(); |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Util; |
| 6 | 6 | |