|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Encryption\Adapters; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Encryption\Contracts\EncryptionInterface; |
|
18
|
|
|
use Quantum\Libraries\Encryption\Exceptions\CryptorException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AsymmetricEncryptionAdapter |
|
22
|
|
|
* @package Quantum\Libraries\Encryption |
|
23
|
|
|
*/ |
|
24
|
|
|
class AsymmetricEncryptionAdapter implements EncryptionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Digest algorithm |
|
29
|
|
|
*/ |
|
30
|
|
|
const DIGEST_ALGO = 'SHA512'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Key bits |
|
34
|
|
|
*/ |
|
35
|
|
|
const KEY_BITS = 1024; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $publicKey; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $privateKey; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws CryptorException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
|
|
$keyPair = $this->generateKeyPair(); |
|
53
|
|
|
|
|
54
|
|
|
$this->publicKey = $keyPair['public']; |
|
55
|
|
|
$this->privateKey = $keyPair['private']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $plain |
|
60
|
|
|
* @return string |
|
61
|
|
|
* @throws CryptorException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function encrypt(string $plain): string |
|
64
|
|
|
{ |
|
65
|
|
|
if(!$this->publicKey) { |
|
66
|
|
|
throw CryptorException::publicKeyNotProvided(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
openssl_public_encrypt($plain, $encrypted, $this->publicKey); |
|
70
|
|
|
return base64_encode($encrypted); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $encrypted |
|
75
|
|
|
* @return string |
|
76
|
|
|
* @throws CryptorException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function decrypt(string $encrypted): string |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$this->privateKey) { |
|
81
|
|
|
throw CryptorException::privateKeyNotProvided(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $this->privateKey); |
|
85
|
|
|
return $decrypted; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
* @throws CryptorException |
|
91
|
|
|
*/ |
|
92
|
|
|
private function generateKeyPair(): array |
|
93
|
|
|
{ |
|
94
|
|
|
$resource = openssl_pkey_new([ |
|
95
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA, |
|
96
|
|
|
'private_key_bits' => self::KEY_BITS, |
|
97
|
|
|
'digest_alg' => self::DIGEST_ALGO, |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
if (!$resource) { |
|
101
|
|
|
throw CryptorException::configNotFound(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
openssl_pkey_export($resource, $privateKey); |
|
105
|
|
|
$publicKey = openssl_pkey_get_details($resource)['key']; |
|
106
|
|
|
|
|
107
|
|
|
return [ |
|
108
|
|
|
'private' => $privateKey, |
|
109
|
|
|
'public' => $publicKey |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
} |