njhyuk/laravel-encryptable

使用 mysql AES 函数实现 Laravel Eloquent 模型自动加密和解密。

dev-master 2019-01-20 08:06 UTC

This package is auto-updated.

Last update: 2024-09-20 21:06:21 UTC


README

使用 Mysql AES 函数自动加密 Laravel Eloquent 模型列。

由于使用 Mysql AES 函数,因此可以进行数据库搜索。

注意

Mysql AES 函数不使用初始化向量。

如果您放弃在 Mysql 中搜索,最好使用不同的解决方案。

安装

安装包

composer require njhyuk/laravel-encryptable

配置包

 php artisan vendor:publish --provider="Njhyuk\LaravelEncryptable\EncryptableProvider"

在 env 文件中添加加密密钥

ENCRYPTABLE_KEY=SetYour16ByteKey

用法

指定模型的加密列

使用 Njhyuk\LaravelEncryptable\Encryptable 特性并添加要加密的列。

<?php

namespace App\Models;

use Njhyuk\LaravelEncryptable\Encryptable;

class User extends Authenticatable
{
    use Notifiable;
    use Encryptable;

    /**
     * Encrypted columns
     * 
     * @var array
     */
    protected $encryptable = [
        'name',
        'email'
    ];
}    

插入和更新模型

$user = new User;
$user->email = 'example@example.com'; //It is encrypted and stored.
$user->save();

检索模型

User::where('email','like','%example%')->get(); //Encrypted data retrieval is possible.