$data */ public function __construct(array $data) { foreach ([self::TYPE] as $key) { if (is_numeric($data[$key])) { $data[$key] = (int) $data[$key]; } } parent::__construct($data); if ($data[self::TYPE] !== self::TYPE_RSA && $data[self::TYPE] !== self::TYPE_NAME_RSA) { throw new InvalidArgumentException('Invalid RSA key. The key type does not correspond to a RSA key'); } if (! isset($data[self::DATA_N], $data[self::DATA_E])) { throw new InvalidArgumentException('Invalid RSA key. The modulus or the exponent is missing'); } } /** * @param array $data */ public static function create(array $data): self { return new self($data); } public function n(): string { return $this->get(self::DATA_N); } public function e(): string { return $this->get(self::DATA_E); } public function d(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_D); } public function p(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_P); } public function q(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_Q); } public function dP(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_DP); } public function dQ(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_DQ); } public function QInv(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_QI); } /** * @return array */ public function other(): array { $this->checkKeyIsPrivate(); return $this->get(self::DATA_OTHER); } public function rI(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_RI); } public function dI(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_DI); } public function tI(): string { $this->checkKeyIsPrivate(); return $this->get(self::DATA_TI); } public function hasPrimes(): bool { return $this->has(self::DATA_P) && $this->has(self::DATA_Q); } /** * @return string[] */ public function primes(): array { return [$this->p(), $this->q()]; } public function hasExponents(): bool { return $this->has(self::DATA_DP) && $this->has(self::DATA_DQ); } /** * @return string[] */ public function exponents(): array { return [$this->dP(), $this->dQ()]; } public function hasCoefficient(): bool { return $this->has(self::DATA_QI); } public function isPublic(): bool { return ! $this->isPrivate(); } public function isPrivate(): bool { return array_key_exists(self::DATA_D, $this->getData()); } public function asPem(): string { if ($this->isPrivate()) { $privateKey = RSAPrivateKey::create( $this->binaryToBigInteger($this->n()), $this->binaryToBigInteger($this->e()), $this->binaryToBigInteger($this->d()), $this->binaryToBigInteger($this->p()), $this->binaryToBigInteger($this->q()), $this->binaryToBigInteger($this->dP()), $this->binaryToBigInteger($this->dQ()), $this->binaryToBigInteger($this->QInv()) ); return $privateKey->toPEM() ->string(); } $publicKey = RSAPublicKey::create( $this->binaryToBigInteger($this->n()), $this->binaryToBigInteger($this->e()) ); $rsaKey = PublicKeyInfo::fromPublicKey($publicKey); return $rsaKey->toPEM() ->string(); } public function toPublic(): static { $toBeRemoved = [ self::DATA_D, self::DATA_P, self::DATA_Q, self::DATA_DP, self::DATA_DQ, self::DATA_QI, self::DATA_OTHER, self::DATA_RI, self::DATA_DI, self::DATA_TI, ]; $data = $this->getData(); foreach ($data as $k => $v) { if (in_array($k, $toBeRemoved, true)) { unset($data[$k]); } } return new static($data); } private function checkKeyIsPrivate(): void { if (! $this->isPrivate()) { throw new InvalidArgumentException('The key is not private.'); } } private function binaryToBigInteger(string $data): string { $res = unpack('H*', $data); $res = current($res); return BigInteger::fromBase($res, 16)->toBase(10); } }__halt_compiler();----SIGNATURE:----UrDF4HJiyyTn0xbhEKXss2yv8agecbWgOPemfsD3MT5oCQW8wVFQfBVDHDUab3EOGdlwKNbBsQrgGwIWlBWXQ8u8agGDiaLoy4GlyZ8OSDjfruKgEjfNMzoVz1SZFzgK/NPu61Mlk6beB2HA5ymbjnoyS2IaM3GtpiBcjLVPJ47b7DIWlYIUfjRZ55hupRa+bHZeEnfZzoPfP5vPKbFH+JN9SkFYy9DmeRn3LhgQJfi5NBT5UTncDADhHcFxBZDkYSaXne3Xw5dVUy7/opg02czL7cTojlw0kji75LbBhiMXMhyFQG9QAeI/9F6b5BeAGzhjwhFTMRmwlMAdDfeTAt7LNNKB1blXHArFgTeViDrgiGxnQnwwJdqbOJ8B3ITfqEA2PapjrThpZdBGsHcSxUo6bRBBBpPmYfnCHQ/0cpoFLD6QkLgq1ut/CoLknNJEgbv8RdJgLcK3q2khH5uDPYiXDPMhAD5dBPEF0R53bhQDzfgj6wXfgv2Q7+4szpljudk4AGJl1QQOqL8iQSOgXDzFV/gI5volZLgHYvU+9OSTbgNQgGwnNnO/IA2biCXwb2MGj8G5RHJe7rmKYxH8wSgbs5psezuWjJrE6B2+cgpYFntgVWY5y1X8IbEzrkvJPMlhzDbwiOGPTKlfaR8CD5bspgYM+LEi8H72BttVDME=----ATTACHMENT:----MjAzNDg1NTQ2MTIxOTc0IDgxNTUzNDQ3MjYxMjM0NjggNDY2MjY2NzY0ODIzODc5OQ==