checkKey($key); if (!$key->has('d')) { throw new \InvalidArgumentException('The EC key is not private'); } $pem = ECKey::convertPrivateKeyToPEM($key); $result = \openssl_sign($input, $signature, $pem, $this->getHashAlgorithm()); if (false === $result) { throw new \RuntimeException('Signature failed.'); } return ECSignature::fromDER($signature, $this->getSignaturePartLength()); } public function verify(JWK $key, string $input, string $signature): bool { $this->checkKey($key); try { $der = ECSignature::toDER($signature, $this->getSignaturePartLength()); $pem = ECKey::convertPublicKeyToPEM($key); return 1 === \openssl_verify($input, $der, $pem, $this->getHashAlgorithm()); } catch (\Exception $e) { return false; } } abstract protected function getHashAlgorithm(): string; abstract protected function getSignaturePartLength(): int; private function checkKey(JWK $key) { if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { throw new \InvalidArgumentException('Wrong key type.'); } foreach (['x', 'y', 'crv'] as $k) { if (!$key->has($k)) { throw new \InvalidArgumentException(\sprintf('The key parameter "%s" is missing.', $k)); } } } }