mpyw / easycrypt
A class that provides simple interface for decryptable encryption.
Installs: 1 901
Dependents: 0
Suggesters: 0
Security: 0
Stars: 23
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: ^7.1 || ^8.0
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^8.5
README
A class that provides simple interface for decryptable encryption.
Requirements
- PHP:
^7.1 || ^8.0
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(...)));