/** * Initialize from a PrivateKey object. * * @param PrivateKey $priv_key Private key * @throws \UnexpectedValueException * @return self */ public static function fromPrivateKey(PrivateKey $priv_key) { if ($priv_key instanceof RSAPrivateKey) { return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); } if ($priv_key instanceof ECPrivateKey) { return ECPrivateKeyJWK::fromECPrivateKey($priv_key); } throw new \UnexpectedValueException("Unsupported private key."); }
/** * * @param JWK $jwk * @param Header $header * @throws \UnexpectedValueException * @return RSASSAPKCS1Algorithm */ public static function fromJWK(JWK $jwk, Header $header) { $alg = JWA::deriveAlgorithmName($header, $jwk); if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); } $cls = self::MAP_ALGO_TO_CLASS[$alg]; if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); } return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); }
/** * Initialize from a private key. * * @param RSAPrivateKeyJWK $jwk * @return self */ public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) { return new static($jwk->publicKey(), $jwk); }