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
|
|
|
|