Installation:
composer require b3da/eos-bundle "dev-master"
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
B3da\EasyOpenSslBundle\B3daEasyOpenSslBundle::class => ['all' => true],
Configuration:
Add to config/packages/eos.yaml (Symfony 4+) or config.yml (Symfony 3):
eos_enc_method: aes-256-cbc # Default encryption method
First Use Case: Inject the service in a controller or command:
use B3da\EasyOpenSslBundle\Service\EasyOpenSslService;
class MyController extends AbstractController {
public function __construct(private EasyOpenSslService $eos) {}
public function encryptMessage() {
$client = new Client(); // Your custom Client entity
$this->eos->generateKeyPairForClient($client);
$encrypted = $this->eos->encrypt($client, 'Sensitive data');
return new Response($encrypted);
}
}
Key Management:
$client = new Client();
$this->eos->generateKeyPairForClient($client); // Auto-persists keys
$publicKey = $this->eos->exportPublicKey($client);
$this->eos->importPublicKey($anotherClient, $publicKey);
Encryption/Decryption:
$encrypted = $this->eos->encrypt($senderClient, 'Secret message');
$decrypted = $this->eos->decrypt($encrypted, $recipientClient);
API Integration (Optional):
Enable routes in config/routes.yaml:
b3da_easy_open_ssl:
resource: "@B3daEasyOpenSslBundle/Resources/config/routing.yml"
prefix: /api/eos
Use endpoints for:
/api/eos/client/create (POST)/api/eos/msg/encrypt/{clientId}/{data} (GET)B3da\EasyOpenSslBundle\Entity\Client and Message to store keys/messages in your DB schema.EasyOpenSslService in controllers/commands.generateKeyPairForClient returns false on failure).DataTransformer:
$form->add('encryptedData', TextType::class, [
'transformer' => new EncryptionTransformer($this->eos)
]);
Key Persistence:
Client entity has @ORM\Column for publicKey/privateKey fields.flush() after generateKeyPairForClient():
$this->eos->generateKeyPairForClient($client);
$this->entityManager->flush();
Encryption Method:
aes-256-cbc) may not suit all use cases. Override in config:
eos_enc_method: aes-128-gcm # Requires OpenSSL 1.0.1+
openssl_get_cipher_methods() before use.API Endpoints:
/encrypt/{data} do not validate input length. Large payloads may cause timeouts.Key Rotation:
rotateKeys() method in your Client entity:
public function rotateKeys(EasyOpenSslService $eos) {
$eos->generateKeyPairForClient($this);
$this->oldPublicKey = $this->publicKey; // Backup old key
}
OpenSSL Errors:
try {
$this->eos->encrypt($client, 'data');
} catch (\RuntimeException $e) {
// Log OpenSSL errors (e.g., "error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt")
}
openssl.cipher-sections is enabled in php.ini.Key Format Issues:
if (!openssl_pkey_get_public($client->publicKey)) {
throw new \RuntimeException('Invalid public key format');
}
Performance:
$cacheKey = 'eos:keys:' . $client->id;
$keys = $this->cache->get($cacheKey);
if (!$keys) {
$keys = $this->eos->getKeys($client);
$this->cache->set($cacheKey, $keys, 3600);
}
Custom Ciphers:
B3da\EasyOpenSslBundle\Service\EasyOpenSslService to add methods like:
public function signData(Client $client, string $data): string {
return base64_encode(openssl_sign($data, $signature, $client->privateKey));
}
Event Listeners:
// config/services.yaml
B3da\EasyOpenSslBundle\EventListener\KeyGenerationListener:
tags:
- { name: kernel.event_listener, event: eos.key_generated, method: onKeyGenerated }
Doctrine Lifecycle Callbacks:
Client is persisted:
use B3da\EasyOpenSslBundle\Service\EasyOpenSslService;
class Client {
public function __construct(private EasyOpenSslService $eos) {}
public function prePersist() {
$this->eos->generateKeyPairForClient($this);
}
}
How can I help you explore Laravel packages today?