1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MesCryptoBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mes\Security\CryptoBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Mes\Security\CryptoBundle\Encryption; |
15
|
|
|
use Mes\Security\CryptoBundle\EncryptionInterface; |
16
|
|
|
use Mes\Security\CryptoBundle\KeyGenerator\KeyGenerator; |
17
|
|
|
use Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface; |
18
|
|
|
use Mes\Security\CryptoBundle\Model\KeyInterface; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class EncryptionTest. |
23
|
|
|
*/ |
24
|
|
|
class EncryptionTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EncryptionInterface |
28
|
|
|
*/ |
29
|
|
|
private $encryption; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var KeyGeneratorInterface |
33
|
|
|
*/ |
34
|
|
|
private $generator; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->encryption = new Encryption(); |
39
|
|
|
$this->generator = new KeyGenerator(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function tearDown() |
43
|
|
|
{ |
44
|
|
|
$this->encryption = null; |
45
|
|
|
$this->generator = null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function testEncryptWithKeyEncryptsPlaintext() |
52
|
|
|
{ |
53
|
|
|
$key = $this->generator->generate(); |
54
|
|
|
$plaintext = 'The quick brown fox jumps over the lazy dog'; |
55
|
|
|
$ciphertext = $this->encryption->encryptWithKey($plaintext, $key); |
56
|
|
|
|
57
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
58
|
|
|
|
59
|
|
|
return array( |
60
|
|
|
'ciphertext' => $ciphertext, |
61
|
|
|
'key_encoded' => $key->getEncoded(), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
67
|
|
|
* |
68
|
|
|
* @param $args |
69
|
|
|
*/ |
70
|
|
|
public function testDecryptWithKeyDecryptsCiphertext($args) |
71
|
|
|
{ |
72
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded']); |
73
|
|
|
$plaintext = $this->encryption->decryptWithKey($args['ciphertext'], $key); |
74
|
|
|
|
75
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $plaintext); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
80
|
|
|
* |
81
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
82
|
|
|
* |
83
|
|
|
* @param $args |
84
|
|
|
*/ |
85
|
|
|
public function testDecryptThrowsExceptionBecauseCiphertextIsCorrupted($args) |
86
|
|
|
{ |
87
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded']); |
88
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'].'{FakeString}', $key); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintext |
93
|
|
|
* |
94
|
|
|
* @expectedException \Defuse\Crypto\Exception\BadFormatException |
95
|
|
|
* |
96
|
|
|
* @param $args |
97
|
|
|
*/ |
98
|
|
|
public function testDecryptWithKeyThrowsExceptionBecauseKeyIsCorrupted($args) |
99
|
|
|
{ |
100
|
|
|
$key = $this->generator->generateFromAscii($args['key_encoded'].'{FakeString}'); |
101
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function testEncryptWithKeyEncryptsPlaintextWithPassword() |
108
|
|
|
{ |
109
|
|
|
$key = $this->generator->generate('ThisIsASecretPassword'); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\KeyProtectedByPassword', $key->getRawKey()); |
112
|
|
|
$this->assertSame('ThisIsASecretPassword', $key->getSecret()); |
113
|
|
|
|
114
|
|
|
$plaintext = 'The quick brown fox jumps over the lazy dog'; |
115
|
|
|
$ciphertext = $this->encryption->encryptWithKey($plaintext, $key); |
116
|
|
|
|
117
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\Key', $key->getRawKey()); |
118
|
|
|
$this->assertTrue(ctype_print($ciphertext), 'is printable'); |
119
|
|
|
$this->assertTrue(ctype_print($key->getEncoded()), 'is printable'); |
120
|
|
|
|
121
|
|
|
return array( |
122
|
|
|
'ciphertext' => $ciphertext, |
123
|
|
|
'key_encoded' => $key->getEncoded(), |
124
|
|
|
'secret' => $key->getSecret(), |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
130
|
|
|
* |
131
|
|
|
* @param $args |
132
|
|
|
*/ |
133
|
|
|
public function testDecryptWithKeyDecryptsCiphertextWithPassword($args) |
134
|
|
|
{ |
135
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret']); |
136
|
|
|
|
137
|
|
|
$this->assertInstanceOf('\Defuse\Crypto\KeyProtectedByPassword', $keyFromAscii->getRawKey()); |
138
|
|
|
$this->assertSame($args['secret'], $keyFromAscii->getSecret()); |
139
|
|
|
|
140
|
|
|
$plaintext = $this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
141
|
|
|
|
142
|
|
|
$this->assertSame('The quick brown fox jumps over the lazy dog', $plaintext); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
147
|
|
|
* |
148
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
149
|
|
|
* |
150
|
|
|
* @param $args |
151
|
|
|
*/ |
152
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseSecretIsCorrupted($args) |
153
|
|
|
{ |
154
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret'].'{FakeString}'); |
155
|
|
|
|
156
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
161
|
|
|
* |
162
|
|
|
* @expectedException \Defuse\Crypto\Exception\BadFormatException |
163
|
|
|
* |
164
|
|
|
* @param $args |
165
|
|
|
*/ |
166
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseKeyIsCorrupted($args) |
167
|
|
|
{ |
168
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'].'{FakeString}', $args['secret']); |
169
|
|
|
|
170
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'], $keyFromAscii); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @depends testEncryptWithKeyEncryptsPlaintextWithPassword |
175
|
|
|
* |
176
|
|
|
* @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
177
|
|
|
* |
178
|
|
|
* @param $args |
179
|
|
|
*/ |
180
|
|
|
public function testDecryptWithKeyThrowsExceptionWithCiphertextWithPasswordBecauseCiphertextIsCorrupted($args) |
181
|
|
|
{ |
182
|
|
|
$keyFromAscii = $this->generator->generateFromAscii($args['key_encoded'], $args['secret']); |
183
|
|
|
|
184
|
|
|
$this->encryption->decryptWithKey($args['ciphertext'].'{FakeString}', $keyFromAscii); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function testEncryptWithKeyFileEncryptsFile() |
191
|
|
|
{ |
192
|
|
|
/** @var KeyInterface $key */ |
193
|
|
|
$key = $this->generator->generate('CryptoSecret'); |
194
|
|
|
|
195
|
|
|
// Create file to encrypt. |
196
|
|
|
$tmpfname = tempnam(__DIR__, 'CRYPTO_'); |
197
|
|
|
$plainContent = "Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate."; |
198
|
|
|
$handle = fopen($tmpfname, 'w'); |
199
|
|
|
fwrite($handle, $plainContent); |
200
|
|
|
fclose($handle); |
201
|
|
|
|
202
|
|
|
$filename = md5(uniqid()); |
203
|
|
|
$encryptedFilename = __DIR__."/ENCRYPTED_$filename.crypto"; |
204
|
|
|
|
205
|
|
|
$this->encryption->encryptFileWithKey($tmpfname, $encryptedFilename, $key); |
206
|
|
|
|
207
|
|
|
$this->assertFileExists($encryptedFilename, sprintf('%s file must exists', $encryptedFilename)); |
208
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($encryptedFilename))->getSize()); |
209
|
|
|
|
210
|
|
|
unlink($tmpfname); |
211
|
|
|
|
212
|
|
|
return array( |
213
|
|
|
'key' => $key->getEncoded(), |
214
|
|
|
'secret' => $key->getSecret(), |
215
|
|
|
'encryptedFile' => $encryptedFilename, |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @depends testEncryptWithKeyFileEncryptsFile |
221
|
|
|
* |
222
|
|
|
* @param $args |
223
|
|
|
*/ |
224
|
|
|
public function testDecryptWithKeyFileDecryptsEncryptedFile($args) |
225
|
|
|
{ |
226
|
|
|
/** @var KeyInterface $key */ |
227
|
|
|
$key = $this->generator->generateFromAscii($args['key'], $args['secret']); |
228
|
|
|
|
229
|
|
|
$tmpDecryptedFile = tempnam(__DIR__, '_CRYPTO'); |
230
|
|
|
|
231
|
|
|
$this->encryption->decryptFileWithKey($args['encryptedFile'], $tmpDecryptedFile, $key); |
232
|
|
|
|
233
|
|
|
$this->assertFileExists($tmpDecryptedFile); |
234
|
|
|
$this->assertGreaterThan(0, (new \SplFileInfo($tmpDecryptedFile))->getSize()); |
235
|
|
|
$this->assertContains("Dinanzi a me non fuor cose create se non etterne, e io etterno duro. Lasciate ogni speranza, voi ch'intrate.", file_get_contents($tmpDecryptedFile)); |
236
|
|
|
|
237
|
|
|
unlink($tmpDecryptedFile); |
238
|
|
|
unlink($args['encryptedFile']); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|