EdDsaVerifier   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A verify() 0 12 4
A kid() 0 3 1
A getPublicKey() 0 3 1
A name() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa;
6
7
use MiladRahimi\Jwt\Cryptography\Keys\EdDsaPublicKey;
8
use MiladRahimi\Jwt\Cryptography\Verifier;
9
use MiladRahimi\Jwt\Exceptions\InvalidSignatureException;
10
use RuntimeException;
11
use SodiumException;
12
13
class EdDsaVerifier implements Verifier
14
{
15
    protected static string $name = 'EdDSA';
16
17
    protected EdDsaPublicKey $publicKey;
18
19
    public function __construct(EdDsaPublicKey $publicKey)
20
    {
21
        $this->publicKey = $publicKey;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function verify(string $plain, string $signature): void
28
    {
29
        if (function_exists('sodium_crypto_sign_verify_detached')) {
30
            try {
31
                if (!sodium_crypto_sign_verify_detached($signature, $plain, $this->publicKey->getContent())) {
32
                    throw new InvalidSignatureException('Signature is to verified.');
33
                }
34
            } catch (SodiumException $e) {
35
                throw new InvalidSignatureException('Sodium cannot verify the signature.', 0, $e);
36
            }
37
        } else {
38
            throw new RuntimeException('sodium_crypto_sign_verify_detached function is not available.');
39
        }
40
    }
41
42
    public function name(): string
43
    {
44
        return static::$name;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function kid(): ?string
51
    {
52
        return $this->publicKey->getId();
53
    }
54
55
    public function getPublicKey(): EdDsaPublicKey
56
    {
57
        return $this->publicKey;
58
    }
59
}
60