1- # Laravel/Lumen MySql AES Encrypt/Decrypt
1+ # Laravel MySql AES Encrypt/Decrypt
22
33[ ![ Latest Stable Version] ( https://poser.pugx.org/chr15k/laravel-mysql-encrypt/v )] ( //packagist.org/packages/chr15k/laravel-mysql-encrypt ) [ ![ Latest Unstable Version] ( https://poser.pugx.org/chr15k/laravel-mysql-encrypt/v/unstable )] ( //packagist.org/packages/chr15k/laravel-mysql-encrypt ) [ ![ Total Downloads] ( https://poser.pugx.org/chr15k/laravel-mysql-encrypt/downloads )] ( //packagist.org/packages/chr15k/laravel-mysql-encrypt ) [ ![ License] ( https://poser.pugx.org/chr15k/laravel-mysql-encrypt/license )] ( //packagist.org/packages/chr15k/laravel-mysql-encrypt )
44
5- Laravel/Lumen database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions.
5+ Laravel MySQL encryption using native MySQL AES_DECRYPT and AES_ENCRYPT functions.
66Automatically encrypt and decrypt fields in your Models.
77
88## Install
9+
910### 1. Composer
11+
1012``` bash
1113composer require chr15k/laravel-mysql-encrypt
1214```
1315
14- ### 2. Publish config (optional)
15- ` Laravel `
16- ``` bash
17- php artisan vendor:publish --provider=" Chr15k\MysqlEncrypt\Providers\LaravelServiceProvider"
18- ```
16+ ### 2. Publish config
1917
20- ` Lumen `
2118``` bash
22- mkdir -p config
23- cp vendor/chr15k/laravel-mysql-encrypt/config/config.php config/mysql-encrypt.php
19+ php artisan vendor:publish --provider=" Chr15k\MysqlEncrypt\Providers\MysqlEncryptServiceProvider"
2420```
2521
26- ### 3. Configure Provider
27- ` Laravel `
28- - For Laravel 5.5 or later, the service provider is automatically loaded, skip this step.
22+ ### 3. AES Key generation
2923
30- - For Laravel 5.4 or earlier, add the following to ` config/app.php ` :
31- ``` php
32- 'providers' => array(
33- Chr15k\\MysqlEncrypt\\Providers\\LaravelServiceProvider::class
34- );
35- ```
24+ Add to .env file:
3625
37- ` Lumen `
38- - For Lumen, add the following to ` bootstrap/app.php ` :
39- ``` php
40- $app->register(Chr15k\MysqlEncrypt\Providers\LumenServiceProvider::class);
4126```
42-
43- ### 4. Set encryption key in ` .env ` file
27+ APP_AESENCRYPT_KEY=
4428```
45- APP_AESENCRYPT_KEY=yourencryptionkey
29+
30+ Generate a new secure AES key (or add your existing key manually):
31+
32+ ``` bash
33+ php artisan laravel-mysql-encrypt:key:generate
4634```
4735
4836## Update Models
37+
4938``` php
5039<?php
5140
@@ -68,17 +57,21 @@ class User extends Model
6857```
6958
7059## Validators
60+
7161`unique_encrypted`
62+
7263```
73- unique_encrypted: <table >,<field (optional) >
64+ unique_encrypted: <table >,<field (optional) >,< ignore _id(optional) >
7465```
7566
7667` exists_encrypted `
68+
7769```
7870exists_encrypted:<table>,<field(optional)>
7971```
8072
8173## Scopes
74+
8275Custom Local scopes available:
8376
8477` whereEncrypted `
@@ -90,6 +83,7 @@ Custom Local scopes available:
9083Global scope ` DecryptSelectScope ` automatically booted in models using ` Encryptable ` trait.
9184
9285## Schema columns to support encrypted data
86+
9387``` php
9488Schema::create('users', function (Blueprint $table) {
9589 $table->bigIncrements('id');
@@ -106,5 +100,66 @@ DB::statement('ALTER TABLE `users` ADD `email` VARBINARY(300)');
106100DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');
107101```
108102
103+ ## Example Usage
104+
105+ ``` php
106+ <?php
107+
108+ use App\Models\User;
109+ use Chr15k\MysqlEncrypt\Rules\ExistsEncrypted;
110+ use Chr15k\MysqlEncrypt\Rules\UniqueEncrypted;
111+ use Illuminate\Support\Facades\Hash;
112+ use Illuminate\Support\Facades\Route;
113+ use Illuminate\Support\Facades\Validator;
114+ use Illuminate\Support\Str;
115+
116+ Route::get('/', function () {
117+
118+ $user = User::firstOrCreate([
119+ 120+ ], [
121+ 'name' => 'Test',
122+ 123+ 'email_verified_at' => now(),
124+ 'password' => Hash::make('password'),
125+ 'remember_token' => Str::random(10),
126+ ]);
127+
128+ // querying an encrypted value using the base methods will not work (as expected):
129+ dump(User::where('name', 'Test')->first()); // => null
130+
131+ // querying through the encrypted scopes will decrypt the value as expected:
132+ dump(
133+ User::whereEncrypted('name', 'Test')
134+ ->orWhereEncrypted('name', 'Chris')
135+ ->first()
136+ ); // => App\Models\User
137+
138+ // Accessing the encrypted attribute on the model will automatically decrypt the value:
139+ dump($user->name); // => 'Test'
140+
141+
142+ // Validation rules
143+
144+ // ExistsEncrypted
145+ $validator = Validator::make(['name' => 'Chris'], [
146+ 'name' => [new ExistsEncrypted('users')],
147+ ]);
148+ if ($validator->fails()) {
149+ dump($validator->errors()->first()); // => "The selected name does not exist"
150+ }
151+
152+ // UniqueEncrypted
153+ $validator = Validator::make(['name' => 'Test'], [
154+ 'name' => [new UniqueEncrypted('users')],
155+ ]);
156+ if ($validator->fails()) {
157+ dump($validator->errors()->first()); // => "The name field must be unique"
158+ }
159+
160+ });
161+ ```
162+
109163## License
164+
110165The MIT License (MIT). Please see [ License File] ( https://github.com/chr15k/laravel-mysql-encrypt/blob/master/LICENSE ) for more information.
0 commit comments