bjornvoesten/laravel-ciphersweet

此包已被弃用且不再维护。作者建议使用 bjorn-voesten/ciphersweet-for-laravel 包代替。

Laravel CipherSweet

0.2 2024-04-17 15:21 UTC

This package is auto-updated.

Last update: 2024-04-17 15:23:47 UTC


README

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

Laravel CipherSweet: 可搜索的加密属性

嘿!我得说,我并不是很活跃地维护这个包,但你可以随时发送pull请求!

Laravel CipherSweet 是一个基于 LaravelParagon 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)。有关更多信息,请参阅 许可证文件