<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\CryptoTypes\Asymmetric; use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString; use SpomkyLabs\Pki\CryptoEncoding\PEM; use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; use SpomkyLabs\Pki\CryptoTypes\Asymmetric\RSA\RSAPublicKey; use UnexpectedValueException; abstract class PublicKey { abstract public function algorithmIdentifier(): AlgorithmIdentifierType; abstract public function toDER(): string; public function subjectPublicKey(): BitString { return BitString::create($this->toDER()); } public function publicKeyInfo(): PublicKeyInfo { return PublicKeyInfo::fromPublicKey($this); } public static function fromPEM(PEM $pem) { return match ($pem->type()) { PEM::TYPE_RSA_PUBLIC_KEY => RSAPublicKey::fromDER($pem->data()), PEM::TYPE_PUBLIC_KEY => PublicKeyInfo::fromPEM($pem)->publicKey(), default => throw new UnexpectedValueException('PEM type ' . $pem->type() . ' is not a valid public key.'), }; } } 