tapanderasari/laravel-mysql-encrypt
最新稳定版本:v3.0.0
Composer 安装命令:
composer require tapanderasari/laravel-mysql-encrypt
包简介
Laravel Database encryption mysql side
README 文档
README
Laravel database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions. Automatically encrypt and decrypt fields in your Models.
Version Compatibility
| Package Version | Laravel | PHP |
|---|---|---|
| 3.x | 13.x | ^8.3 |
| 1.x | 10–12 | ^8.0 |
Install
1. Composer
composer require tapanderasari/laravel-mysql-encrypt
2. Publish config (optional)
php artisan vendor:publish --provider="TapanDerasari\MysqlEncrypt\Providers\LaravelServiceProvider"
3. Set encryption key in .env file
APP_AESENCRYPT_KEY=yourencryptionkey
Update Models
<?php namespace App; use TapanDerasari\MysqlEncrypt\Traits\Encryptable; use Illuminate\Database\Eloquent\Model; class User extends Model { use Encryptable; // <-- 1. Include trait public array $encryptable = [ // <-- 2. Include columns to be encrypted 'email', 'first_name', 'last_name', 'telephone', ]; }
Schema
Encrypted columns must use VARBINARY (or BINARY/BLOB) in MySQL, not VARCHAR.
Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('password'); $table->binary('first_name', 300); // VARBINARY(300) $table->binary('last_name', 300); $table->binary('email', 300); $table->binary('telephone', 50); $table->rememberToken(); $table->timestamps(); });
Validators
unique_encrypted
unique_encrypted:<table>,<field(optional)>,<ignore_id(optional)>
exists_encrypted
exists_encrypted:<table>,<field(optional)>
Scopes
A global scope DecryptSelectScope is automatically applied to models using the Encryptable trait. It rewrites SELECT queries to wrap encrypted columns with AES_DECRYPT().
The following local scopes are available:
Exact match
User::whereEncrypted('email', 'john@example.com')->first(); User::whereNotEncrypted('email', 'john@example.com')->get();
OR conditions
User::whereEncrypted('email', 'a@b.com') ->orWhereEncrypted('email', 'c@d.com') ->get(); User::whereNotEncrypted('email', 'a@b.com') ->orWhereNotEncrypted('email', 'c@d.com') ->get();
LIKE search
User::whereEncryptedLike('first_name', 'John')->get(); User::orWhereEncryptedLike('first_name', 'Jane')->get();
Ordering
User::orderByEncrypted('first_name', 'asc')->get(); User::orderByEncryptedSort('last_name', 'desc')->get();
Implementing encryption for existing data
For this you can create one command like
php artisan make:command EncryptionForExistingData
In this command you fetch existing table or model data without global scope DecryptSelectScope.
You can refer the example, clicking on below Example button:
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 1.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-06