ghalibilal / laravel-hmac-encryption
A Laravel encryption package with HMAC integrity check
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/ghalibilal/laravel-hmac-encryption
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
- laravel/framework: ^9.0|^10.0|^11.0|^12.0
This package is not auto-updated.
Last update: 2026-01-04 16:42:04 UTC
README
A simple and secure Laravel package for encrypting and decrypting model attributes, queries, and sensitive data using AES-256-CBC with HMAC integrity.
โ
Supports .env-based encryption keys and automatic attribute encryption via model casting.
๐ฆ Features
- ๐ AES-256-CBC encryption with HMAC SHA-256
- ๐ง Automatic encryption/decryption via Eloquent attribute casting
- ๐งฑ Custom query macros:
whereEncrypted,orWhereEncrypted - โ๏ธ Configurable encryption key and IV from
.env - โ Facade support for easy usage
๐ Installation
composer require ghalibilal/laravel-hmac-encryption:dev-main
Then publish the configuration file:
php artisan vendor:publish --tag=config
๐ .env Configuration
You must define two secure values in your .env file:
ENCRYPTION_KEY=base64:your_base64_encoded_32_byte_key_here ENCRYPTION_SECRET=base64:your_base64_encoded_16_byte_iv_here
๐ How to generate secure keys
Generate a 32-byte encryption key: php -r "echo 'base64:' . base64_encode(random_bytes(32)) . PHP_EOL;" Generate a 16-byte IV (initialization vector): php -r "echo 'base64:' . base64_encode(random_bytes(16)) . PHP_EOL;"
โ ๏ธ These values are required and must be securely stored.
๐ง Usage of EncryptedCast in a Model
Automatically encrypt/decrypt specific model fields:
use Illuminate\Database\Eloquent\Model; use Ghalibilal\LaravelEncryption\Casts\EncryptedCast; class User extends Model { protected $casts = [ 'email' => EncryptedCast::class, 'phone' => EncryptedCast::class, ]; }
๐ Query Using Encrypted Values
User::whereEncrypted('email', '=', 'secret@example.com')->first(); User::orWhereEncrypted('phone', '=', '1234567890')->get();
๐ Artisan Commands
Encrypt Model Attributes
You can encrypt attributes of a model directly from the terminal using:
php artisan encrypt:model --class="App\\Models\\User"
The package automatically encrypts the value before executing the query.
๐งช Example use Ghalibilal\LaravelEncryption\Facades\Encryption; $plain = 'Sensitive info'; $encrypted = Encryption::encrypt($plain); $decrypted = Encryption::decrypt($encrypted);