CoseHash::hash()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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