CoseHash   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 38
ccs 7
cts 8
cp 0.875
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A hash() 0 3 1
1
<?php
2
3
namespace MadWizard\WebAuthn\Crypto;
4
5
use MadWizard\WebAuthn\Exception\UnsupportedException;
6
7
class CoseHash
8
{
9
    /**
10
     * @var string
11
     */
12
    private $phpAlg;
13
14
    private const MAP = [
15
        CoseAlgorithm::RS1 => 'sha1',
16
        CoseAlgorithm::ES256 => 'sha256',
17
        CoseAlgorithm::ES384 => 'sha384',
18
        CoseAlgorithm::ES512 => 'sha512',
19
        CoseAlgorithm::RS256 => 'sha256',
20
        CoseAlgorithm::RS384 => 'sha384',
21
        CoseAlgorithm::RS512 => 'sha512',
22
    ];
23
24
    /**
25
     * CoseHash constructor.
26
     *
27
     * @param int $algorithm CoseAlgorithm identifier
28
     *
29
     * @see CoseAlgorithm
30
     *
31
     * @throws UnsupportedException
32
     */
33 1
    public function __construct(int $algorithm)
34
    {
35 1
        $phpAlg = self::MAP[$algorithm] ?? null;
36 1
        if ($phpAlg === null) {
37
            throw new UnsupportedException(sprintf('COSE algorithm %d not supported for hashing.', $algorithm));
38
        }
39 1
        $this->phpAlg = $phpAlg;
40 1
    }
41
42 1
    public function hash(string $data): string
43
    {
44 1
        return hash($this->phpAlg, $data, true);
45
    }
46
}
47