anexia/laravel-encryption

eloquent 模型的加密和解密

1.0.0 2018-10-10 13:45 UTC

This package is auto-updated.

Last update: 2024-09-17 21:26:05 UTC


README

一个 Laravel 扩展包,为 eloquent 模型添加数据库加密支持。

1. 安装和配置

1. 通过 composer 安装

通过 composer 安装模块,因此需要修改 composer.json 文件中的 require 部分

"require": {
    "anexia/laravel-encryption": "1.0.0"
}

现在运行

composer update [-o]

将包的源代码添加到您的 /vendor 目录,并更新自动加载。

2. 添加服务提供器到应用配置

'providers' => [
    /*
     * Package Service Providers...
     */
    \Anexia\LaravelEncryption\DatabaseEncryptionServiceProvider::class,
]

3. 添加加密到数据库配置

目前仅支持 Postgres 和 PGP。

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
    'cipher' => 'pgp'
],

2. 使用方法

2.1 模型

将 DatabaseEncryption 特性添加到您的 eloquent 模型中。

<?php

namespace App;

use Anexia\LaravelEncryption\DatabaseEncryption;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class User extends Authenticatable
{
    use Notifiable, DatabaseEncryption;
    

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
    

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    

    /**
     * @return array
     */
    protected static function getEncryptedFields()
    {
        return [
            'password'
        ];
    }
    

    /**
     * @return string
     */
    protected function getEncryptKey()
    {
        return 'thisismysupersecretencryptionkey';
    }
}

2.2 更新

只需在模型上调用 save() 方法。字段将自动加密。

2.3 查询

默认情况下,加密属性将替换为其对应 "_encrypted" 的值。

$user = User::find(1);

上述查询将具有 "password_encrypted" 属性,没有 "password" 属性。

2.3.1 解密属性

使用宏 withDecryptKey 进行自动解密。

2.3.1 'select *' 查询

$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1);

在上面的例子中 $user 将有两个属性

  • password: 解密后的密码
  • password_encrypted: 数据库中的加密值
  • id
  • name
  • email
  • remember_token

2.3.2 选择特定字段

$user = User::->find(1, ['id']);

$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1, ['id']);

在上面的两个例子中,$user 都只有一个属性

  • id
$user = User::->find(1, ['id', 'password']);

在上面的例子中,$user 只有两个属性

  • id
  • password_encrypted: 数据库中的加密值
$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1, ['id', 'password']);

在上面的例子中,$user 只有三个属性

  • id
  • password: 解密后的密码
  • password_encrypted: 数据库中的加密值

2.3.3 检查特定解码值

使用宏 whereDecripted 在加密属性上运行 "where field = value" 查询。

$user = User::whereDecrypted('password', 'thisIsTheWantedPassword', 'thisismysupersecretencryptionkey')->first();

在上面的例子中,$user 将是第一个具有(解密)密码 'thisIsTheWantedPassword' 的条目。