1 | <?php |
||
19 | abstract class HMACAlgorithm extends SignatureAlgorithm |
||
20 | { |
||
21 | /** |
||
22 | * Mapping from algorithm name to class name. |
||
23 | * |
||
24 | * @internal |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | const MAP_ALGO_TO_CLASS = [ |
||
29 | JWA::ALGO_HS256 => HS256Algorithm::class, |
||
30 | JWA::ALGO_HS384 => HS384Algorithm::class, |
||
31 | JWA::ALGO_HS512 => HS512Algorithm::class, |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Shared secret key. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $_key; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param string $key Shared secret key |
||
45 | */ |
||
46 | 23 | public function __construct(string $key) |
|
47 | { |
||
48 | 23 | $this->_key = $key; |
|
49 | 23 | } |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | * |
||
54 | * @return self |
||
55 | */ |
||
56 | 13 | public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
57 | { |
||
58 | 13 | $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
59 | 13 | $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
60 | 13 | if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
61 | 1 | throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
62 | } |
||
63 | 12 | $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
64 | 12 | return new $cls($jwk->key()); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | * |
||
70 | * @throws \RuntimeException For generic errors |
||
71 | */ |
||
72 | 27 | public function computeSignature(string $data): string |
|
73 | { |
||
74 | 27 | $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
75 | 27 | if (false === $result) { |
|
76 | 1 | $err = error_get_last(); |
|
77 | 1 | $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
78 | 1 | throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
79 | } |
||
80 | 26 | return $result; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 12 | public function validateSignature(string $data, string $signature): bool |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 11 | public function headerParameters(): array |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get algorithm name recognized by the Hash extension. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | abstract protected function _hashAlgo(): string; |
||
106 | } |
||
107 |