-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryptionTest.php
More file actions
156 lines (113 loc) · 4.52 KB
/
Copy pathEncryptionTest.php
File metadata and controls
156 lines (113 loc) · 4.52 KB
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
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace CodedMonkey\Dirigent\Tests\UnitTests\Encryption;
use CodedMonkey\Dirigent\Encryption\Encryption;
use CodedMonkey\Dirigent\Encryption\EncryptionException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class EncryptionTest extends TestCase
{
public const DATA = 'mamma mia';
protected function tearDown(): void
{
$files = Finder::create()
->files()
->in(__DIR__ . '/keys')
->name('*.key');
foreach ($files as $file) {
new Filesystem()->remove($file->getRealPath());
}
}
public function testSeal(): void
{
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$encryption = new Encryption($privateKey, $publicKey, []);
$sealedData = $encryption->seal(self::DATA);
$this->assertSame(self::DATA, sodium_crypto_box_seal_open(sodium_hex2bin($sealedData), $privateKey));
}
public function testReveal(): void
{
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$encryption = new Encryption($privateKey, $publicKey, []);
$sealedData = sodium_bin2hex(sodium_crypto_box_seal(self::DATA, $publicKey));
$this->assertSame(self::DATA, $encryption->reveal($sealedData));
}
public function testRevealWithRotatedKey(): void
{
$rotatedPrivateKey = sodium_crypto_box_keypair();
$rotatedPublicKey = sodium_crypto_box_publickey($rotatedPrivateKey);
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$encryption = new Encryption($privateKey, $publicKey, [$rotatedPrivateKey]);
$sealedData = sodium_bin2hex(sodium_crypto_box_seal(self::DATA, $rotatedPublicKey));
$this->assertSame(self::DATA, $encryption->reveal($sealedData));
}
public function testRevealFailsWithInvalidPublicKey(): void
{
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey(sodium_crypto_box_keypair());
$encryption = new Encryption($privateKey, $publicKey, []);
$sealedData = $encryption->seal(self::DATA);
$this->expectException(EncryptionException::class);
$this->expectExceptionMessage('Unable to decrypt data');
$encryption->reveal($sealedData);
}
public function testCreateFromHex(): void
{
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$encryption = Encryption::create(
sodium_bin2hex($privateKey),
null,
sodium_bin2hex($publicKey),
null,
[],
[],
);
$this->validateEncryption($encryption);
}
public function testCreateFromFiles(): void
{
$filesystem = new Filesystem();
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$privateKeyPath = __DIR__ . '/keys/private.key';
$publicKeyPath = __DIR__ . '/keys/public.key';
$filesystem->dumpFile($privateKeyPath, sodium_bin2hex($privateKey));
$filesystem->dumpFile($publicKeyPath, sodium_bin2hex($publicKey));
$encryption = Encryption::create(
null,
$privateKeyPath,
null,
$publicKeyPath,
[],
[],
);
$this->validateEncryption($encryption);
}
public function testValidate(): void
{
$this->expectNotToPerformAssertions();
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($privateKey);
$encryption = new Encryption($privateKey, $publicKey, []);
$encryption->validate();
}
public function testValidateFailsWithInvalidPublicKey(): void
{
$privateKey = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey(sodium_crypto_box_keypair());
$this->expectException(EncryptionException::class);
$this->expectExceptionMessage('The encryption key is not valid.');
$encryption = new Encryption($privateKey, $publicKey, []);
$encryption->validate();
}
private function validateEncryption(Encryption $encryption): void
{
$data = $encryption->reveal($encryption->seal(self::DATA));
$this->assertSame(self::DATA, $data);
}
}