danielpardamean/laravel-mysql-encrypt

Laravel 9.x 数据库加密(MySQL端)

1.0.3 2023-12-21 07:26 UTC

This package is auto-updated.

Last update: 2024-09-21 09:22:56 UTC


README

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

安装

1. Composer

composer require danielpardamean/laravel-mysql-encrypt

2. 发布配置(可选)

Laravel

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

Lumen

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

3. 配置提供者

Laravel

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

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

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

Lumen

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

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

APP_AESENCRYPT_KEY=yourencryptionkey

更新模型

<?php

namespace App;

use DanielPardamean\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)。有关更多信息,请参阅许可证文件