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

dev-main 2025-10-12 15:36 UTC

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);