bestyii/yii2-encrypter

为 Yii2 加密数据

安装次数: 76

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

v1.0.0 2022-08-19 03:10 UTC

This package is auto-updated.

Last update: 2024-09-19 07:30:44 UTC


README

兼容 mcrypt_encryptopenssl_encrypt

Latest Stable Version Total Downloads License

安装

安装此扩展的最佳方式是通过 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',
            ],
        ],
    ];
}

行为将在保存数据到数据库前自动加密所有数据,并在检索后解密。

请注意,行为将使用扩展的当前配置进行加密。