-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEncryption.php
More file actions
185 lines (158 loc) · 4.48 KB
/
Encryption.php
File metadata and controls
185 lines (158 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Encryption;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use Config\Encryption as EncryptionConfig;
/**
* CodeIgniter Encryption Manager
*
* Provides two-way keyed encryption via PHP's Sodium and/or OpenSSL extensions.
* This class determines the driver, cipher, and mode to use, and then
* initializes the appropriate encryption handler.
*
* @property-read string $digest
* @property-read string $driver
* @property-read list<string> $drivers
* @property-read string $key
*
* @see \CodeIgniter\Encryption\EncryptionTest
*/
class Encryption
{
/**
* The encrypter we create
*
* @var EncrypterInterface
*/
protected $encrypter;
/**
* The driver being used
*
* @var string
*/
protected $driver;
/**
* The key/seed being used
*
* @var string
*/
protected $key;
/**
* The derived HMAC key
*
* @var string
*/
protected $hmacKey;
/**
* HMAC digest to use
*
* @var string
*/
protected $digest = 'SHA512';
/**
* Map of drivers to handler classes, in preference order
*
* @var array
*/
protected $drivers = [
'OpenSSL',
'Sodium',
];
/**
* Handlers that are to be installed
*
* @var array<string, bool>
*/
protected $handlers = [];
/**
* @throws EncryptionException
*/
public function __construct(?EncryptionConfig $config = null)
{
$config ??= new EncryptionConfig();
$this->key = $config->key;
$this->driver = $config->driver;
$this->digest = $config->digest ?? 'SHA512';
$this->handlers = [
'OpenSSL' => extension_loaded('openssl'),
// the SodiumHandler uses some API (like sodium_pad) that is available only on v1.0.14+
'Sodium' => extension_loaded('sodium') && version_compare(SODIUM_LIBRARY_VERSION, '1.0.14', '>='),
];
if (! in_array($this->driver, $this->drivers, true) || (array_key_exists($this->driver, $this->handlers) && ! $this->handlers[$this->driver])) {
throw EncryptionException::forNoHandlerAvailable($this->driver);
}
}
/**
* Initialize or re-initialize an encrypter
*
* @return EncrypterInterface
*
* @throws EncryptionException
*/
public function initialize(?EncryptionConfig $config = null)
{
if ($config instanceof EncryptionConfig) {
$this->key = $config->key;
$this->driver = $config->driver;
$this->digest = $config->digest ?? 'SHA512';
}
if (empty($this->driver)) {
throw EncryptionException::forNoDriverRequested();
}
if (! in_array($this->driver, $this->drivers, true)) {
throw EncryptionException::forUnKnownHandler($this->driver);
}
if (empty($this->key)) {
throw EncryptionException::forNeedsStarterKey();
}
$this->hmacKey = bin2hex(\hash_hkdf($this->digest, $this->key));
$handlerName = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler';
$this->encrypter = new $handlerName($config);
if (($config->previousKeys ?? []) !== []) {
$this->encrypter = new KeyRotationDecorator($this->encrypter, $config->previousKeys);
}
return $this->encrypter;
}
/**
* Create a random key
*
* @param int $length Output length
*
* @return string
*/
public static function createKey($length = 32)
{
return random_bytes($length);
}
/**
* __get() magic, providing readonly access to some of our protected properties
*
* @param string $key Property name
*
* @return array|string|null
*/
public function __get($key)
{
if ($this->__isset($key)) {
return $this->{$key};
}
return null;
}
/**
* __isset() magic, providing checking for some of our protected properties
*
* @param string $key Property name
*/
public function __isset($key): bool
{
return in_array($key, ['key', 'digest', 'driver', 'drivers'], true);
}
}