thetalabs / laravel-database-encryption
一个无需烦恼的Laravel模型特性,可用于添加加密模型属性,并可选择盲索引。
Requires (Dev)
- illuminate/support: ^5.7
- orchestra/testbench: ^3.7
- phpunit/phpunit: ^7.5
README
一个无需烦恼的Laravel模型特性,可用于添加加密模型属性,并可选择盲索引。
标记为加密的模型属性将通过Laravel的默认加密进行加密。
该库还支持对加密属性进行盲索引。在加密之前,盲索引将值进行哈希处理并存储在单独的列中。然后可以使用 whereEncrypted()
和 orWhereEncrypted()
进行查询
最后,您可以按字段禁用盲索引,也可以根据喜好更改哈希算法。
注意 - 此特性不会为您创建数据库列。您必须创建自己的迁移
特性
- 加密 - 在设置时对指定属性进行加密。
- 解密 - 在访问时自动解密属性。
- 高性能 - 在访问后自动缓存解密值以提高性能。
- 搜索(可选) - 对原始输入进行哈希处理以支持 精确 搜索。支持 模糊 搜索。见下文。
- 可定制 - 可定制的哈希算法和字段命名。
- 安全 - 自动隐藏哈希以防止暴力破解和数据泄露。
要求
此软件包需要Laravel 5.6。Laravel的早期版本有不同的模型实现,与此软件包不兼容。
安装
composer require thetalabs/laravel-database-encryption
使用方法
将特性添加到您的模型中
您还需要配置您要加密的列。
以下示例将加密 ssn
属性,但不对其进行哈希处理。在这种情况下,它将不可搜索。
use ThetaLabs\DbEncryption\HasEncryptedAttributes;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasEncryptedAttributes;
// Encrypt the following attributes
protected static $encrypted = [
'ssn',
];
}
注意,如果您想启用盲索引进行搜索,可以这样做。这些索引完全是可选的,如果禁用选项,则不会存储任何哈希数据。
protected static $encrypted = [
'ssn' => 'bi'
// or with a custom bi field...
'ssn' => 'bi:custom_bi_field'
];
默认情况下,盲索引使用sha256进行哈希处理。您可以根据属性更改此设置。
protected static $encrypted = [
'ssn' => 'bi,hash:sha1',
// or with custom bi field...
'ssn' => 'bi:custom_bi_field,hash:sha1'
];
有关支持的哈希算法,请参阅 hash_hmac
文档。
添加加密数据库列
创建一个迁移来更新列的长度和类型。
注意:长度取决于您选择的哈希算法(如果有)用于盲索引。默认情况下 sha256
的长度为 64
。
// in your migration...
$table->string('ssn', 9);
$table->string('ssn_bi', 64);
搜索
已添加一个本地作用域,允许您使用盲索引列查询加密字段。
// searches all users where ssn_bi (or custom bi field)
// is 000-00-0000
$user = User::whereEncrypted('ssn', '000-00-0000')
->first();
// or...
// Search by name or where ssn is 000-00-0000
$users = User::where('name', 'Thomas Krause')
->orWhereEncrypted('ssn', '000-00-0000')
->get();
隐藏加密字段
通常,您会希望在不需要时限制解密值。
您应使用Laravel内置的 $hidden
和 $visible
属性来完成此操作。
class User extends Model
{
use HasEncryptedAttributes;
// Encrypt the following attributes
protected static $encrypted = [
'ssn',
];
// Hide the encrypted attributes
// will prevent them showing in toArray() and JSON
protected $hidden = [
'ssn'
];
}
模糊搜索
我们还支持在模型中存储字段拆分,这些字段与原始值一起存储在盲索引中。请注意,所有值都使用配置的哈希进行哈希处理。
这允许您定义数据应该如何拆分,并允许对拆分的部分以及原始值进行搜索。
定义一个具有以下命名约定函数以启用此功能。您必须返回一个包含您希望可搜索的每个值的数组。返回的值将在纯文本中,我们将处理其余部分。
字段命名约定为 explodeAttributeToSearchables
。其中属性是字段名。例如,对于字段 ssn
,函数应为 explodeSsnToSearchables
。
类似地,字段名为 buy_me_a_beer
的字段将有一个名为 explodeBuyMeABeerToSearchables
的拆分函数。
class User extends Model
{
use HasEncryptedAttributes;
// Encrypt the following attributes
protected static $encrypted = [
'ssn' => 'bi,
];
// Can be anything you want here
// name must match convention
// you must return an array
protected function explodeSsnToSearchables($ssn)
{
return explode('-', $ssn);
}
}
注意事项
在设置属性时计算盲索引以供搜索。如果您想在加密后搜索某一列,则需要执行迁移来重新加密所有值以计算哈希值。
// Something like...
$users = User::all();
foreach ($users as $user) {
// Will force re-encryption and set ssn as dirty.
// will also update the hash for the field if it's been set
$user->ssn = $user->ssn;
}