PHP code example of meritech / encryption-bundle
1. Go to this page and download the library: Download meritech/encryption-bundle 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/ */
meritech / encryption-bundle example snippets
// config/bundles.php
return [
Meritech\EncryptionBundle\EncryptionBundle::class => ['all' => true],
];
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
// Encrypted string (randomized - different ciphertext each time)
#[ORM\Column(type: 'encrypted_string')]
private string $email;
// Encrypted JSON data
#[ORM\Column(type: 'encrypted_json', nullable: true)]
private ?array $preferences = null;
// Deterministic encryption (same plaintext = same ciphertext)
// Use for exact-match searches without blind index
#[ORM\Column(type: 'encrypted_string_deterministic')]
private string $ssn;
}
use Doctrine\ORM\Mapping as ORM;
use Meritech\EncryptionBundle\Attribute\BlindIndex;
#[ORM\Entity]
#[ORM\Index(columns: ['email_index'], name: 'idx_email_blind')]
class User
{
#[ORM\Column(type: 'encrypted_string')]
#[BlindIndex(indexProperty: 'emailIndex', bits: 64)]
private string $email;
// Blind index column - auto-populated on persist/update
#[ORM\Column(type: 'blind_index', length: 16, nullable: true)]
private ?string $emailIndex = null;
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
}
use Meritech\EncryptionBundle\Crypto\BlindIndexer;
class UserRepository extends ServiceEntityRepository
{
public function __construct(
ManagerRegistry $registry,
private readonly BlindIndexer $blindIndexer,
) {
parent::__construct($registry, User::class);
}
public function findByEmail(string $email): ?User
{
// Compute blind index for the search value
$normalized = $this->blindIndexer->normalize($email);
$index = $this->blindIndexer->generate($normalized, 'User.email', 64);
// Query - may return multiple results due to collisions
$candidates = $this->createQueryBuilder('u')
->where('u.emailIndex = :index')
->setParameter('index', $index)
->getQuery()
->getResult();
// Filter false positives by comparing decrypted values
foreach ($candidates as $user) {
if (mb_strtolower($user->getEmail()) === mb_strtolower($email)) {
return $user;
}
}
return null;
}
}
bash
php -r "echo 'base64:' . base64_encode(random_bytes(32)) . PHP_EOL;"