maize-tech/laravel-encryptable

Laravel Encryptable

3.3.0 2024-03-27 11:36 UTC

This package is auto-updated.

Last update: 2024-09-08 08:03:56 UTC


README

Laravel Encryptable

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包允许您对敏感数据(如用户的姓名、姓氏和电子邮件地址)进行匿名处理,类似于Laravel的加密功能,但仍能直接查询数据库。一个示例用例可能是需要通过匿名属性进行搜索查询。

此包目前支持 MySQLPostgreSQL 数据库。

安装

您可以通过composer安装此包

composer require maize-tech/laravel-encryptable

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Maize\Encryptable\EncryptableServiceProvider" --tag="encryptable-config"

这是发布配置文件的内容

return [
    /*
    |--------------------------------------------------------------------------
    | Encryption key
    |--------------------------------------------------------------------------
    |
    | The key used to encrypt data.
    | Once defined, never change it or encrypted data cannot be correctly decrypted.
    |
    */

    'key' => env('ENCRYPTION_KEY'),

    /*
    |--------------------------------------------------------------------------
    | Encryption cipher
    |--------------------------------------------------------------------------
    |
    | The cipher used to encrypt data.
    | Once defined, never change it or encrypted data cannot be correctly decrypted.
    | Default value is the cipher algorithm used by default in MySQL.
    |
    */

    'cipher' => env('ENCRYPTION_CIPHER', 'aes-128-ecb'),
];

用法

基本用法

要使用此包,只需将Encryptable转换添加到您想要匿名化的所有模型属性中。

<?php

namespace App\Models;

use Maize\Encryptable\Encryptable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = [
        'name',
        'email',
    ];

    protected $casts = [
        'name' => Encryptable::class,
        'email' => Encryptable::class,
    ];
}

完成后,所有值在存储到数据库之前都会被加密,在通过Eloquent查询时会被解密。

通过PHP手动加密

use Maize\Encryptable\Encryption;

$value = "your-decrypted-value";

$encrypted = Encryption::php()->encrypt($value); // returns the encrypted value

通过PHP手动解密

use Maize\Encryptable\Encryption;

$encrypted = "your-encrypted-value";

$value = Encryption::php()->decrypt($value); // returns the decrypted value

通过数据库手动解密

use Maize\Encryptable\Encryption;

$encrypted = "your-encrypted-value";

$encryptedQuery = Encryption::db()->encrypt($value); // returns the query used to find the decrypted value

自定义验证规则

您可以使用两个自定义规则之一来检查给定加密值的唯一性或存在性。

ExistsEncrypted是Laravel的Exists规则的扩展,而UniqueEncrypted是Laravel的Unique规则的扩展。您可以使用它们与Laravel的基本规则相同的方式使用。

use Maize\Encryptable\Rules\ExistsEncrypted;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$data = [
    'email' => 'email@example.com',
];

Validator::make($data, [
    'email' => [
        'required',
        'string',
        'email',
        new ExistsEncrypted('users'), // checks whether the given email exists in the database
        Rule::existsEncrypted('users') // alternative way to invoke the rule
    ],
]);
use Maize\Encryptable\Rules\UniqueEncrypted;
use Illuminate\Support\Facades\Validator;

$data = [
    'email' => 'email@example.com',
];

Validator::make($data, [
    'email' => [
        'required',
        'string',
        'email',
        new UniqueEncrypted('users'), // checks whether the given email does not already exist in the database
        Rule::uniqueEncrypted('users') // alternative way to invoke the rule
    ],
]);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请参阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。