phpcodersnp / laravel-database-encryption
通过 Eloquent 自动加密和解密数据库
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.4
README
此软件包因原始所有者不活跃,从此处克隆。
使用 openssl 加密和解密 Laravel 模型属性的软件包
主要特性
- 轻松加密和解密数据库字段
- 最小化配置
- 使用以下方式包含搜索加密数据:
whereEncrypted
、orWhereEncrypted
、orderByEncrypted
、selectEncrypted
、concatEncrypted
- 使用 openssl 加密和解密字段
可用方法
whereEncrypted
、orWhereEncrypted
、orderByEncrypted
、selectEncrypted
、concatEncrypted
用法
whereEncrypted
、orWhereEncrypted
whereEncrypted('first_name','john') orWhereEncrypted('last_name','!=','Doe')
orderByEncrypted
orderByEncrypted('last_name','asc') orderByEncrypted('last_name','desc')
selectEncrypted
selectEncrypted([ "first_name as userFirstName", "last_name", ]) // array only
concatEncrypted
concatEncrypted('first_name , " " ,last_name AS fullUserName')
要求
- Laravel: >= 5
- PHP: >= 7.3
Schema 要求
加密值通常比纯文本值长,有时长得多。您可能会发现数据库表中的列宽度需要更改,以存储此软件包生成的加密值。
我们强烈建议将列类型更改为 TEXT
或 LONGTEXT
安装
步骤 1: Composer
通过 Composer 命令行
composer require phpcodersnp/laravel-database-encryption
步骤 2: 将 ServiceProvider 添加到您的 app/config.php 文件(Laravel 5.4 或以下)
将服务提供者添加到 config/app.php 配置文件中的 providers 数组,如下所示
'providers' => [ ... \PHPCodersNp\DBEncryption\Providers\DBEncryptionServiceProvider::class, ],
步骤 3: 使用以下 Artisan 命令发布配置文件
php artisan vendor:publish --provider="PHPCodersNp\DBEncryption\Providers\DBEncryptionServiceProvider"
对于 Lumen 项目
配置配置文件
cp vendor/phpcodersnp/laravel-database-encryption/src/Config/config.php config/laravelDatabaseEncryption.php
注册配置和软件包
复制以下代码并将其粘贴到 bootstrap/app.php
$app->configure('laravelDatabaseEncryption');
$app->register(PHPCodersNp\DBEncryption\Providers\DBEncryptionServiceProvider::class);
用法
在您想要应用加密的任何 Eloquent 模型中使用 EncryptedAttribute
特性,并定义一个包含要加密的属性列表的 protected $encrypted
数组。
例如
use PHPCodersNp\DBEncryption\Traits\EncryptedAttribute; class User extends Eloquent { use EncryptedAttribute; /** * The attributes that should be encrypted on save. * * @var array */ protected $encryptable = [ 'first_name', 'last_name', 'email' ]; }
通过包含 EncryptedAttribute
特性,Eloquent 提供的 setAttribute()
、getAttribute()
和 getAttributeFromArray()
方法被重写,以包括额外的步骤。
加密现有数据
如果您数据库中有当前数据,可以使用此命令进行加密
php artisan encryptable:encryptModel 'App\Models\User'
此外,您还可以使用此命令进行解密
php artisan encryptable:decryptModel 'App\Models\User'
注意:您必须首先实现 Encryptable
特性并设置 $encryptable
属性
存在和唯一验证规则
如果您使用存在和唯一规则与加密值,将其替换为 exists_encrypted 和 unique_encrypted
$validator = validator(['email'=>'foo@bar.com'], ['email'=>'exists_encrypted:users,email']); $validator = validator(['email'=>'foo@bar.com'], ['email'=>'unique_encrypted:users,email']);
常见问题解答
我能否搜索加密数据?
YES!您将能够搜索由本软件包加密的属性,因为如果需要搜索数据,请使用 whereEncrypted
和 orWhereEncrypted
函数
User::whereEncrypted('email','test@gmail.com') ->orWhereEncrypted('email','test2@gmail.com') ->first();
一旦模型使用 EncryptedAttribute
,就会自动添加到 eloquent
鸣谢
本包的灵感来源于以下项目:austinheap/laravel-database-encryption magros/laravel-model-encryption DustApplication/laravel-database-model-encryption elgiborsolution/laravel-database-encryption
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。