prajapatidhara1510/laravel-mysql-encrypt

Laravel 5.x | 6.x | 7.x | 8.x 数据库端mysql加密

v1.0.3 2024-01-22 07:14 UTC

This package is auto-updated.

Last update: 2024-09-22 09:08:25 UTC


README

使用原生AES_DECRYPT和AES_ENCRYPT函数在Laravel/Lumen数据库端进行数据库加密。自动加密和解密模型中的字段。

安装

1. Composer

composer require PrajapatiDhara1510/laravel-mysql-encrypt

2. 发布配置(可选)

Laravel

php artisan vendor:publish --provider="PrajapatiDhara1510\MysqlEncrypt\Providers\LaravelServiceProvider"

Lumen

mkdir -p config
cp vendor/prajapatidhara1510/laravel-mysql-encrypt/config/config.php config/mysql-encrypt.php

3. 配置提供者

Laravel

  • 对于Laravel 5.5或更高版本,服务提供者会自动加载,跳过此步骤。

  • 对于Laravel 5.4或更早版本,将以下内容添加到config/app.php

'providers' => array(
    PrajapatiDhara1510\\MysqlEncrypt\\Providers\\LaravelServiceProvider::class
);

Lumen

  • 对于Lumen,将以下内容添加到bootstrap/app.php
$app->register(PrajapatiDhara1510\MysqlEncrypt\Providers\LumenServiceProvider::class);

4. 在.env文件中设置加密密钥

APP_AESENCRYPT_KEY=yourencryptionkey

更新模型

<?php

namespace App;

use PrajapatiDhara1510\MysqlEncrypt\Traits\Encryptable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Encryptable; // <-- 1. Include trait

    protected $encryptable = [ // <-- 2. Include columns to be encrypted
        'email',
        'first_name',
        'last_name',
        'telephone',
    ];
}

验证器

unique_encrypted

unique_encrypted:<table>,<field(optional)>

exists_encrypted

exists_encrypted:<table>,<field(optional)>

作用域

提供自定义局部作用域

whereEncrypted whereNotEncrypted orWhereEncrypted orWhereNotEncrypted orWhereEncryptedLike orderByEncrypted orderByEncryptedSort whereInEncrypted

全局作用域 DecryptSelectScope 会自动在具有Encryptable特质的模型中使用。

支持加密数据的模式列

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

// Once the table has been created, use ALTER TABLE to create VARBINARY
// or BLOB types to store encrypted data.
DB::statement('ALTER TABLE `users` ADD `first_name` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `last_name` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `email` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件