HmacSha   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 26
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A generateSignature() 0 3 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient\Signature;
6
7
use Yiisoft\Yii\AuthClient\Exception\NotSupportedException;
8
9
use function function_exists;
10
11
/**
12
 * HmacSha represents 'HMAC SHA' signature method.
13
 *
14
 * > **Note:** This class requires PHP "Hash" extension(<https://php.net/manual/en/book.hash.php>).
15
 */
16
final class HmacSha extends Signature
17
{
18
    /**
19
     * @var string hash algorithm, e.g. `sha1`, `sha256` and so on.
20
     *
21
     * @link https://php.net/manual/ru/function.hash-algos.php
22
     */
23
    private string $algorithm;
24
25 3
    public function __construct(string $algorithm)
26
    {
27 3
        if (!function_exists('hash_hmac')) {
28
            throw new NotSupportedException('PHP "Hash" extension is required.');
29
        }
30
31 3
        $this->algorithm = $algorithm;
32
    }
33
34 2
    public function getName(): string
35
    {
36 2
        return 'HMAC-' . strtoupper($this->algorithm);
37
    }
38
39 3
    public function generateSignature(string $baseString, string $key): string
40
    {
41 3
        return base64_encode(hash_hmac($this->algorithm, $baseString, $key, true));
42
    }
43
}
44