Skip to content

Commit

Permalink
Fix encryption migration and test coverage (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov authored Sep 24, 2024
2 parents 78f9bb4 + 474e1ec commit f0122d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/migrations/20240922.193320_0_re_encrypt_columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions tests/Feature/Service/Encrypter/AES256ECBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Service\Encrypter;

use App\Service\Encrypter\AES256ECB;
use Spiral\Encrypter\Exception\EncrypterException;
use Tests\Fixtures;
use Tests\TestCase;

class AES256ECBTest extends TestCase
{
public function testEncryptDecrypt(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256ECB();
$encrypted = $encrypter->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)));
}
}
39 changes: 39 additions & 0 deletions tests/Feature/Service/Encrypter/AES256GCMTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Service\Encrypter;

use App\Service\Encrypter\AES256GCM;
use Spiral\Encrypter\Exception\EncrypterException;
use Tests\Fixtures;
use Tests\TestCase;

class AES256GCMTest extends TestCase
{
public function testEncryptDecrypt(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256GCM();
$encrypted = $encrypter->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)));
}
}

0 comments on commit f0122d5

Please sign in to comment.