edmondsylat/laravel-topsec

Laravel CipherSweet

0.1.3 2020-01-08 21:38 UTC

This package is auto-updated.

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


README

此存储库已弃用,推荐使用: https://github.com/bjornvoesten/ciphersweet-for-laravel

Laravel CipherSweet: 可搜索的加密属性

嘿!我要说的是,我对维护这个包不是很积极,但你可以随时发送拉取请求!

Laravel CipherSweet 是一个 Laravel 实现的 Paragon Initiative Enterprises CipherSweet 可搜索字段级加密。

Preview

在继续之前,请确保你对 CipherSweet 有一些基本了解。

安装

使用 composer 安装此包

composer require bjornvoesten/laravel-ciphersweet

发布配置文件

php artisan vendor:publish --tag=ciphersweet-config

生成加密密钥

php artisan ciphersweet:key

注意! 所有加密列都依赖于此密钥。如果密钥更改,已加密的列将无法再解密!

配置

算法

您可以通过定义 crypto 来更改加密算法

ENCRYPTION_CRYPTO=modern/fips

有关加密索引算法的更多信息,请参阅 文档

用法

定义加密

Bjornvoesten\CipherSweet\Traits\HasEncryption 特性添加到模型中。

    use HasEncryption;

定义应加密的属性。

    /**
     * The attributes that can be encrypted.
     *
     * @var array
     */
    protected $encrypted = [
        'social_security_number',
    ];

默认情况下,索引列名称使用名称生成,并在其后添加 _index 后缀。

因此,social_security_number 属性将使用默认索引列 social_security_number_index

或者,您可以为每个属性定义多个索引并定义更多选项。

    /**
     * Set the social security number attribute encryption.
     *
     * @param \Bjornvoesten\CipherSweet\Contracts\Attribute $attribute
     * @return void
     */
    public function socialSecurityNumberAttributeEncryption($attribute): void
    {
        $attribute
            ->index('social_security_number_full_index', function (Index $index) {
                $index
                    ->bits(32)
                    ->slow();
            })
            ->index('social_security_number_last_four_index', function (Index $index) {
                $index
                    ->bits(16)
                    ->transform(
                        new LastFourDigits()
                    );
            });
    }

有关索引选项的更多信息,请参阅 文档

并且请确保您已在数据库表中创建了索引列!

搜索模型

您可以使用查询构建器的默认 where 子句或使用 whereEncrypted 方法来搜索加密属性。

 \App\User::query()
    ->where('social_security_number', '=', $number)
    ->get();

使用 whereEncrypted 方法,您还可以定义可搜索的索引。

 \App\User::query()
    ->whereEncrypted('social_security_number', '=', $number, [
        'social_security_number_last_four_index',
    ])
    ->get();

注意 当使用 等于 操作符进行搜索时,如果值在所有可用或定义的索引中找到,则将返回模型。当使用 不等于 操作符时,将返回所有值在任何可用或定义的索引中找不到的模型。

注意事项

由于 CipherSweet 中搜索功能的限制,在搜索加密属性时,只有 =!= 操作符可用。

测试

$ composer test

待定。

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件