HmacSha::generateSignature()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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