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, 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))); + } +}