-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lablnet
committed
Feb 6, 2019
0 parents
commit 858a8c4
Showing
8 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Malik Umer Farooq | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "lablnet/encryption", | ||
"description": "PHP Encryption package.", | ||
"keywords": ["php", "encryption","psr-3","free","package","Lablnet"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Malik Umer Farooq", | ||
"email":"[email protected]", | ||
"homepage": "https://softhub99.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Lablnet\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
use Lablnet\Encryption; | ||
require '../vendor/autoload.php'; | ||
|
||
$encryption = new Encryption(); | ||
|
||
//Encrypt the message | ||
$encrypt = $encryption->encrypt("This is a text"); | ||
|
||
var_dump($encrypt); | ||
echo "<br\>"; | ||
|
||
//Decrypt the message | ||
$decrypt = $encryption->decrypt($encrypt); | ||
var_dump($decrypt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# PHP Encryption | ||
Encryption in PHP. | ||
|
||
## Requirement | ||
1. PHP 7 (7.3 Recommanded). | ||
2. Composer. | ||
3. openSSL php extension. | ||
4. Sodium php extension for use sodium adapter. | ||
|
||
## Insallation | ||
Installing this package is very simple, first ensure you have the right PHP version and composer installed then in your terminal/(command prompt) run: | ||
``` composer require lablnet/encryption ``` | ||
|
||
|
||
## Encrypt | ||
You can encrypt string by calling to encrypt method | ||
|
||
```php | ||
<?php | ||
use Lablnet\Encryption; | ||
require '../vendor/autoload.php'; | ||
|
||
$encryption = new Encryption(); | ||
|
||
//Encrypt the message | ||
$encrypt = $encryption->encrypt("This is a text"); | ||
|
||
echo $encrypt; | ||
``` | ||
|
||
### Decrypt | ||
You can decrypt token by calling decrypt method | ||
|
||
```php | ||
<?php | ||
use Lablnet\Encryption; | ||
require '../vendor/autoload.php'; | ||
|
||
$encryption = new Encryption(); | ||
|
||
//Decrypt the message | ||
$decrypt = $encryption->decrypt($encrypt); | ||
echo $decrypt; | ||
``` | ||
|
||
### Adapter | ||
This Package support two encryption adapter | ||
- OpenSSL | ||
- Sodium | ||
|
||
Default openSSL will use, | ||
you can use any one you want. | ||
|
||
### change Adapter | ||
You can pass supported adapter to class like | ||
|
||
Use of sodium | ||
```php | ||
<?php | ||
use Lablnet\Encryption; | ||
require '../vendor/autoload.php'; | ||
|
||
$encryption = new Encryption('sodium'); | ||
``` | ||
Use of openSSL | ||
```php | ||
<?php | ||
use Lablnet\Encryption; | ||
require '../vendor/autoload.php'; | ||
|
||
$encryption = new Encryption('openssl'); | ||
|
||
//You can also provide your own key for openSSL | ||
$encryption1 = new Encryption('openssl','my-key'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zest Framework. | ||
* | ||
* @author Malik Umer Farooq <[email protected]> | ||
* @author-profile https://www.facebook.com/malikumerfarooq01/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @license MIT | ||
*/ | ||
|
||
namespace Lablnet\Adapter; | ||
|
||
abstract class AbstractAdapter | ||
{ | ||
/** | ||
* Store the secret key. | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @var key | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* Encrypt the message. | ||
* | ||
* @param (mixed) $data data to be encrypted | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @return mixed | ||
*/ | ||
abstract public function encrypt($data); | ||
|
||
/** | ||
* Decrypt the message. | ||
* | ||
* @param (mixed) $token encrypted token | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @return mixed | ||
*/ | ||
abstract public function decrypt($token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zest Framework. | ||
* | ||
* @author Malik Umer Farooq <[email protected]> | ||
* @author-profile https://www.facebook.com/malikumerfarooq01/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @license MIT | ||
*/ | ||
|
||
namespace Lablnet\Adapter; | ||
|
||
class OpenSslEncryption | ||
{ | ||
/** | ||
* Store the secret key. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @var key | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* Store the cipher iv. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @var string | ||
*/ | ||
private $iv; | ||
|
||
/** | ||
* Cipher. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @var string | ||
*/ | ||
private $cipher = 'AES-256-CBC'; | ||
|
||
/** | ||
* __Construct. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($key = null) | ||
{ | ||
if (isset($key)) { | ||
$this->iv = openssl_random_pseudo_bytes($this->iv_bytes($this->cipher)); | ||
$this->key = hash('sha512', $key); | ||
} else { | ||
throw new \Exception('Crypto key not found', 500); | ||
} | ||
} | ||
|
||
/** | ||
* Encrypt the message. | ||
* | ||
* @param $data => data to be encrypted | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return token | ||
*/ | ||
public function encrypt($data) | ||
{ | ||
return base64_encode(openssl_encrypt($data, $this->cipher, $this->key, 0, $this->iv).'&&'.bin2hex($this->iv)); | ||
} | ||
|
||
/** | ||
* Decrypt the message. | ||
* | ||
* @param $token => encrypted token | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return mix-data | ||
*/ | ||
public function decrypt($token) | ||
{ | ||
$token = base64_decode($token); | ||
list($token, $this->iv) = explode('&&', $token); | ||
|
||
return openssl_decrypt($token, $this->cipher, $this->key, 0, hex2bin($this->iv)); | ||
} | ||
|
||
/** | ||
* Get the length of cipher. | ||
* | ||
* @param $method | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @return int | ||
*/ | ||
protected function iv_bytes($method) | ||
{ | ||
return openssl_cipher_iv_length($method); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Zest Framework. | ||
* | ||
* @author Malik Umer Farooq <[email protected]> | ||
* @author-profile https://www.facebook.com/malikumerfarooq01/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @license MIT | ||
*/ | ||
|
||
namespace Lablnet\Adapter; | ||
|
||
class SodiumEncryption extends AbstractAdapter | ||
{ | ||
/** | ||
* __Construct. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public function __construct($key = null) | ||
{ | ||
if (!function_exists('sodium_crypto_secretbox_keygen')) { | ||
throw new \Exception('The sodium php extension does not installed or enabled', 500); | ||
} | ||
|
||
$this->key = sodium_crypto_secretbox_keygen(); | ||
} | ||
|
||
/** | ||
* Encrypt the message. | ||
* | ||
* @param (mixed) $data data to be encrypted | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @return mixed | ||
*/ | ||
public function encrypt($data) | ||
{ | ||
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); | ||
$token = base64_encode($nonce.sodium_crypto_secretbox($data, $nonce, $this->key).'&&'.$this->key); | ||
|
||
return $token; | ||
} | ||
|
||
/** | ||
* Decrypt the message. | ||
* | ||
* @param (mixed) $token encrypted token | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @return mixed | ||
*/ | ||
public function decrypt($token) | ||
{ | ||
$decoded = base64_decode($token); | ||
list($decoded, $this->key) = explode('&&', $decoded); | ||
if ($decoded === false) { | ||
throw new Exception('The decoding failed'); | ||
} | ||
if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { | ||
throw new \Exception('The token was truncated'); | ||
} | ||
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit'); | ||
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); | ||
|
||
$plain = sodium_crypto_secretbox_open($ciphertext, | ||
$nonce, $this->key); | ||
|
||
if ($plain === false) { | ||
throw new \Exception('The message was tampered with in transit'); | ||
} | ||
|
||
return $plain; | ||
} | ||
} |
Oops, something went wrong.