xdanet/laravel-mysql-encrypt

Laravel 5.x | 6.x | 7.x | 8.x 数据库加密(MySQL端)

2.0.0 2021-02-19 08:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 21:07:39 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

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

安装

1. Composer

composer require xandanet/laravel-mysql-encrypt

2. 发布配置(可选)

Laravel

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

Lumen

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

3. 配置提供者

Laravel

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

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

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

Lumen

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

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

APP_AESENCRYPT_KEY=yourencryptionkey

更新模型

<?php

namespace App;

use XandaNet\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 orderByEncrypted

全局作用域 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)。请参阅 许可文件 获取更多信息。