spatie/crypto
Generate RSA key pairs and encrypt/decrypt (and sign/verify) data using private/public keys in PHP. Provides simple wrappers around OpenSSL for better DX, with support for loading keys from files and writing generated keys to disk.
storage/app/keys/) or environment variables, aligning with Laravel’s conventions for sensitive data.encrypt() (symmetric) or integrate with packages like spatie/laravel-encryption for hybrid approaches.openssl extension (enabled by default in most Laravel deployments). Critical: Verify openssl is available in staging/production.encrypted column or a custom table).| Risk Area | Mitigation Strategy |
|---|---|
| Key Compromise | Store private keys in storage/app/keys/ with strict file permissions (e.g., chmod 600). Use Laravel’s filesystem disk for encryption. |
| OpenSSL Misconfig | Test with openssl_get_error() and log failures. Fallback to a symmetric cipher (e.g., openssl_encrypt) if RSA is unavailable. |
| Key Size Limitations | Defaults to 2048-bit RSA. For high-security needs, configure via KeyPair::generate(4096) but expect slower performance. |
| Serialization | Encrypted data is base64-encoded strings. Ensure downstream systems (e.g., databases, APIs) can handle binary-safe storage. |
| Nonce/IV Handling | Uses OpenSSL’s defaults. For custom security policies, extend the KeyPair class. |
key_revocations table).KeyPair::generate(4096) if needed.libsodium alternatives.config/crypto.php for key paths).encrypt()/decrypt() globally (e.g., Crypto::encrypt($data)).php artisan crypto:generate for key rotation.TEXT columns (PostgreSQL/MySQL support binary-safe base64).encrypt() for metadata if mixed modes are needed.spatie/laravel-activitylog to audit decryption events.PrivateKey::fromFile() with keys stored in storage/app/keys/.config/services.php).decrypted_at timestamp to track usage.spatie/crypto, others with Laravel’s encrypt().| Component | Compatibility Notes |
|---|---|
| PHP Version | Tested on PHP 8.1+. Ensure openssl extension is enabled (`php -m |
| Laravel | No version constraints, but test with Laravel 9+ for facades/service providers. |
| Databases | Base64-encoded data works in all SQL databases, but binary fields may be needed for large payloads. |
| Cloud Providers | AWS KMS/GCP KMS: Consider using these instead for HSM-backed keys. |
| Legacy Systems | If integrating with non-PHP systems, expose keys via a key management API. |
php artisan crypto:generate --path=storage/app/keys.config/services.php:
'crypto' => [
'private_key' => storage_path('app/keys/private_key.pem'),
'public_key' => storage_path('app/keys/public_key.pem'),
],
CryptoService facade:
use Spatie\Crypto\Rsa\PrivateKey;
use Spatie\Crypto\Rsa\PublicKey;
class CryptoService {
public function encrypt(string $data): string {
return PrivateKey::fromFile(config('crypto.private_key'))->encrypt($data);
}
}
CryptoMiddleware to verify signatures on incoming requests.valid_until column).KeyPair::generate() and deploy via CI/CD.try-catch blocks around decrypt()).OpenSSL error: Verify openssl extension is loaded.Decryption failed: Check key paths in config or data corruption.CryptoException handler with detailed error logging.openssl rsa -check to validate keys manually.openssl_error_string().PrivateKey as a singleton).KeyPair::generate(2048) to validate hardware.| Failure Scenario | Impact | Mitigation Strategy |
|---|---|---|
| Private Key Leak | Data breach | Rotate keys immediately; audit all encrypted data. |
| OpenSSL Extension Disabled | Encryption fails | Fallback to openssl_encrypt (symmetric) or alert ops. |
| Key Corruption | Decryption failures | Store keys |
How can I help you explore Laravel packages today?