richardstyles/eloquentencryption
Installation:
composer require richardstyles/eloquentencryption
Publish the config file:
php artisan vendor:publish --provider="RichardStyles\EloquentEncryption\EloquentEncryptionServiceProvider" --tag="config"
Configure Encryption Key:
Edit .env to specify your RSA private key path:
ENCRYPTION_KEY_PATH=/path/to/private_key.pem
Generate a 4096-bit RSA key pair if needed (e.g., using OpenSSL):
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:4096
First Use Case:
Encrypt a model attribute by adding $encrypted to the $fillable array:
use RichardStyles\EloquentEncryption\EloquentEncryption;
class User extends Model
{
use EloquentEncryption;
protected $fillable = ['name', 'email', 'ssn']; // 'ssn' will be encrypted
}
Save a user instance:
$user = new User(['ssn' => '123-45-6789']);
$user->save(); // 'ssn' is automatically encrypted
Encryption Scope:
$encrypted in $fillable to mark fields for encryption.getEncryptedAttributes():
protected function getEncryptedAttributes()
{
return ['ssn', 'credit_card']; // Dynamic list
}
Querying Encrypted Fields:
$users = User::whereRaw("ssn = ?", [Encrypt::encrypt('123-45-6789')])->get();
Key Rotation:
ENCRYPTION_KEY_PATH in .env and re-encrypting data:
php artisan eloquent-encryption:reencrypt
Partial Encryption:
$user->update(['ssn' => '987-65-4321'], ['encrypted' => ['ssn']]);
API Responses:
Automatically decrypt fields before JSON serialization by overriding toArray():
public function toArray()
{
return array_merge(parent::toArray(), [
'ssn' => $this->ssn, // Automatically decrypted
]);
}
Form Requests:
Use Encrypt::decrypt() in request validation:
public function rules()
{
return [
'ssn' => ['required', 'string', function ($attribute, $value, $fail) {
if (!Encrypt::decrypt($value)) {
$fail('Invalid SSN format.');
}
}],
];
}
Testing: Mock the encryption service in tests:
$this->app->instance(Encrypt::class, Mockery::mock(Encrypt::class));
Performance Overhead:
email for login).Key Management:
Database Indexing:
PHP Extensions:
openssl and sodium extensions. Verify with:
php -m | grep -E 'openssl|sodium'
Laravel Caching:
public $cache = false;
Decryption Failures:
chmod 600 private_key.pem).openssl rsa -in private_key.pem -check
Corrupted Data:
$user->ssn = Encrypt::encrypt($user->ssn);
$user->save();
Logs:
Enable debug mode in config/eloquent-encryption.php:
'debug' => env('ENCRYPTION_DEBUG', false),
Custom Encryption: Override the encryption service:
$this->app->bind(Encrypt::class, function () {
return new CustomEncryptService();
});
Attribute Whitelisting: Restrict encryption to specific models by extending the trait:
abstract class EncryptableModel extends Model
{
use EloquentEncryption;
public static function bootEncryptable()
{
static::addGlobalScope(new EncryptableScope);
}
}
Batch Re-encryption: Extend the re-encryption command for large datasets:
php artisan eloquent-encryption:reencrypt --model=User --chunk=1000
Fallback for Missing Keys: Handle missing keys gracefully:
try {
$decrypted = Encrypt::decrypt($encryptedValue);
} catch (KeyNotFoundException $e) {
// Fallback logic (e.g., log or use default value)
}
How can I help you explore Laravel packages today?