-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathEncryptionSecretKey.php
67 lines (64 loc) · 1.92 KB
/
EncryptionSecretKey.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
<?php
declare(strict_types=1);
namespace ParagonIE\Halite\Asymmetric;
use ParagonIE\ConstantTime\Binary;
use ParagonIE\Halite\Alerts\InvalidKey;
use ParagonIE\HiddenString\HiddenString;
use SodiumException;
use TypeError;
use const SODIUM_CRYPTO_BOX_SECRETKEYBYTES;
use function
sodium_crypto_box_publickey_from_secretkey,
sprintf;
/**
* Class EncryptionSecretKey
* @package ParagonIE\Halite\Asymmetric
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://www.mozilla.org/en-US/MPL/2.0/.
*/
final class EncryptionSecretKey extends SecretKey
{
/**
* EncryptionSecretKey constructor.
* @param HiddenString $keyMaterial - The actual key data
* @throws InvalidKey
* @throws TypeError
*/
public function __construct(
#[\SensitiveParameter]
HiddenString $keyMaterial,
?HiddenString $pk = null
) {
if (Binary::safeStrlen($keyMaterial->getString()) !== SODIUM_CRYPTO_BOX_SECRETKEYBYTES) {
throw new InvalidKey(
sprintf(
'Encryption secret key must be CRYPTO_BOX_SECRETKEYBYTES (%d) bytes long',
SODIUM_CRYPTO_BOX_SECRETKEYBYTES
)
);
}
parent::__construct($keyMaterial, $pk);
}
/**
* See the appropriate derived class.
*
* @return EncryptionPublicKey
*
* @throws InvalidKey
* @throws TypeError
* @throws SodiumException
*/
public function derivePublicKey(): EncryptionPublicKey
{
if (is_null($this->cachedPublicKey)) {
$this->cachedPublicKey = sodium_crypto_box_publickey_from_secretkey(
$this->getRawKeyMaterial()
);
}
return new EncryptionPublicKey(
new HiddenString($this->cachedPublicKey)
);
}
}