<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\CryptoTypes\Asymmetric\RFC8410; use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString; use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString; use SpomkyLabs\Pki\CryptoEncoding\PEM; use SpomkyLabs\Pki\CryptoTypes\Asymmetric\Attribute\OneAsymmetricKeyAttributes; use SpomkyLabs\Pki\CryptoTypes\Asymmetric\OneAsymmetricKey; use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PrivateKey; abstract class RFC8410PrivateKey extends PrivateKey { protected int $_version; protected ?OneAsymmetricKeyAttributes $_attributes; protected function __construct( protected string $_privateKeyData, protected ?string $_publicKeyData = null ) { $this->_version = OneAsymmetricKey::VERSION_2; $this->_attributes = null; } public function withVersion(int $version): self { $obj = clone $this; $obj->_version = $version; return $obj; } public function withAttributes(?OneAsymmetricKeyAttributes $attribs): self { $obj = clone $this; $obj->_attributes = $attribs; return $obj; } public function privateKeyData(): string { return $this->_privateKeyData; } public function hasPublicKey(): bool { return isset($this->_publicKeyData); } public function toASN1(): OctetString { return OctetString::create($this->_privateKeyData); } public function toDER(): string { return $this->toASN1() ->toDER(); } public function toPEM(): PEM { $pub = $this->_publicKeyData === null ? null : BitString::create($this->_publicKeyData); return OneAsymmetricKey::create($this->algorithmIdentifier(), $this->toDER(), $this->_attributes, $pub) ->withVersion($this->_version) ->toPEM(); } } 