misere
nkov / yii2-security
基于公私钥的openssl加密的Yii2扩展
1.0.1
2020-01-06 11:52 UTC
Requires
- php: >=5.6
- ext-openssl: *
- yiisoft/yii2: ^2.0.13
Requires (Dev)
- codeception/codeception: ^2.2
Suggests
- ext-openssl: This library needs OpenSSL
This package is auto-updated.
Last update: 2024-09-16 03:22:40 UTC
README
基于openssl公私钥进行加密和解密的Yii2扩展
支持
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist miserenkov/yii2-security "^1.0"
或者在您的 composer.json
文件的require部分添加以下内容:
"miserenkov/yii2-security": "^1.0"
配置
要使用安全扩展,您应该在应用程序配置中配置它,如下所示
'components' => [ ... 'security' => [ 'class' => 'miserenkov\security\Security', 'certificateFile' => '', // alias or path to default certificate file // or 'publicKeyFile' => '', // alias or path to default public key file 'privateKeyFile' => '', // alias or path to default private key file 'passphrase' => '', // passphrase to default private key (if exists) ], ... ],
基本用法
加密
非对称加密
将返回base64编码的加密数据。
使用默认公钥
Yii::$app->security->encryptByPublicKey( $data // string data for ecnryption );
使用自定义公钥
Yii::$app->security->encryptByPublicKey( $data, // string data for ecnryption $publicKey // alias or path to custom public key or PEM formatted public key );
混合加密
将返回一个包含加密密钥和加密数据的数组(['key' => '...', 'data' => '...'])。
使用默认公钥
Yii::$app->security->encryptHybrid( $data // string data for ecnryption );
使用自定义公钥
Yii::$app->security->encryptHybrid( $data, // string data for ecnryption $publicKey // alias or path to custom public key or PEM formatted public key );
解密
非对称解密
将返回解密数据。
使用默认私钥
Yii::$app->security->decryptByPrivateKey( $data // string data for decryption );
使用自定义私钥
Yii::$app->security->decryptByPrivateKey( $data, // string data for decryption $privateKey, // alias or path to custom private key or PEM formatted private key $passphrase // passphrase for private key (if exists) );
混合加密
将返回解密数据。
使用默认私钥
Yii::$app->security->decryptHybrid( $data, // string data for decryption or array from // encryption key and ecnrypted data (['key' => '...', 'data' => '...']) $key // encryption key if $data as string );
使用自定义私钥
Yii::$app->security->decryptHybrid( $data, //string data for decryption or array from // encryption key and ecnrypted data (['key' => '...', 'data' => '...']) $key, // encryption key if $data as string $privateKey, // alias or path to custom private key or PEM formatted private key $passphrase // passphrase for private key (if exists) );