/** * @param PrivateKey $private_key * @param string $message * @return string * @throws InvalidKeyLengthAlgorithmException * @throws InvalidKeyTypeAlgorithmException */ public function sign(PrivateKey $private_key, $message) { if (!$private_key instanceof RSAPrivateKey) { throw new InvalidKeyTypeAlgorithmException(); } if ($this->getMinKeyLen() > $private_key->getBitLength()) { throw new InvalidKeyLengthAlgorithmException(sprintf('min len %s - cur len %s.', $this->getMinKeyLen(), $private_key->getBitLength())); } $res = $this->rsa_impl->loadKey($private_key->getEncoded()); if (!$res) { throw new InvalidKeyTypeAlgorithmException(); } $this->rsa_impl->setHash($this->getHashingAlgorithm()); $this->rsa_impl->setMGFHash($this->getHashingAlgorithm()); $this->rsa_impl->setSignatureMode($this->getPaddingMode()); return $this->rsa_impl->sign($message); }
/** * @param PrivateKey $private_key * @return IAsymmetricJWK|null * @throws InvalidJWKType */ public static function fromPrivateKey(PrivateKey $private_key) { if (!$private_key instanceof RSAPrivateKey) { throw new InvalidJWKType(); } $jwk = new RSAJWK(); $jwk->private_key = $private_key; $jwk->set[RSAKeysParameters::Exponent] = Base64urlUInt::fromBigInt($private_key->getPublicExponent()); $jwk->set[RSAKeysParameters::Modulus] = Base64urlUInt::fromBigInt($private_key->getModulus()); $jwk->set[RSAKeysParameters::PrivateExponent] = Base64urlUInt::fromBigInt($private_key->getPrivateExponent()); return $jwk; }