Debian Codegraph @

lib/3rdParty/composer/web-auth/cose-lib/src/Algorithm/Signature/ECDSA/ECDSA.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Cose\Algorithm\Signature\ECDSA;
6
7 use Cose\Algorithm\Signature\Signature;
8 use Cose\Key\Ec2Key;
9 use Cose\Key\Key;
10 use InvalidArgumentException;
11 use function openssl_sign;
12 use function openssl_verify;
13
14 /**
15 * @see \Cose\Tests\Algorithm\Signature\ECDSA\ECDSATest
16 */
17 abstract class ECDSA implements Signature
18 {
19 public function sign(string $data, Key $key): string
20 {
21 $key = $this->handleKey($key);
22 openssl_sign($data, $signature, $key->asPEM(), $this->getHashAlgorithm());
23
24 return ECSignature::fromAsn1($signature, $this->getSignaturePartLength());
25 }
26
27 public function verify(string $data, Key $key, string $signature): bool
28 {
29 $key = $this->handleKey($key);
30 $publicKey = $key->toPublic();
31 $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
32 return openssl_verify($data, $signature, $publicKey->asPEM(), $this->getHashAlgorithm()) === 1;
33 }
34
35 abstract protected function getCurve(): int;
36
37 abstract protected function getHashAlgorithm(): int;
38
39 abstract protected function getSignaturePartLength(): int;
40
41 private function handleKey(Key $key): Ec2Key
42 {
43 $key = Ec2Key::create($key->getData());
44 if ($key->curve() !== $this->getCurve()) {
45 throw new InvalidArgumentException('This key cannot be used with this algorithm');
46 }
47
48 return $key;
49 }
50 }