Skip to content

Commit c81b656

Browse files
committed
initial commit
0 parents  commit c81b656

File tree

11 files changed

+304
-0
lines changed

11 files changed

+304
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/tests export-ignore
8+
/.editorconfig export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/vendor
3+
composer.lock
4+
.phpunit.result.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 chr15k
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Laravel MySql AES Encrypt/Decrypt
2+
3+
Laravel database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions.
4+
Automatically encrypt and decrypt fields in your Models.
5+
6+
## 1. Install
7+
```bash
8+
composer require chr15k/laravel-mysql-encrypt
9+
```
10+
11+
## 2. Publish config (optional)
12+
```bash
13+
php artisan vendor:publish --provider="Chr15k\MysqlEncrypt\MysqlEncryptServiceProvider"
14+
```
15+
16+
## 3. Configure Provider (Laravel 5.4 or earlier)
17+
For Laravel 5.4 or earlier, you'll need to add the following to config/app.php:
18+
19+
```php
20+
'providers' => array(
21+
Chr15k\\MysqlEncrypt\\MysqlEncryptServiceProvider::class
22+
)
23+
```
24+
25+
## 4. Set encryption key in `.env` file
26+
```
27+
APP_AESENCRYPT_KEY=yourencryptionkey
28+
```
29+
30+
## Update Models
31+
```php
32+
<?php
33+
34+
namespace App;
35+
36+
use Chr15k\MysqlEncrypt\Traits\Encryptable;
37+
use Illuminate\Database\Eloquent\Model;
38+
39+
class User extends Model
40+
{
41+
use Encryptable; // <-- 1. Include trait
42+
43+
protected $encryptable = [ // <-- 2. Include columns to be encrypted
44+
'email',
45+
'first_name',
46+
'last_name',
47+
'telephone',
48+
];
49+
}
50+
```
51+
52+
## Schema columns to support encrypted data
53+
```php
54+
Schema::create('users', function (Blueprint $table) {
55+
$table->bigIncrements('id');
56+
$table->string('password');
57+
$table->rememberToken();
58+
$table->timestamps();
59+
});
60+
61+
// Once the table has been created, use ALTER TABLE to create VARBINARY
62+
// or BLOB types to store encrypted data.
63+
DB::statement('ALTER TABLE `users` ADD `first_name` VARBINARY(300)');
64+
DB::statement('ALTER TABLE `users` ADD `last_name` VARBINARY(300)');
65+
DB::statement('ALTER TABLE `users` ADD `email` VARBINARY(300)');
66+
DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');
67+
```

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "chr15k/laravel-mysql-encrypt",
3+
"description": "Laravel 5.x | 6.x | 7.x Database encryption mysql side",
4+
"keywords": [
5+
"laravel",
6+
"chr15k",
7+
"mysql",
8+
"encryption",
9+
"php",
10+
"pii"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Christopher Keller",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.2.5",
21+
"illuminate/support": "^5.0|^6.0|^7.0",
22+
"illuminate/database": "^5.0|^6.0|^7.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Chr15k\\MysqlEncrypt\\": "src/"
27+
},
28+
"files": [
29+
"src/helpers.php"
30+
]
31+
},
32+
"config": {
33+
"sort-packages": true
34+
},
35+
"extra": {
36+
"laravel": {
37+
"providers": [
38+
"Chr15k\\MysqlEncrypt\\MysqlEncryptServiceProvider"
39+
]
40+
}
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true
44+
}

config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
5+
'key' => env('APP_AESENCRYPT_KEY'),
6+
7+
];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Chr15k\MysqlEncrypt;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class MysqlEncryptServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function boot()
13+
{
14+
$this->publishes([
15+
__DIR__.'/../config/config.php' => config_path('mysql-encrypt.php'),
16+
], 'config');
17+
}
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function register()
23+
{
24+
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'mysql-encrypt');
25+
}
26+
}

src/Scopes/DecryptSelectScope.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Chr15k\MysqlEncrypt\Scopes;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Scope;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Schema;
10+
11+
class DecryptSelectScope implements Scope
12+
{
13+
/**
14+
* Apply the scope to a given Eloquent query builder.
15+
*
16+
* @param \Illuminate\Database\Eloquent\Builder $builder
17+
* @param \Illuminate\Database\Eloquent\Model $model
18+
* @return void
19+
*/
20+
public function apply(Builder $builder, Model $model)
21+
{
22+
$encryptable = $model->encryptable();
23+
24+
$columns = empty($columns) ? Schema::getColumnListing($model->getTable()) : $columns;
25+
26+
if (empty($encryptable) || empty($columns)) {
27+
return $builder->addSelect(...$columns);
28+
}
29+
30+
$select = collect($columns)->map(function($column) use ($encryptable) {
31+
return (in_array($column, $encryptable)) ? db_decrypt($column) : $column;
32+
});
33+
34+
return $builder->addSelect(...$select);
35+
}
36+
}

src/Traits/Encryptable.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Chr15k\MysqlEncrypt\Traits;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Chr15k\MysqlEncrypt\Scopes\DecryptSelectScope;
7+
8+
trait Encryptable
9+
{
10+
/**
11+
* @return void
12+
*/
13+
public static function bootEncryptable()
14+
{
15+
static::addGlobalScope(new DecryptSelectScope);
16+
}
17+
18+
/**
19+
* @param string $key
20+
* @param mixed $value
21+
*
22+
* @return mixed
23+
*/
24+
public function setAttribute($key, $value)
25+
{
26+
if (is_null($value) || !in_array($key, $this->encryptable)) {
27+
return parent::setAttribute($key, $value);
28+
}
29+
30+
return parent::setAttribute($key, db_encrypt($value));
31+
}
32+
33+
/**
34+
* @return array
35+
*/
36+
public function encryptable(): array
37+
{
38+
return $this->encryptable ?? [];
39+
}
40+
}

0 commit comments

Comments
 (0)