bestyii / yii2-encrypter
为 Yii2 加密数据
v1.0.0
2022-08-19 03:10 UTC
Requires
- yiisoft/yii2: ^2.0.0
README
兼容 mcrypt_encrypt
及 openssl_encrypt
安装
安装此扩展的最佳方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist bestyii/yii2-encrypter "*"
或
"bestyii/yii2-encrypter": "*"
将以下内容添加到您的 composer.json
文件的 require 部分。
配置
在配置文件中加入
return [ //... 'components' => [ //... 'encrypter' => [ 'class' => 'bestyii\encrypter\Encrypter', 'key' => '32bit string', 'iv' => '32bit string', ], ], ];
如何使用
手动
您现在可以在应用的任何部分手动使用加密器来加密字符串
\Yii::$app->encrypter->encrypt('string to encrypt');
或解密已加密的字符串
\Yii::$app->encrypter->decrypt('string to decrypt');
使用Behavior自动加密/解密
此扩展还提供了一个可以轻松附加到任何 ActiveRecord Model 的行为。
使用以下语法附加行为。
public function behaviors() { return [ 'encryption' => [ 'class' => '\bestyii\encrypter\EncrypterBehavior', 'attributes' => [ 'attributeName', 'otherAttributeName', ], ], ]; }
行为将在保存数据到数据库前自动加密所有数据,并在检索后解密。
请注意,行为将使用扩展的当前配置进行加密。