From c23107682ad8c4c0e4786bb4b78bd3a1ff3a88f2 Mon Sep 17 00:00:00 2001 From: vokomarov Date: Tue, 24 Sep 2024 22:17:32 +0300 Subject: [PATCH 1/2] Fix migration script for empty values --- app/migrations/20240922.193320_0_re_encrypt_columns.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/migrations/20240922.193320_0_re_encrypt_columns.php b/app/migrations/20240922.193320_0_re_encrypt_columns.php index d769743..d545d90 100644 --- a/app/migrations/20240922.193320_0_re_encrypt_columns.php +++ b/app/migrations/20240922.193320_0_re_encrypt_columns.php @@ -68,6 +68,10 @@ public function down(): void private function convert(string $value): string { + if ($value === '') { + return $value; + } + return $this->encrypter->encrypt( $this->encrypter->decrypt($value, Cipher::AES256ECB), Cipher::AES256GCM, From 474e1ec3f51d394122d96f59bc80bc2c65a22e6e Mon Sep 17 00:00:00 2001 From: vokomarov Date: Tue, 24 Sep 2024 22:35:32 +0300 Subject: [PATCH 2/2] Add encrypter specific tests --- .../Service/Encrypter/AES256ECBTest.php | 39 +++++++++++++++++++ .../Service/Encrypter/AES256GCMTest.php | 39 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/Feature/Service/Encrypter/AES256ECBTest.php create mode 100644 tests/Feature/Service/Encrypter/AES256GCMTest.php diff --git a/tests/Feature/Service/Encrypter/AES256ECBTest.php b/tests/Feature/Service/Encrypter/AES256ECBTest.php new file mode 100644 index 0000000..4e8efa0 --- /dev/null +++ b/tests/Feature/Service/Encrypter/AES256ECBTest.php @@ -0,0 +1,39 @@ +encrypt($message, $key); + + $this->assertNotEmpty($encrypted); + $this->assertNotEquals($message, $encrypted); + $this->assertEquals($message, $encrypter->decrypt($encrypted, $key)); + } + + public function testDecryptError(): void + { + $key = Fixtures::string(); + $message = Fixtures::string(256); + + $encrypter = new AES256ECB(); + $encrypted = $encrypter->encrypt($message, $key); + + $this->expectException(EncrypterException::class); + + $this->assertEquals($message, $encrypter->decrypt($encrypted, $key . Fixtures::string(1))); + } +} diff --git a/tests/Feature/Service/Encrypter/AES256GCMTest.php b/tests/Feature/Service/Encrypter/AES256GCMTest.php new file mode 100644 index 0000000..7cbbff0 --- /dev/null +++ b/tests/Feature/Service/Encrypter/AES256GCMTest.php @@ -0,0 +1,39 @@ +encrypt($message, $key); + + $this->assertNotEmpty($encrypted); + $this->assertNotEquals($message, $encrypted); + $this->assertEquals($message, $encrypter->decrypt($encrypted, $key)); + } + + public function testDecryptError(): void + { + $key = Fixtures::string(); + $message = Fixtures::string(256); + + $encrypter = new AES256GCM(); + $encrypted = $encrypter->encrypt($message, $key); + + $this->expectException(EncrypterException::class); + + $this->assertEquals($message, $encrypter->decrypt($encrypted, $key . Fixtures::string(1))); + } +}