-
Notifications
You must be signed in to change notification settings - Fork 108
/
AsymmetricSecretKey.php
147 lines (134 loc) · 3.76 KB
/
AsymmetricSecretKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace ParagonIE\Paseto\Keys;
use ParagonIE\ConstantTime\Base64UrlSafe;
use ParagonIE\ConstantTime\Binary;
use ParagonIE\Paseto\{
SendingKey,
ProtocolInterface
};
use ParagonIE\Paseto\Protocol\{
Version1,
Version2
};
/**
* Class AsymmetricSecretKey
* @package ParagonIE\Paseto\Keys
*/
class AsymmetricSecretKey implements SendingKey
{
/** @var string $key */
protected $key;
/** @var ProtocolInterface $protocol */
protected $protocol;
/**
* AsymmetricSecretKey constructor.
*
* @param string $keyData
* @param ProtocolInterface $protocol
* @throws \Exception
* @throws \TypeError
*/
public function __construct(
string $keyData,
ProtocolInterface $protocol = null
) {
$protocol = $protocol ?? new Version2;
if (\hash_equals($protocol::header(), Version2::HEADER)) {
$len = Binary::safeStrlen($keyData);
if ($len === SODIUM_CRYPTO_SIGN_KEYPAIRBYTES) {
$keyData = Binary::safeSubstr($keyData, 0, 64);
} elseif ($len !== SODIUM_CRYPTO_SIGN_SECRETKEYBYTES) {
if ($len !== SODIUM_CRYPTO_SIGN_SEEDBYTES) {
throw new \Exception(
'Secret keys must be 32 or 64 bytes long; ' . $len . ' given.'
);
}
$keypair = \sodium_crypto_sign_seed_keypair($keyData);
$keyData = Binary::safeSubstr($keypair, 0, 64);
}
}
$this->key = $keyData;
$this->protocol = $protocol;
}
/**
* @param ProtocolInterface $protocol
* @return self
* @throws \Exception
* @throws \TypeError
*/
public static function generate(ProtocolInterface $protocol = null): self
{
$protocol = $protocol ?? new Version2;
if (\hash_equals($protocol::header(), Version1::HEADER)) {
$rsa = Version1::getRsa();
/** @var array<string, string> $keypair */
$keypair = $rsa->createKey(2048);
return new self($keypair['privatekey']);
}
return new self(
\sodium_crypto_sign_secretkey(
\sodium_crypto_sign_keypair()
)
);
}
/**
* @return string
* @throws \TypeError
*/
public function encode(): string
{
return Base64UrlSafe::encodeUnpadded($this->key);
}
/**
* @param string $encoded
* @return self
* @throws \Exception
* @throws \TypeError
*/
public static function fromEncodedString(string $encoded): self
{
$decoded = Base64UrlSafe::decode($encoded);
return new self($decoded);
}
/**
* @return ProtocolInterface
*/
public function getProtocol(): ProtocolInterface
{
return $this->protocol;
}
/**
* @return AsymmetricPublicKey
* @throws \Exception
* @throws \TypeError
*/
public function getPublicKey(): AsymmetricPublicKey
{
switch ($this->protocol::header()) {
case Version1::HEADER:
return new AsymmetricPublicKey(
Version1::RsaGetPublicKey($this->key),
new Version1
);
default:
return new AsymmetricPublicKey(
\sodium_crypto_sign_publickey_from_secretkey($this->key)
);
}
}
/**
* @return string
*/
public function raw()
{
return $this->key;
}
/**
* @return array
*/
public function __debugInfo()
{
return [];
}
}