- How do I install `spomky-labs/php-aes-gcm` in a Laravel project?
- Run `composer require spomky-labs/php-aes-gcm` in your project root. Ensure PHP 7.2+ and OpenSSL are enabled. No additional Laravel-specific setup is required, though you’ll need to bind the library to the service container or create a facade for convenience.
- Can I use this library to replace Laravel’s built-in `encrypt()` method?
- Yes, but be aware that AES-GCM (this library) and Laravel’s default CBC mode are incompatible. If migrating, you’ll need to re-encrypt existing data. Use this library for new encryption needs where GCM’s performance and integrity checks are critical.
- What Laravel versions does this package support?
- The library works with Laravel 7+ and PHP 7.2+. For older Laravel versions (e.g., 5.x), you may need to polyfill dependencies like `sodium_compat` or upgrade PHP to meet the library’s requirements.
- How do I handle nonces (IVs) in AES-GCM for Laravel applications?
- The library generates unique nonces per encryption, but you must ensure nonces are never reused. For distributed systems, consider using UUIDs or timestamps as nonces. Store them securely alongside encrypted data (e.g., in a database column or cache).
- Is this library suitable for encrypting database fields in Laravel Eloquent models?
- Yes, but implement encryption/decryption logic in `getAttribute()` and `setAttribute()` or use model observers. Avoid encrypting entire fields blindly—designate specific columns (e.g., `api_token`, `credit_card`) and document the schema changes.
- How do I integrate this with Laravel’s service container?
- Bind the library in a service provider’s `register()` method, e.g., `app->bind(AesGcm::class, function () { return new AesGcm('your-key-here'); });`. For key management, inject the key via environment variables or a dedicated key management service.
- What are the performance implications compared to Laravel’s default encryption?
- AES-GCM is generally faster than CBC (Laravel’s default) for most use cases, especially with hardware acceleration. Benchmark your specific workload, but expect lower CPU overhead and better throughput for bulk operations like API payloads or batch processing.
- How do I handle decryption failures (e.g., corrupted data or wrong keys)?
- Wrap decryption calls in a `try-catch` block to catch `CryptographicException`. Log failures with context (e.g., ciphertext snippet, timestamp) and implement a fallback strategy, such as alerting admins or using a backup key for critical data.
- Can I use this library for encrypting API request/response bodies in Laravel?
- Yes, but design a middleware or resource transformer to encrypt/decrypt payloads. Avoid encrypting entire requests—target sensitive fields (e.g., PII, tokens) or use it for end-to-end encryption between microservices. Document the encryption scheme for API consumers.
- What alternatives exist for authenticated encryption in Laravel?
- For Laravel, consider `libsodium` (via `paragonie/sodium_compat`) for a modern API, though it’s heavier. For simplicity, Laravel’s `encrypt()` suffices if you don’t need GCM’s performance or integrity checks. This library is ideal if you prioritize AES-GCM’s speed and security.