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

EdDsaSigner   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 43
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A getPrivateKey() 0 3 1
A sign() 0 11 3
A __construct() 0 4 1
A kid() 0 3 1
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