|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Security\Encryption; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Contracts\Security\EncrypterInterface; |
|
15
|
|
|
use BlitzPHP\Exceptions\EncryptionException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Gestionnaire de chiffrement BlitzPHP |
|
19
|
|
|
* |
|
20
|
|
|
* Fournit un cryptage à clé bidirectionnel via les extensions PHP Sodium et/ou OpenSSL. |
|
21
|
|
|
* Cette classe détermine le pilote, le chiffrement et le mode à utiliser, puis initialise le gestionnaire de chiffrement approprié. |
|
22
|
|
|
* |
|
23
|
|
|
* @credit <a href="http://www.codeigniter.com">CodeIgniter 4.4 - \CodeIgniter\Encryption\Encryption</a> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Encryption implements EncrypterInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Le chiffreur que nous créons |
|
29
|
|
|
*/ |
|
30
|
|
|
protected EncrypterInterface $encrypter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Le pilote utilisé |
|
34
|
|
|
*/ |
|
35
|
|
|
protected string $driver; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The key/seed being used |
|
39
|
|
|
*/ |
|
40
|
|
|
protected string $key; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* La clé HMAC dérivée |
|
44
|
|
|
*/ |
|
45
|
|
|
protected string $hmacKey; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* HMAC digest à utiliser |
|
49
|
|
|
*/ |
|
50
|
|
|
protected string $digest = 'SHA512'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Pilotes aux classes de gestionnaires, par ordre de préférence |
|
54
|
|
|
*/ |
|
55
|
|
|
protected array $drivers = [ |
|
56
|
|
|
'OpenSSL', |
|
57
|
|
|
'Sodium', |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gestionnaires à installer |
|
62
|
|
|
* |
|
63
|
|
|
* @var array<string, bool> |
|
64
|
|
|
*/ |
|
65
|
|
|
protected array $handlers = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws EncryptionException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct(protected ?object $config = null) |
|
71
|
|
|
{ |
|
72
|
|
|
$config ??= (object) config('encryption'); |
|
73
|
|
|
|
|
74
|
|
|
$this->config = $config; |
|
75
|
|
|
$this->key = $config->key; |
|
|
|
|
|
|
76
|
|
|
$this->driver = $config->driver; |
|
|
|
|
|
|
77
|
|
|
$this->digest = $config->digest ?? 'SHA512'; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$this->handlers = [ |
|
80
|
|
|
'OpenSSL' => extension_loaded('openssl'), |
|
81
|
|
|
// le SodiumHandler utilise une API (comme sodium_pad) qui n'est disponible que sur la version 1.0.14+ |
|
82
|
|
|
'Sodium' => extension_loaded('sodium') && version_compare(SODIUM_LIBRARY_VERSION, '1.0.14', '>='), |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
if (! in_array($this->driver, $this->drivers, true) || (array_key_exists($this->driver, $this->handlers) && ! $this->handlers[$this->driver])) { |
|
86
|
|
|
throw EncryptionException::noHandlerAvailable($this->driver); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function encrypt(string $data, null|array|string $params = null): string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->encrypter()->encrypt($data, $params); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function decrypt(string $data, null|array|string $params = null): string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->encrypter()->decrypt($data, $params); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Initialiser ou réinitialiser un chiffreur |
|
108
|
|
|
* |
|
109
|
|
|
* @throws EncryptionException |
|
110
|
|
|
*/ |
|
111
|
|
|
public function initialize(?object $config = null): EncrypterInterface |
|
112
|
|
|
{ |
|
113
|
|
|
if ($config) { |
|
114
|
|
|
$this->key = $config->key; |
|
115
|
|
|
$this->driver = $config->driver; |
|
116
|
|
|
$this->digest = $config->digest ?? 'SHA512'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (empty($this->driver)) { |
|
120
|
|
|
throw EncryptionException::noDriverRequested(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (! in_array($this->driver, $this->drivers, true)) { |
|
124
|
|
|
throw EncryptionException::unKnownHandler($this->driver); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (empty($this->key)) { |
|
128
|
|
|
throw EncryptionException::needsStarterKey(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->hmacKey = bin2hex(\hash_hkdf($this->digest, $this->key)); |
|
132
|
|
|
|
|
133
|
|
|
$handlerName = 'BlitzPHP\\Security\\Encryption\\Handlers\\' . $this->driver . 'Handler'; |
|
134
|
|
|
$this->encrypter = new $handlerName($config); |
|
135
|
|
|
|
|
136
|
|
|
return $this->encrypter; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Créer une clé aléatoire |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function createKey(int $length = 32): string |
|
143
|
|
|
{ |
|
144
|
|
|
return random_bytes($length); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Fourni un accès en lecture seule à certaines de nos propriétés |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $key Nom de la propriete |
|
151
|
|
|
* |
|
152
|
|
|
* @return array|bool|int|string|null |
|
153
|
|
|
*/ |
|
154
|
|
|
public function __get($key) |
|
155
|
|
|
{ |
|
156
|
|
|
if ($this->__isset($key)) { |
|
157
|
|
|
return $this->{$key}; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Assure la vérification de certaines de nos propriétés |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $key Nom de la propriete |
|
167
|
|
|
*/ |
|
168
|
|
|
public function __isset($key): bool |
|
169
|
|
|
{ |
|
170
|
|
|
return in_array($key, ['key', 'digest', 'driver', 'drivers'], true); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function encrypter(): EncrypterInterface |
|
174
|
|
|
{ |
|
175
|
|
|
if (null === $this->encrypter) { |
|
176
|
|
|
$this->encrypter = $this->initialize($this->config); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->encrypter; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|