Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Mar 15, 2022
1 parent bcd5b83 commit 60f8b00
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea/
/.vs/
/.vscode/
/vendor/
/composer.lock
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,109 @@
# Encryption
PHP OpenSSL/Sodium Encryption and Decryption

[![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)

## Requirements

- PHP 7.4 or higher
- MB_String extension
- Depending on usage:
- OpenSSL extesion
- Sodium extension


## Installation

```
composer require initphp/encryption
```

## Configuration

```php
$options = [
'algo' => 'SHA256',
'cipher' => 'AES-256-CTR',
'key' => null,
'blocksize' => 16,
];
```

- `algo` : Used by OpenSSL handler only. The algorithm to use to sign the data.
- `cipher` : Used by OpenSSL handler only. The encryption algorithm that will be used to encrypt the data.
- `key` : The top secret key string to use for encryption.
- `blocksize` : It is used for sodium handler only. It is used in the `sodium_pad()` and `sodium_unpad()` functions.


## Usage

```php
require_once "vendor/autoload.php";
use \InitPHP\Encryption\Encrypt;

// OpenSSL Handler
/** @var $openssl \InitPHP\Encryption\HandlerInterface */
$openssl = Encrypt::use(\InitPHP\Encryption\OpenSSL::class, [
'algo' => 'SHA256',
'cipher' => 'AES-256-CTR',
'key' => 'TOP_Secret_Key',
]);

// Sodium Handler
/** @var $sodium \InitPHP\Encryption\HandlerInterface */
$sodium = Encrypt::use(\InitPHP\Encryption\Sodium::class, [
'key' => 'TOP_Secret_Key',
'blocksize' => 16,
]);
```

### Methods

#### `encrypt()`

```php
public function encrypt(mixed $data, array $options = []): string;
```

#### `decrypt()`

```php
public function decrypt(string $data, array $options = []): mixed;
```

## Writing Your Own Handler

```php
namespace App;

use \InitPHP\Encryption\{HandlerInterface, BaseHandler};

class MyHandler extends BaseHandler implements HandlerInterface
{
public function encrypt($data, array $options = []): string
{
$options = $this->options($options);
// ... process
}

public function decrypt($data, array $options = [])
{
$options = $this->options($options);
// ... process
}
}
```

```php
use \InitPHP\Encryption\Encrypt;

$myhandler = Encrypt::use(\App\MyHandler::class);
```

## Credits

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr)

## License

Copyright © 2022 [MIT License](./LICENSE)
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "initphp/encryption",
"description": "PHP OpenSSL/Sodium Encryption and Decryption",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"InitPHP\\Encryption\\": "src/"
}
},
"authors": [
{
"name": "Muhammet ŞAFAK",
"email": "[email protected]",
"role": "Developer",
"homepage": "https://www.muhammetsafak.com.tr"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=7.4",
"ext-mbstring": "*"
}
}
80 changes: 80 additions & 0 deletions src/BaseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* BaseEncryption.php
*
* This file is part of InitPHP.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\Encryption;

use const CASE_LOWER;

use function array_merge;
use function array_change_key_case;
use function strtolower;
use function mb_substr;

abstract class BaseHandler implements HandlerInterface
{

protected array $options = [
'algo' => 'SHA256',
'cipher' => 'AES-256-CTR',
'key' => null,
'blocksize' => 16,
];

abstract public function encrypt($data, array $options = []): string;

abstract public function decrypt($data, array $options = []);

public function __construct(array $options = [])
{
$this->setOptions($options);
}

public function setOptions(array $options = []): self
{
if(empty($options)){
return $this;
}
$this->options = array_merge($this->options, array_change_key_case($options, CASE_LOWER));
return $this;
}

public function setOption(string $name, $value): self
{
$this->options[strtolower($name)] = $value;
return $this;
}

public function getOption(string $name, $default = null)
{
$name = strtolower($name);
return $this->options[$name] ?? $default;
}

public function getOptions(): array
{
return $this->options ?? [];
}

protected function options(array $options = []): array
{
return empty($options) ? $this->options : array_merge($this->options, array_change_key_case($options, CASE_LOWER));
}

protected function substr($str, int $offset, ?int $length = null): string
{
return mb_substr($str, $offset, $length, '8bit');
}

}
56 changes: 56 additions & 0 deletions src/Encrypt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Encrypt.php
*
* This file is part of InitPHP.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\Encryption;

use \InitPHP\Encryption\Exceptions\EncryptionException;

use function is_string;
use function class_exists;

class Encrypt
{

/**
* @param string|HandlerInterface $handler
* @param array $options
* @return HandlerInterface
* @throws EncryptionException
*/
public static function use($handler, array $options = []): HandlerInterface
{
if(is_string($handler) && class_exists($handler)){
$handler = new $handler($options);
$options = null;
}
if(!($handler instanceof HandlerInterface)){
throw new EncryptionException('');
}
return empty($options) ? $handler : $handler->setOptions($options);
}

/**
* @param string|HandlerInterface $handler
* @param array $options
* @return HandlerInterface
* @throws EncryptionException
*/
public static function create($handler, array $options = []): HandlerInterface
{
return self::use($handler, $options);
}


}
20 changes: 20 additions & 0 deletions src/Exceptions/EncryptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* EncryptionException.php
*
* This file is part of InitPHP.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\Encryption\Exceptions;

class EncryptionException extends \Exception
{
}
27 changes: 27 additions & 0 deletions src/HandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* HandlerInterface.php
*
* This file is part of InitPHP.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\Encryption;

interface HandlerInterface
{

public function encrypt($data, array $options = []): string;

public function decrypt($data, array $options = []);

public function setOptions(array $options = []): HandlerInterface;

}
Loading

0 comments on commit 60f8b00

Please sign in to comment.