wellingtoncarneirobarbosa / laravel-encrypt-database
通过 Eloquent 自动加密和解密数据库
dev-main
2023-10-21 15:44 UTC
Requires
- illuminate/database: ^8.73
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2024-09-21 17:34:29 UTC
README
目前,Laravel 有一个名为 "encrypted" 的模型铸造。只需使用铸造即可,这样更简单、更高效。
例如
//YourModel.php protected $casts = [ 'your_encrypted_field' => 'encrypted', ];
Laravel 加密数据库
自动加密和解密您的数据库数据。已在 Laravel 8 上测试和使用。
重要
- 请注意,用于加密您的数据的密钥是您的
app_key
,因此请将其保存在安全的地方。 - 如果您丢失了它,您将丢失所有数据库数据。
- 目前,$casts 属性对于加密字段尚不可用。
- 强烈建议将加密列的类型更改为
TEXT
或LONGTEXT
功能
- 最小配置
- 轻松加密和解密数据库字段
- 包括使用以下方式搜索加密数据:whereEncrypted 和 orWhereEncrypted
- 包括 unique_encrypted,exists_encrypted 规则
- 使用 openssl 加密和解密字段
要求
Laravel >= 8.0
PHP >= 7.4
安装
安装包
composer require wellingtoncarneirobarbosa/laravel-encrypt-database
在您的 providers 列表配置文件 app.php 中添加服务提供者:168
WellingtonCarneiroBarbosa\EncryptDatabase\Providers\EncryptDatabaseProvider::class,
发布配置文件
php artisan vendor:publish --tag=laravel-database-encryption
使用方法
只需在您的可加密模型上使用特质,并列出可加密字段。
use WellingtonCarneiroBarbosa\EncryptDatabase\Traits\EncryptableModel;
class User extends Authenticable
{
Use HasFactory,
EncryptableModel;
/**
* The attributes that should be encrypted.
*
* @var array
*/
protected $encryptable = [
'name',
'email',
'birth_date',
];
}
注意:如果您在模型中有一个类似 "setNameAttribute" 的突变器,您应该手动实现加密方法
public function setNameAttribute(string $value)
{
$value = ucwords($value);
$this->attributes['name'] = $this->encrypt('name', $value);
}
如果您有一个访问器方法,您也应该手动实现解密方法
public function getNameAttribute()
{
$decrypted = $this->decrypt('name', $this->attributes['name']);
$value = strtolower($decrypted);
return $value;
}
如果您正在使用 unique
或 exists
验证表单数据,您应该分别将其替换为 unique_encrypted
和 exists_encrypted