GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HMACAlgorithm::computeSignature()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWS\Algorithm;
6
7
use Sop\JWX\JWA\JWA;
8
use Sop\JWX\JWK\JWK;
9
use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK;
10
use Sop\JWX\JWS\SignatureAlgorithm;
11
use Sop\JWX\JWT\Header\Header;
12
use Sop\JWX\JWT\Parameter\AlgorithmParameter;
13
14
/**
15
 * Base class for algorithms implementing HMAC signature.
16
 *
17
 * @see https://tools.ietf.org/html/rfc7518#section-3.2
18
 */
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
    public 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
87
    {
88 12
        return $this->computeSignature($data) === $signature;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 11
    public function headerParameters(): array
95
    {
96 11
        return array_merge(parent::headerParameters(),
97 11
            [AlgorithmParameter::fromAlgorithm($this)]);
98
    }
99
100
    /**
101
     * Get algorithm name recognized by the Hash extension.
102
     */
103
    abstract protected function _hashAlgo(): string;
104
}
105