bjorn-voesten / ciphersweet-for-laravel
Requires
- php: ^7.2.5|^8.0
- illuminate/config: ^7.0|^8.0|^9.0
- illuminate/database: ^7.0|^8.0|^9.0
- illuminate/support: ^7.0|^8.0|^9.0
- paragonie/ciphersweet: ^2.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0|^7.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-17 16:31:47 UTC
README
Laravel实现Paragon Initiative Enterprises CipherSweet可搜索字段级加密。
在继续之前,请确保您对CipherSweet有基本的了解。
安装
使用composer安装包
composer require bjorn-voesten/ciphersweet-for-laravel
然后该包将自动注册自己。
加密密钥
在您的.env
文件中,您应该添加
CIPHERSWEET_KEY=
然后生成一个加密密钥
php artisan ciphersweet:key
配置文件
发布配置文件
php artisan vendor:publish --tag=ciphersweet-config
用法
定义加密
将BjornVoesten\CipherSweet\Concerns\WithAttributeEncryption
特质添加到您的模型中
并将BjornVoesten\CipherSweet\Casts\Encrypted
类型转换添加到您想要加密的属性中。
<?php use Illuminate\Database\Eloquent\Model; use BjornVoesten\CipherSweet\Concerns\WithAttributeEncryption; use BjornVoesten\CipherSweet\Casts\Encrypted; class User extends Model { use WithAttributeEncryption; protected $fillable = [ 'social_security_number', ]; protected $casts = [ 'social_security_number' => Encrypted::class, ]; }
默认情况下,索引列名称使用名称后缀_index
生成。
因此social_security_number
将使用social_security_number_index
。
使用自定义索引
或者您可以为每个属性定义多个索引并定义更多选项。
<?php use Illuminate\Database\Eloquent\Model; use BjornVoesten\CipherSweet\Concerns\WithAttributeEncryption; use BjornVoesten\CipherSweet\Casts\Encrypted; use BjornVoesten\CipherSweet\Contracts\Attribute; use BjornVoesten\CipherSweet\Contracts\Index; class User extends Model { // ... /** * Encrypt the social security number. * * @param \BjornVoesten\CipherSweet\Contracts\Attribute $attribute * @return void */ public function encryptSocialSecurityNumberAttribute(Attribute $attribute): void { $attribute->index('social_security_number_last_four_index', function (Index $index) { $index ->bits(16) ->transform(new LastFourDigits()); }); } }
加密和解密
属性将在填充和检索属性值时自动加密和解密。
注意 由于该包使用Laravel类型转换,因此无法将Encrypted
类型转换和访问器/修改器组合使用。
搜索
注意 当使用等于
运算符进行搜索时,如果值出现在所有可用或定义的索引之一中,则返回模型。当使用不等于
运算符时,返回所有值未出现在任何可用或定义的索引中的模型。
注意 由于CipherSweet中搜索功能的限制,在搜索加密属性时,仅提供=
和!=
运算符。
whereEncrypted
、orWhereEncrypted
User::query() ->whereEncrypted('social_security_number', '=', '123-456-789') ->orWhereEncrypted('social_security_number', '=', '123-456-789') ->get();
whereInEncrypted
、orWhereInEncrypted
User::query() ->whereInEncrypted('social_security_number', [ '123-456-789', ]) ->orWhereInEncrypted('social_security_number', [ '456-123-789', ]) ->get();
贡献
有关详细信息,请参阅contributing.md和待办事项列表。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件security@bjornvoesten.com联系,而不是使用问题跟踪器。
测试
make test