EdDsaSigner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa;
6
7
use MiladRahimi\Jwt\Cryptography\Keys\EdDsaPrivateKey;
8
use MiladRahimi\Jwt\Cryptography\Signer;
9
use MiladRahimi\Jwt\Exceptions\SigningException;
10
use RuntimeException;
11
use SodiumException;
12
13
use function function_exists;
14
15
class EdDsaSigner implements Signer
16
{
17
    protected static string $name = 'EdDSA';
18
19
    protected EdDsaPrivateKey $privateKey;
20
21
    public function __construct(EdDsaPrivateKey $privateKey)
22
    {
23
        $this->privateKey = $privateKey;
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function sign(string $message): string
30
    {
31
        if (function_exists('sodium_crypto_sign_detached')) {
32
            try {
33
                return sodium_crypto_sign_detached($message, $this->privateKey->getContent());
34
            } catch (SodiumException $e) {
35
                throw new SigningException("Cannot sign using Sodium extension.", 0, $e);
36
            }
37
        }
38
39
        throw new RuntimeException('The sodium_crypto_sign_detached function is not available.');
40
    }
41
42
    public function name(): string
43
    {
44
        return static::$name;
45
    }
46
47
    public function kid(): ?string
48
    {
49
        return $this->privateKey->getId();
50
    }
51
52
    public function getPrivateKey(): EdDsaPrivateKey
53
    {
54
        return $this->privateKey;
55
    }
56
}
57