Skip to content

Commit 2a78cd1

Browse files
authored
Merge pull request #65 from David-Dadon/fix/aes-gcm
Fix AES GCM encryptor compilation crash, and database command for aes-gcm
2 parents e991815 + c3a6038 commit 2a78cd1

3 files changed

Lines changed: 191 additions & 5 deletions

File tree

src/Command/EncryptDatabaseCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ private function decryptTable(string $tableName, array $fieldArray, string $dire
102102
}
103103

104104
if ('encrypt' === $direction) {
105-
$newValue = $this->encryptor->encrypt($value);
105+
$newValue = $this->encryptor->encrypt($value, $fieldName);
106106
} else {
107-
$newValue = $this->encryptor->decrypt($value);
107+
$newValue = $this->encryptor->decrypt($value, $fieldName);
108108
}
109109

110110
$decryptedFields[$fieldName] = $newValue;

src/Encryptors/AesGcmEncryptor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use SpecShaper\EncryptBundle\Exception\EncryptException;
88
use SpecShaper\EncryptBundle\EventListener\DoctrineEncryptListenerInterface;
99
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10-
use Symfony\Contracts\Service\Attribute\Required;
1110

1211
class AesGcmEncryptor implements EncryptorInterface
1312
{
@@ -48,7 +47,7 @@ public function setDefaultAssociatedData(?string $defaultAssociatedData): void
4847
/**
4948
* @throws \Exception
5049
*/
51-
public function encrypt(?string $data, ?string $columnName): ?string
50+
public function encrypt(?string $data, ?string $columnName = null): ?string
5251
{
5352
if (is_null($data)) {
5453
return null;
@@ -84,7 +83,7 @@ public function encrypt(?string $data, ?string $columnName): ?string
8483
/**
8584
* @throws \Exception
8685
*/
87-
public function decrypt(?string $data, ?string $columnName): ?string
86+
public function decrypt(?string $data, ?string $columnName = null): ?string
8887
{
8988
if (is_null($data)) {
9089
return null;
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpecShaper\EncryptBundle\tests\Unit\Encryptors;
6+
7+
use SpecShaper\EncryptBundle\Encryptors\AesGcmEncryptor;
8+
use Symfony\Component\EventDispatcher\EventDispatcher;
9+
10+
/**
11+
* @author David Dadon <david.dadon@neftys.fr>
12+
*/
13+
class AesGcmEncryptorTest extends \PHPUnit\Framework\TestCase
14+
{
15+
private const TEST_KEY = 'YBmNcBGfrZoayB+V254wdYa/abvxSUWJsjCtlMc1tRI=';
16+
17+
public function testEncryptException(): void
18+
{
19+
$this->expectException(\TypeError::class);
20+
21+
// Given
22+
$object = new \stdClass();
23+
$object->test = 'Test';
24+
25+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
26+
$encryptor->setSecretKey(self::TEST_KEY);
27+
28+
// When
29+
$encryptor->encrypt($object);
30+
}
31+
32+
public function testEncryptNullReturnsNull(): void
33+
{
34+
// Given
35+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
36+
$encryptor->setSecretKey(self::TEST_KEY);
37+
$encryptor->setDefaultAssociatedData(null);
38+
39+
// When
40+
$result = $encryptor->encrypt(null);
41+
42+
// Then
43+
$this->assertTrue($result === null);
44+
}
45+
46+
public function testEncryptOnlySuffix(): void
47+
{
48+
// Given
49+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
50+
$encryptor->setSecretKey(self::TEST_KEY);
51+
$encryptor->setDefaultAssociatedData(null);
52+
53+
// When
54+
$result = $encryptor->encrypt('<ENC>');
55+
56+
// Then
57+
$this->assertTrue($result === '<ENC>');
58+
}
59+
60+
public function testEncryptAndDecryptReturnsOriginalValue(): void
61+
{
62+
// Given
63+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
64+
$encryptor->setSecretKey(self::TEST_KEY);
65+
$encryptor->setDefaultAssociatedData(null);
66+
$value = 'Honey, where are my pants?';
67+
68+
// When
69+
$encryptedValue = $encryptor->encrypt($value);
70+
71+
// Then
72+
$this->assertTrue($encryptedValue !== $value);
73+
74+
// When
75+
$decrypted = $encryptor->decrypt($encryptedValue);
76+
77+
// Then
78+
$this->assertTrue($decrypted === $value);
79+
}
80+
81+
/**
82+
* @throws \Exception
83+
*/
84+
public function testDecryptException(): void
85+
{
86+
$this->expectException(\TypeError::class);
87+
// or for PHPUnit < 5.2
88+
// $this->setExpectedException(InvalidArgumentException::class);
89+
90+
// Given
91+
$object = new \stdClass();
92+
$object->test = 'Test';
93+
94+
// ...and then add your test code that generates the exception
95+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
96+
$encryptor->setSecretKey(self::TEST_KEY);
97+
98+
// When
99+
$encryptor->decrypt($object);
100+
}
101+
102+
/**
103+
* @throws \Exception
104+
*/
105+
public function testDecryptNullReturnsNull(): void
106+
{
107+
// Given
108+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
109+
$encryptor->setSecretKey(self::TEST_KEY);
110+
$encryptor->setDefaultAssociatedData(null);
111+
112+
// When
113+
$result = $encryptor->decrypt(null);
114+
115+
// Then
116+
$this->assertTrue($result === null);
117+
}
118+
119+
public function testDecryptWithoutSuffixReturnsOrignialValue(): void
120+
{
121+
// Given
122+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
123+
$encryptor->setSecretKey(self::TEST_KEY);
124+
$encryptor->setDefaultAssociatedData(null);
125+
126+
// When
127+
$result = $encryptor->decrypt('Test value <ENC');
128+
129+
// Then
130+
$this->assertTrue($result === 'Test value <ENC');
131+
}
132+
133+
public function testDecryptReturnsExpectedValue(): void
134+
{
135+
// Given
136+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
137+
$encryptor->setSecretKey(self::TEST_KEY);
138+
$encryptor->setDefaultAssociatedData(null);
139+
140+
// When
141+
$decrypted = $encryptor->decrypt('g5wofClWz/wG44umXsUw+wAHQiqhTmo0eGIcODXvV6bjU3xDR8paa7wzu8EoJh0xGOJPD+Ue<ENC>');
142+
143+
// Then
144+
$this->assertTrue($decrypted === 'Honey, where are my pants?');
145+
}
146+
147+
public function testEncryptWithColumnName(): void
148+
{
149+
// Given
150+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
151+
$encryptor->setSecretKey(self::TEST_KEY);
152+
$value = 'Honey, where are my pants?';
153+
154+
// When
155+
$encryptedValue = $encryptor->encrypt($value, 'columnName');
156+
157+
// Then
158+
$this->assertFalse($encryptedValue === $value);
159+
160+
// When
161+
$decrypted = $encryptor->decrypt($encryptedValue, 'columnName');
162+
163+
// Then
164+
$this->assertTrue($decrypted === $value);
165+
}
166+
167+
public function testEncryptWithDefaultAssociatedData(): void
168+
{
169+
// Given
170+
$encryptor = new AesGcmEncryptor(new EventDispatcher());
171+
$encryptor->setSecretKey(self::TEST_KEY);
172+
$encryptor->setDefaultAssociatedData('DefaultAssociatedData');
173+
$value = 'Honey, where are my pants?';
174+
175+
// When
176+
$encryptedValue = $encryptor->encrypt($value);
177+
178+
// Then
179+
$this->assertFalse($encryptedValue === $value);
180+
181+
// When
182+
$decrypted = $encryptor->decrypt($encryptedValue);
183+
184+
// Then
185+
$this->assertTrue($decrypted === $value);
186+
}
187+
}

0 commit comments

Comments
 (0)