Skip to content

Commit 858a8c4

Browse files
author
Lablnet
committed
first
0 parents  commit 858a8c4

File tree

8 files changed

+493
-0
lines changed

8 files changed

+493
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Malik Umer Farooq
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "lablnet/encryption",
3+
"description": "PHP Encryption package.",
4+
"keywords": ["php", "encryption","psr-3","free","package","Lablnet"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Malik Umer Farooq",
9+
"email":"lablnet01@gmail.com",
10+
"homepage": "https://softhub99.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"Lablnet\\": "src/"
16+
}
17+
}
18+
}

example/index.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
use Lablnet\Encryption;
3+
require '../vendor/autoload.php';
4+
5+
$encryption = new Encryption();
6+
7+
//Encrypt the message
8+
$encrypt = $encryption->encrypt("This is a text");
9+
10+
var_dump($encrypt);
11+
echo "<br\>";
12+
13+
//Decrypt the message
14+
$decrypt = $encryption->decrypt($encrypt);
15+
var_dump($decrypt);

readme.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# PHP Encryption
2+
Encryption in PHP.
3+
4+
## Requirement
5+
1. PHP 7 (7.3 Recommanded).
6+
2. Composer.
7+
3. openSSL php extension.
8+
4. Sodium php extension for use sodium adapter.
9+
10+
## Insallation
11+
Installing this package is very simple, first ensure you have the right PHP version and composer installed then in your terminal/(command prompt) run:
12+
``` composer require lablnet/encryption ```
13+
14+
15+
## Encrypt
16+
You can encrypt string by calling to encrypt method
17+
18+
```php
19+
<?php
20+
use Lablnet\Encryption;
21+
require '../vendor/autoload.php';
22+
23+
$encryption = new Encryption();
24+
25+
//Encrypt the message
26+
$encrypt = $encryption->encrypt("This is a text");
27+
28+
echo $encrypt;
29+
```
30+
31+
### Decrypt
32+
You can decrypt token by calling decrypt method
33+
34+
```php
35+
<?php
36+
use Lablnet\Encryption;
37+
require '../vendor/autoload.php';
38+
39+
$encryption = new Encryption();
40+
41+
//Decrypt the message
42+
$decrypt = $encryption->decrypt($encrypt);
43+
echo $decrypt;
44+
```
45+
46+
### Adapter
47+
This Package support two encryption adapter
48+
- OpenSSL
49+
- Sodium
50+
51+
Default openSSL will use,
52+
you can use any one you want.
53+
54+
### change Adapter
55+
You can pass supported adapter to class like
56+
57+
Use of sodium
58+
```php
59+
<?php
60+
use Lablnet\Encryption;
61+
require '../vendor/autoload.php';
62+
63+
$encryption = new Encryption('sodium');
64+
```
65+
Use of openSSL
66+
```php
67+
<?php
68+
use Lablnet\Encryption;
69+
require '../vendor/autoload.php';
70+
71+
$encryption = new Encryption('openssl');
72+
73+
//You can also provide your own key for openSSL
74+
$encryption1 = new Encryption('openssl','my-key');
75+
```

src/Adapter/AbstractAdapter.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Zest Framework.
5+
*
6+
* @author Malik Umer Farooq <lablnet01@gmail.com>
7+
* @author-profile https://www.facebook.com/malikumerfarooq01/
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*
12+
* @since 3.0.0
13+
*
14+
* @license MIT
15+
*/
16+
17+
namespace Lablnet\Adapter;
18+
19+
abstract class AbstractAdapter
20+
{
21+
/**
22+
* Store the secret key.
23+
*
24+
* @since 3.0.0
25+
*
26+
* @var key
27+
*/
28+
private $key;
29+
30+
/**
31+
* Encrypt the message.
32+
*
33+
* @param (mixed) $data data to be encrypted
34+
*
35+
* @since 3.0.0
36+
*
37+
* @return mixed
38+
*/
39+
abstract public function encrypt($data);
40+
41+
/**
42+
* Decrypt the message.
43+
*
44+
* @param (mixed) $token encrypted token
45+
*
46+
* @since 3.0.0
47+
*
48+
* @return mixed
49+
*/
50+
abstract public function decrypt($token);
51+
}

src/Adapter/OpenSslEncryption.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Zest Framework.
5+
*
6+
* @author Malik Umer Farooq <lablnet01@gmail.com>
7+
* @author-profile https://www.facebook.com/malikumerfarooq01/
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*
12+
* @since 1.0.0
13+
*
14+
* @license MIT
15+
*/
16+
17+
namespace Lablnet\Adapter;
18+
19+
class OpenSslEncryption
20+
{
21+
/**
22+
* Store the secret key.
23+
*
24+
* @since 1.0.0
25+
*
26+
* @var key
27+
*/
28+
private $key;
29+
30+
/**
31+
* Store the cipher iv.
32+
*
33+
* @since 1.0.0
34+
*
35+
* @var string
36+
*/
37+
private $iv;
38+
39+
/**
40+
* Cipher.
41+
*
42+
* @since 1.0.0
43+
*
44+
* @var string
45+
*/
46+
private $cipher = 'AES-256-CBC';
47+
48+
/**
49+
* __Construct.
50+
*
51+
* @since 1.0.0
52+
*
53+
* @return void
54+
*/
55+
public function __construct($key = null)
56+
{
57+
if (isset($key)) {
58+
$this->iv = openssl_random_pseudo_bytes($this->iv_bytes($this->cipher));
59+
$this->key = hash('sha512', $key);
60+
} else {
61+
throw new \Exception('Crypto key not found', 500);
62+
}
63+
}
64+
65+
/**
66+
* Encrypt the message.
67+
*
68+
* @param $data => data to be encrypted
69+
*
70+
* @since 1.0.0
71+
*
72+
* @return token
73+
*/
74+
public function encrypt($data)
75+
{
76+
return base64_encode(openssl_encrypt($data, $this->cipher, $this->key, 0, $this->iv).'&&'.bin2hex($this->iv));
77+
}
78+
79+
/**
80+
* Decrypt the message.
81+
*
82+
* @param $token => encrypted token
83+
*
84+
* @since 1.0.0
85+
*
86+
* @return mix-data
87+
*/
88+
public function decrypt($token)
89+
{
90+
$token = base64_decode($token);
91+
list($token, $this->iv) = explode('&&', $token);
92+
93+
return openssl_decrypt($token, $this->cipher, $this->key, 0, hex2bin($this->iv));
94+
}
95+
96+
/**
97+
* Get the length of cipher.
98+
*
99+
* @param $method
100+
*
101+
* @since 3.0.0
102+
*
103+
* @return int
104+
*/
105+
protected function iv_bytes($method)
106+
{
107+
return openssl_cipher_iv_length($method);
108+
}
109+
}

src/Adapter/SodiumEncryption.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Zest Framework.
5+
*
6+
* @author Malik Umer Farooq <lablnet01@gmail.com>
7+
* @author-profile https://www.facebook.com/malikumerfarooq01/
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*
12+
* @since 3.0.0
13+
*
14+
* @license MIT
15+
*/
16+
17+
namespace Lablnet\Adapter;
18+
19+
class SodiumEncryption extends AbstractAdapter
20+
{
21+
/**
22+
* __Construct.
23+
*
24+
* @since 3.0.0
25+
*/
26+
public function __construct($key = null)
27+
{
28+
if (!function_exists('sodium_crypto_secretbox_keygen')) {
29+
throw new \Exception('The sodium php extension does not installed or enabled', 500);
30+
}
31+
32+
$this->key = sodium_crypto_secretbox_keygen();
33+
}
34+
35+
/**
36+
* Encrypt the message.
37+
*
38+
* @param (mixed) $data data to be encrypted
39+
*
40+
* @since 3.0.0
41+
*
42+
* @return mixed
43+
*/
44+
public function encrypt($data)
45+
{
46+
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
47+
$token = base64_encode($nonce.sodium_crypto_secretbox($data, $nonce, $this->key).'&&'.$this->key);
48+
49+
return $token;
50+
}
51+
52+
/**
53+
* Decrypt the message.
54+
*
55+
* @param (mixed) $token encrypted token
56+
*
57+
* @since 3.0.0
58+
*
59+
* @return mixed
60+
*/
61+
public function decrypt($token)
62+
{
63+
$decoded = base64_decode($token);
64+
list($decoded, $this->key) = explode('&&', $decoded);
65+
if ($decoded === false) {
66+
throw new Exception('The decoding failed');
67+
}
68+
if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
69+
throw new \Exception('The token was truncated');
70+
}
71+
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
72+
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
73+
74+
$plain = sodium_crypto_secretbox_open($ciphertext,
75+
$nonce, $this->key);
76+
77+
if ($plain === false) {
78+
throw new \Exception('The message was tampered with in transit');
79+
}
80+
81+
return $plain;
82+
}
83+
}

0 commit comments

Comments
 (0)