Completed
Push — master ( e214c2...9b8774 )
by Kevin
02:27
created

HMAC::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
crap 2.032
1
<?php
2
3
namespace Zenstruck\JWT\Signer;
4
5
use Zenstruck\JWT\Signer;
6
7
/**
8
 * @author Alessandro Nadalin <[email protected]>
9
 * @author Kevin Bond <[email protected]>
10
 */
11
abstract class HMAC implements Signer
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 7
    public function sign($input, $key)
17
    {
18 7
        return hash_hmac($this->hashingAlgorithm(), $input, (string) $key, true);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 6
    public function verify($input, $signature, $key)
25
    {
26 6
        if (!function_exists('hash_equals')) {
27
            throw new \RuntimeException('hash_equals is only available in PHP 5.6+, install symfony/polyfill-php56 in PHP 5.4/5.5');
28
        }
29
30 6
        $signedInput = $this->sign($input, $key);
31
32 6
        return hash_equals($signature, $signedInput);
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    abstract protected function hashingAlgorithm();
39
}
40