Installation:
composer require ranabd36/openssl-encryption:1.0.0
The package auto-registers, but verify config/app.php includes:
'providers' => [
OpenSSLEncryption\Providers\OpenSSLEncryptionServiceProvider::class,
],
'aliases' => [
'OpenSSL' => OpenSSLEncryption\OpenSSL::class,
]
Generate Keys:
Run the Artisan command to create a public.pem and private.pem in storage/app/openssl/:
php artisan openssl:key-generate
First Use Case: Encrypt a message with a recipient’s public key:
$encrypted = OpenSSL::encrypt('Sensitive data', 'path/to/recipient_public.pem');
Decrypt with your private key:
$decrypted = OpenSSL::decrypt($encrypted, 'path/to/your_private.pem');
Key Management:
storage/app/openssl/ (or configure via config/openssl.php).openssl:key-generate for new keys; avoid hardcoding keys in code.Encryption/Decryption:
$encrypted = OpenSSL::encrypt($data, 'public_key_path');
// Send $encrypted via API/email.
$decrypted = OpenSSL::decrypt($encryptedData, 'private_key_path');
$signature = OpenSSL::sign($data, 'private_key_path');
$isValid = OpenSSL::verify($data, $signature, 'public_key_path');
Integration with Laravel:
return response()->json(['data' => OpenSSL::encrypt($data, $clientPublicKey)]);
$job->handle(OpenSSL::encrypt($jobData, $recipientKey));
Configuration:
php artisan vendor:publish --provider="OpenSSLEncryption\Providers\OpenSSLEncryptionServiceProvider"
Adjust key_directory, key_prefix, or openssl_config as needed.Key Paths:
'/absolute/path') breaks portability. Use relative paths from storage/ or config.config(['openssl.keys' => [
'user1' => 'storage/app/openssl/user1_public.pem',
]]);
Key Permissions:
chmod 600). OpenSSL fails silently if permissions are too loose.storage/logs/laravel.log for OpenSSL errors like error:0906A068:PEM routines:PEM_do_header:bad password read.Data Size Limits:
Key Rotation:
Error Handling:
try-catch:
try {
$decrypted = OpenSSL::decrypt($data, $keyPath);
} catch (\OpenSSLEncryption\Exceptions\OpenSSLErrorException $e) {
Log::error("Decryption failed: " . $e->getMessage());
// Handle gracefully (e.g., retry with fallback key).
}
Testing:
openssl:key-generate in tests to avoid polluting storage/:
$this->artisan('openssl:key-generate')->expectsQuestion('Key name', 'test_key')->run();
Performance:
$key = Cache::remember("public_key_{$userId}", now()->addHours(1), fn() => file_get_contents($keyPath));
Security:
.gitignore).OPENSSL_PRIVATE_KEY_PATH=storage/app/openssl/private.pem
$path = env('OPENSSL_PRIVATE_KEY_PATH');
Extending:
OpenSSL facade to add logging or metrics:
class CustomOpenSSL extends \OpenSSLEncryption\OpenSSL {
public function encrypt($data, $publicKey) {
Log::debug("Encrypting data for key: " . basename($publicKey));
return parent::encrypt($data, $publicKey);
}
}
Bind it in AppServiceProvider:
$this->app->bind(\OpenSSLEncryption\OpenSSL::class, CustomOpenSSL::class);
Debugging:
config/openssl.php:
'openssl_config' => [
'config' => '/path/to/openssl.cnf',
'debug' => true, // Adds verbose output to logs
]
How can I help you explore Laravel packages today?