Passed
Push — master ( d759f2...bf67e8 )
by Milad
02:12
created

EdDsaSigner::kid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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