PHP code example of precision-soft / symfony-doctrine-encrypt
1. Go to this page and download the library: Download precision-soft/symfony-doctrine-encrypt library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
precision-soft / symfony-doctrine-encrypt example snippets
use PrecisionSoft\Doctrine\Encrypt\PrecisionSoftDoctrineEncryptBundle;
return [
PrecisionSoftDoctrineEncryptBundle::class => ['all' => true],
];
declare(strict_types=1);
use PrecisionSoft\Doctrine\Encrypt\Encryptor\Aes256Encryptor;
use PrecisionSoft\Doctrine\Encrypt\Encryptor\Aes256FixedEncryptor;
use Symfony\Config\PrecisionSoftDoctrineEncryptConfig;
return static function (PrecisionSoftDoctrineEncryptConfig $precisionSoftDoctrineEncryptConfig): void {
$precisionSoftDoctrineEncryptConfig->salt('%env(APP_ENCRYPTION_SALT)%');
$precisionSoftDoctrineEncryptConfig->encryptors([
Aes256Encryptor::class,
Aes256FixedEncryptor::class,
]);
$precisionSoftDoctrineEncryptConfig->enabledTypes(['encryptedAes256', 'encryptedAes256fixed']);
};
declare(strict_types=1);
use Symfony\Config\PrecisionSoftDoctrineEncryptConfig;
return static function (PrecisionSoftDoctrineEncryptConfig $precisionSoftDoctrineEncryptConfig): void {
$precisionSoftDoctrineEncryptConfig->salts([
'v1' => '%env(APP_ENCRYPTION_SALT_V1)%',
'v2' => '%env(APP_ENCRYPTION_SALT_V2)%',
]);
$precisionSoftDoctrineEncryptConfig->currentSaltVersion('v2');
};
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Customer
{
#[ORM\Column(type: 'encryptedAes256')]
private string $name;
#[ORM\Column(type: 'encryptedAes256fixed')]
private string $email;
}
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Customer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use PrecisionSoft\Doctrine\Encrypt\Service\EntityService;
class CustomerRepository extends ServiceEntityRepository
{
public function __construct(
ManagerRegistry $managerRegistry,
private readonly EntityService $entityService,
) {
parent::__construct($managerRegistry, Customer::class);
}
public function findByEmail(string $email): ?Customer
{
$queryBuilder = $this->createQueryBuilder('c')
->where('c.email = :email');
$this->entityService->setEncryptedParameter($queryBuilder, 'email', Customer::class, 'email', $email);
return $queryBuilder->getQuery()->getOneOrNullResult();
}
}
declare(strict_types=1);
use Symfony\Config\PrecisionSoftDoctrineEncryptConfig;
return static function (PrecisionSoftDoctrineEncryptConfig $precisionSoftDoctrineEncryptConfig): void {
$precisionSoftDoctrineEncryptConfig->salts([
'v1' => '%env(APP_ENCRYPTION_SALT_V1)%',
'v2' => '%env(APP_ENCRYPTION_SALT_V2)%',
]);
$precisionSoftDoctrineEncryptConfig->currentSaltVersion('v2');
};
$precisionSoftDoctrineEncryptConfig->salts(['v2' => '%env(APP_ENCRYPTION_SALT_V2)%']);
$precisionSoftDoctrineEncryptConfig->currentSaltVersion('v2');
declare(strict_types=1);
namespace App\Encryptor;
use PrecisionSoft\Doctrine\Encrypt\Contract\EncryptorInterface;
use PrecisionSoft\Doctrine\Encrypt\Type\Aes256Type;
class MyCustomEncryptor implements EncryptorInterface
{
public function getTypeClass(): string
{
return Aes256Type::class;
}
public function getTypeName(): string
{
return Aes256Type::getFullName();
}
public function encrypt(string $data): string
{
}
public function decrypt(string $data): string
{
}
}
declare(strict_types=1);
namespace App\Encryptor;
use PrecisionSoft\Doctrine\Encrypt\Contract\DeterministicEncryptorInterface;
use PrecisionSoft\Doctrine\Encrypt\Type\Aes256FixedType;
class MyDeterministicEncryptor implements DeterministicEncryptorInterface
{
public function getTypeClass(): string
{
return Aes256FixedType::class;
}
public function getTypeName(): string
{
return Aes256FixedType::getFullName();
}
public function encrypt(string $data): string
{
}
public function decrypt(string $data): string
{
}
}
try {
// ...
} catch (Exception $exception) {
$logger->error($exception->getMessage(), $exception->getContext());
}
shell
php -r "echo base64_encode(random_bytes(48));"
shell
php bin/console precision-soft:doctrine:database:encrypt
shell
php bin/console precision-soft:doctrine:database:decrypt
shell
php bin/console precision-soft:doctrine:database:rotate
shell
php bin/console precision-soft:doctrine:database:encrypt --manager=secondary
shell
php bin/console precision-soft:doctrine:database:rotate
shell
php bin/console precision-soft:doctrine:database:decrypt
shell
php bin/console precision-soft:doctrine:database:encrypt
shell
PHP_VERSION=8.4 ./dc build && PHP_VERSION=8.4 ./dc up -d
shell
./dc exec dev php -d pcov.enabled=1 vendor/bin/simple-phpunit --coverage-text