HMAC::sign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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($signedInput, $signature);
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    abstract protected function hashingAlgorithm();
39
}
40