Skip to content

Commit 60f8b00

Browse files
author
=
committed
First commit
1 parent bcd5b83 commit 60f8b00

9 files changed

+492
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea/
2+
/.vs/
3+
/.vscode/
4+
/vendor/
5+
/composer.lock

README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,109 @@
11
# Encryption
22
PHP OpenSSL/Sodium Encryption and Decryption
3+
4+
[![Latest Stable Version](http://poser.pugx.org/initphp/encryption/v)](https://packagist.org/packages/initphp/encryption) [![Total Downloads](http://poser.pugx.org/initphp/encryption/downloads)](https://packagist.org/packages/initphp/encryption) [![Latest Unstable Version](http://poser.pugx.org/initphp/encryption/v/unstable)](https://packagist.org/packages/initphp/encryption) [![License](http://poser.pugx.org/initphp/encryption/license)](https://packagist.org/packages/initphp/encryption) [![PHP Version Require](http://poser.pugx.org/initphp/encryption/require/php)](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)

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "initphp/encryption",
3+
"description": "PHP OpenSSL/Sodium Encryption and Decryption",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"InitPHP\\Encryption\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Muhammet ŞAFAK",
14+
"email": "[email protected]",
15+
"role": "Developer",
16+
"homepage": "https://www.muhammetsafak.com.tr"
17+
}
18+
],
19+
"minimum-stability": "stable",
20+
"require": {
21+
"php": ">=7.4",
22+
"ext-mbstring": "*"
23+
}
24+
}

src/BaseHandler.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* BaseEncryption.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Encryption;
17+
18+
use const CASE_LOWER;
19+
20+
use function array_merge;
21+
use function array_change_key_case;
22+
use function strtolower;
23+
use function mb_substr;
24+
25+
abstract class BaseHandler implements HandlerInterface
26+
{
27+
28+
protected array $options = [
29+
'algo' => 'SHA256',
30+
'cipher' => 'AES-256-CTR',
31+
'key' => null,
32+
'blocksize' => 16,
33+
];
34+
35+
abstract public function encrypt($data, array $options = []): string;
36+
37+
abstract public function decrypt($data, array $options = []);
38+
39+
public function __construct(array $options = [])
40+
{
41+
$this->setOptions($options);
42+
}
43+
44+
public function setOptions(array $options = []): self
45+
{
46+
if(empty($options)){
47+
return $this;
48+
}
49+
$this->options = array_merge($this->options, array_change_key_case($options, CASE_LOWER));
50+
return $this;
51+
}
52+
53+
public function setOption(string $name, $value): self
54+
{
55+
$this->options[strtolower($name)] = $value;
56+
return $this;
57+
}
58+
59+
public function getOption(string $name, $default = null)
60+
{
61+
$name = strtolower($name);
62+
return $this->options[$name] ?? $default;
63+
}
64+
65+
public function getOptions(): array
66+
{
67+
return $this->options ?? [];
68+
}
69+
70+
protected function options(array $options = []): array
71+
{
72+
return empty($options) ? $this->options : array_merge($this->options, array_change_key_case($options, CASE_LOWER));
73+
}
74+
75+
protected function substr($str, int $offset, ?int $length = null): string
76+
{
77+
return mb_substr($str, $offset, $length, '8bit');
78+
}
79+
80+
}

src/Encrypt.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Encrypt.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Encryption;
17+
18+
use \InitPHP\Encryption\Exceptions\EncryptionException;
19+
20+
use function is_string;
21+
use function class_exists;
22+
23+
class Encrypt
24+
{
25+
26+
/**
27+
* @param string|HandlerInterface $handler
28+
* @param array $options
29+
* @return HandlerInterface
30+
* @throws EncryptionException
31+
*/
32+
public static function use($handler, array $options = []): HandlerInterface
33+
{
34+
if(is_string($handler) && class_exists($handler)){
35+
$handler = new $handler($options);
36+
$options = null;
37+
}
38+
if(!($handler instanceof HandlerInterface)){
39+
throw new EncryptionException('');
40+
}
41+
return empty($options) ? $handler : $handler->setOptions($options);
42+
}
43+
44+
/**
45+
* @param string|HandlerInterface $handler
46+
* @param array $options
47+
* @return HandlerInterface
48+
* @throws EncryptionException
49+
*/
50+
public static function create($handler, array $options = []): HandlerInterface
51+
{
52+
return self::use($handler, $options);
53+
}
54+
55+
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* EncryptionException.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Encryption\Exceptions;
17+
18+
class EncryptionException extends \Exception
19+
{
20+
}

src/HandlerInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* HandlerInterface.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\Encryption;
17+
18+
interface HandlerInterface
19+
{
20+
21+
public function encrypt($data, array $options = []): string;
22+
23+
public function decrypt($data, array $options = []);
24+
25+
public function setOptions(array $options = []): HandlerInterface;
26+
27+
}

0 commit comments

Comments
 (0)