mpyw/easycrypt
最新稳定版本:v4.1.3
Composer 安装命令:
composer require mpyw/easycrypt
包简介
A class that provides simple interface for decryptable encryption.
README 文档
README
A class that provides simple interface for decryptable encryption.
Requirements
- PHP:
^8.2
Note
Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.
Installing
composer require mpyw/easycrypt
Usage
Basic
The default cipher method is aes256 (aes-256-cbc).
<?php use Mpyw\EasyCrypt\Cryptor; $cryptor = new Cryptor; $secretData = '[Secret Data]'; $password = '[Password]'; $encrypted = $cryptor->encrypt($secretData, $password); $decrypted = $cryptor->decrypt($encrypted, $password); // String on success, false on failure. var_dump($secretData === $decrypted); // bool(true)
Throw DecryptionFailedException when decryption failed
It throws DecryptionFailedException instead of returning false.
$decrypted = $cryptor->mustDecrypt($encrypted, $password);
Use fixed password
You can use FixedPasswordCryptor instead of raw Cryptor.
This is useful when we use a fixed password from an application config.
<?php use Mpyw\EasyCrypt\FixedPasswordCryptor; $cryptor = new FixedPasswordCryptor('[Password]'); $secretData = '[Secret Data]'; $encrypted = $cryptor->encrypt($secretData); $decrypted = $cryptor->decrypt($encrypted); // String on success, false on failure. var_dump($secretData === $decrypted); // bool(true)
Use AEAD (Authenticated Encryption with Associated Data) suites
If you need to use AEAD suites that adopt CTR mode, it is recommended to provide truly unique counter value.
use Mpyw\EasyCrypt\IvGenerator\IvGeneratorInterface; class Counter implements IvGeneratorInterface { protected \PDO $pdo; public function __construct(\PDO $pdo) { $this->pdo = $pdo; } public function generate(int $length): string { $this->pdo->exec('INSERT INTO counters()'); return $this->pdo->lastInsertId(); } }
<?php use Mpyw\EasyCrypt\Cryptor; $cryptor = new Cryptor('aes-256-gcm', new Counter(new \PDO(...)));
统计信息
- 总下载量: 4.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-09