|
1 | 1 | # Encryption |
2 | 2 | PHP OpenSSL/Sodium Encryption and Decryption |
| 3 | + |
| 4 | +[](https://packagist.org/packages/initphp/encryption) [](https://packagist.org/packages/initphp/encryption) [](https://packagist.org/packages/initphp/encryption) [](https://packagist.org/packages/initphp/encryption) [](https://packagist.org/packages/initphp/encryption) |
| 5 | + |
| 6 | +## Requirements |
| 7 | + |
| 8 | +- PHP 7.4 or higher |
| 9 | +- MB_String extension |
| 10 | +- Depending on usage: |
| 11 | + - OpenSSL extesion |
| 12 | + - Sodium extension |
| 13 | + |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +``` |
| 18 | +composer require initphp/encryption |
| 19 | +``` |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +```php |
| 24 | +$options = [ |
| 25 | + 'algo' => 'SHA256', |
| 26 | + 'cipher' => 'AES-256-CTR', |
| 27 | + 'key' => null, |
| 28 | + 'blocksize' => 16, |
| 29 | +]; |
| 30 | +``` |
| 31 | + |
| 32 | +- `algo` : Used by OpenSSL handler only. The algorithm to use to sign the data. |
| 33 | +- `cipher` : Used by OpenSSL handler only. The encryption algorithm that will be used to encrypt the data. |
| 34 | +- `key` : The top secret key string to use for encryption. |
| 35 | +- `blocksize` : It is used for sodium handler only. It is used in the `sodium_pad()` and `sodium_unpad()` functions. |
| 36 | + |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +```php |
| 41 | +require_once "vendor/autoload.php"; |
| 42 | +use \InitPHP\Encryption\Encrypt; |
| 43 | + |
| 44 | +// OpenSSL Handler |
| 45 | +/** @var $openssl \InitPHP\Encryption\HandlerInterface */ |
| 46 | +$openssl = Encrypt::use(\InitPHP\Encryption\OpenSSL::class, [ |
| 47 | + 'algo' => 'SHA256', |
| 48 | + 'cipher' => 'AES-256-CTR', |
| 49 | + 'key' => 'TOP_Secret_Key', |
| 50 | +]); |
| 51 | + |
| 52 | +// Sodium Handler |
| 53 | +/** @var $sodium \InitPHP\Encryption\HandlerInterface */ |
| 54 | +$sodium = Encrypt::use(\InitPHP\Encryption\Sodium::class, [ |
| 55 | + 'key' => 'TOP_Secret_Key', |
| 56 | + 'blocksize' => 16, |
| 57 | +]); |
| 58 | +``` |
| 59 | + |
| 60 | +### Methods |
| 61 | + |
| 62 | +#### `encrypt()` |
| 63 | + |
| 64 | +```php |
| 65 | +public function encrypt(mixed $data, array $options = []): string; |
| 66 | +``` |
| 67 | + |
| 68 | +#### `decrypt()` |
| 69 | + |
| 70 | +```php |
| 71 | +public function decrypt(string $data, array $options = []): mixed; |
| 72 | +``` |
| 73 | + |
| 74 | +## Writing Your Own Handler |
| 75 | + |
| 76 | +```php |
| 77 | +namespace App; |
| 78 | + |
| 79 | +use \InitPHP\Encryption\{HandlerInterface, BaseHandler}; |
| 80 | + |
| 81 | +class MyHandler extends BaseHandler implements HandlerInterface |
| 82 | +{ |
| 83 | + public function encrypt($data, array $options = []): string |
| 84 | + { |
| 85 | + $options = $this->options($options); |
| 86 | + // ... process |
| 87 | + } |
| 88 | + |
| 89 | + public function decrypt($data, array $options = []) |
| 90 | + { |
| 91 | + $options = $this->options($options); |
| 92 | + // ... process |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +```php |
| 98 | +use \InitPHP\Encryption\Encrypt; |
| 99 | + |
| 100 | +$myhandler = Encrypt::use(\App\MyHandler::class); |
| 101 | +``` |
| 102 | + |
| 103 | +## Credits |
| 104 | + |
| 105 | +- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +Copyright © 2022 [MIT License](./LICENSE) |
0 commit comments