Пример #1
0
 /**
  * Initialize public key from PEM.
  *
  * @param PEM $pem
  * @throws \UnexpectedValueException
  * @return self
  */
 public static function fromPEM(PEM $pem)
 {
     switch ($pem->type()) {
         case PEM::TYPE_RSA_PUBLIC_KEY:
             return RSAPublicKey::fromDER($pem->data());
         case PEM::TYPE_PUBLIC_KEY:
             return PublicKeyInfo::fromPEM($pem)->publicKey();
     }
     throw new \UnexpectedValueException("PEM type " . $pem->type() . " is not a valid public key.");
 }
Пример #2
0
 /**
  * Get public key.
  *
  * @throws \RuntimeException
  * @return PublicKey
  */
 public function publicKey()
 {
     $algo = $this->algorithmIdentifier();
     switch ($algo->oid()) {
         // RSA
         case AlgorithmIdentifier::OID_RSA_ENCRYPTION:
             return RSAPublicKey::fromDER($this->_publicKeyData);
             // elliptic curve
         // elliptic curve
         case AlgorithmIdentifier::OID_EC_PUBLIC_KEY:
             if (!$algo instanceof ECPublicKeyAlgorithmIdentifier) {
                 throw new \UnexpectedValueException("Not an EC algorithm.");
             }
             // ECPoint is directly mapped into public key data
             return new ECPublicKey($this->_publicKeyData, $algo->namedCurve());
     }
     throw new \RuntimeException("Public key " . $algo->oid() . " not supported.");
 }