https://github.com/mpyw/easycrypt
A class that provides simple interface for decryptable encryption.
https://github.com/mpyw/easycrypt
encryption php
Last synced: over 1 year ago
JSON representation
A class that provides simple interface for decryptable encryption.
- Host: GitHub
- URL: https://github.com/mpyw/easycrypt
- Owner: mpyw
- License: mit
- Created: 2013-08-29T08:10:10.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2023-03-13T04:48:35.000Z (over 3 years ago)
- Last Synced: 2024-10-13T13:07:48.212Z (over 1 year ago)
- Topics: encryption, php
- Language: PHP
- Homepage:
- Size: 60.5 KB
- Stars: 23
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EasyCrypt [](https://github.com/mpyw/EasyCrypt/actions) [](https://coveralls.io/github/mpyw/EasyCrypt?branch=master)
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
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.
```php
$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
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.
```php
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