This repository was archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathEncryptionHelper.php
376 lines (322 loc) · 9.35 KB
/
EncryptionHelper.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* src/EncryptionHelper.php.
*
* @author Austin Heap <me@austinheap.com>
* @version v0.2.1
*/
declare(strict_types=1);
namespace AustinHeap\Database\Encryption;
use Illuminate\Support\Facades\Config;
use RuntimeException;
/**
* EncryptionHelper.
*
* @link https://github.com/austinheap/laravel-database-encryption
* @link https://packagist.org/packages/austinheap/laravel-database-encryption
* @link https://austinheap.github.io/laravel-database-encryption/classes/AustinHeap.Database.Encryption.EncryptionHelper.html
*/
class EncryptionHelper extends EncryptionDefaults
{
/**
* Internal version number.
*
* @var string
*/
public const VERSION = '0.1.2';
/**
* Private enable flag cache.
*
* @var null|bool
*/
private $enabledCache = null;
/**
* Private versioning flag cache.
*
* @var null|bool
*/
private $versioningCache = null;
/**
* Private version parts cache.
*
* @var null|array
*/
private $versionPartsCache = null;
/**
* Private control characters cache.
*
* @var null|array
*/
private $controlCharactersCache = null;
/**
* Private prefix cache.
*
* @var null|string
*/
private $prefixCache = null;
/**
* Private header prefix cache.
*
* @var null|string
*/
private $headerPrefixCache = null;
/**
* EncryptionHelper constructor.
*/
public function __construct()
{
$this->reset();
}
/**
* Reset the class; mostly the cache.
*
* @return EncryptionHelper
*/
public function reset(): self
{
$this->enabledCache =
$this->versioningCache =
$this->versionPartsCache =
$this->controlCharactersCache =
$this->prefixCache =
$this->headerPrefixCache = null;
return $this;
}
/**
* Get the package version.
*
* @return string
*/
public function getVersion(): string
{
throw_if(! defined('LARAVEL_DATABASE_ENCRYPTION_VERSION'), RuntimeException::class,
'The provider did not boot.');
return LARAVEL_DATABASE_ENCRYPTION_VERSION;
}
/**
* Set the enabled flag.
*
* @return bool
*/
public function setEnabled(?bool $value = null): self
{
$this->enabledCache = $value;
return $this;
}
/**
* Check the enabled flag.
*
* @return bool
*/
public function isEnabled(): bool
{
if (is_null($this->enabledCache)) {
$enabled = Config('database-encryption.enabled', null);
$this->enabledCache = ! is_null($enabled) && is_bool($enabled) ? $enabled : self::isEnabledDefault();
}
return $this->enabledCache;
}
/**
* Set the enabled flag inverse.
*
* @return bool
*/
public function setDisabled(?bool $value = null): self
{
return $this->setEnabled(is_bool($value) ? ! $value : null);
}
/**
* Check the enabled flag inverse.
*
* @return bool
*/
public function isDisabled(): bool
{
return ! $this->isEnabled();
}
/**
* Set the versioning flag.
*
* @return null|bool
*/
public function setVersioning(?bool $value = null): self
{
$this->reset();
$this->versioningCache = $value;
return $this;
}
/**
* Check the versioning flag.
*
* @return bool
*/
public function isVersioning(): bool
{
if (is_null($this->versioningCache)) {
$versioning = Config('database-encryption.versioning', null);
$this->versioningCache = ! is_null($versioning) && is_bool($versioning) ? $versioning : self::isVersioningDefault();
}
return $this->versioningCache;
}
/**
* Set the enabled flag inverse.
*
* @return null|bool
*/
public function setVersionless(?bool $value = null): self
{
return $this->setVersioning(is_bool($value) ? ! $value : null);
}
/**
* Check the versioning flag inverse.
*
* @return bool
*/
public function isVersionless(): bool
{
return ! $this->isVersioning();
}
/**
* Get the package version in parts.
*
* @return array
*/
public function getVersionParts(?int $padding = null): array
{
if (! is_array($this->versionPartsCache)) {
$this->versionPartsCache = [];
}
$padding = is_int($padding) && $padding === 0 ? null : $padding;
$key = 'padding-'.(is_null($padding) ? 'null' : (string) $padding);
if (! array_key_exists($key, $this->versionPartsCache)) {
$parts = explode('.', $this->getVersion());
$this->versionPartsCache[$key] = array_map(function ($part) use ($padding) {
$part = (string) $part;
if (is_null($padding)) {
return $part;
}
$length = strlen(is_string($part) ? $part : (string) $part);
return $length == $padding ? $part : str_repeat('0', $padding - $length).$part;
}, $parts);
}
return $this->versionPartsCache[$key];
}
/**
* Get the package version for a prefix.
*
* @return string
*/
public function getVersionForPrefix(int $padding = 2, string $glue = '-'): string
{
return 'VERSION-'.implode($glue, $this->getVersionParts($padding));
}
/**
* Set the configured header prefix.
*
* @return EncryptionHelper
*/
public function setHeaderPrefix(?string $value = null): self
{
throw_if(is_string($value) && strlen(trim($value)) == 0, RuntimeException::class,
'Cannot use empty string as header prefix.');
$this->headerPrefixCache = $value;
return $this;
}
/**
* Get the configured header prefix.
*
* @return string
*/
public function getHeaderPrefix(): string
{
if (is_null($this->headerPrefixCache)) {
$characters = $this->getControlCharacters();
$this->headerPrefixCache = $characters['header']['start']['string'].
$characters['prefix']['start']['string'].
$this->getPrefix().
$characters['prefix']['stop']['string'];
}
return $this->headerPrefixCache;
}
/**
* Build a header string, optionally with an object.
*
* @return string
*/
public function buildHeader($object = null): string
{
$characters = $this->getControlCharacters();
return $characters['header']['start']['string'].
$characters['prefix']['start']['string'].
$this->getPrefix().
$characters['prefix']['stop']['string'].
$characters['field']['start']['string'].
'version'.
$characters['field']['delimiter']['string'].
self::getVersionForPrefix().
$characters['field']['stop']['string'].
(is_null($object) ? '' :
$characters['field']['start']['string'].
'type'.
$characters['field']['delimiter']['string'].
gettype($object).'['.(is_object($object) ? get_class($object) : 'native').']'.
$characters['field']['stop']['string']
).
$characters['header']['stop']['string'];
}
/**
* Set the configured prefix.
*
* @return EncryptionHelper
*/
public function setPrefix(?string $value = null): self
{
throw_if(is_string($value) && strlen(trim($value)) == 0, RuntimeException::class,
'Cannot use empty string as prefix.');
$this->prefixCache = is_string($value) && $this->isVersioning() ?
str_replace('%VERSION%', $this->getVersionForPrefix(), $value) :
$value;
return $this;
}
/**
* Get the configured prefix.
*
* @return string
*/
public function getPrefix(): string
{
if (is_null($this->prefixCache)) {
$prefix = Config::get('database-encryption.prefix', null);
$prefix = ! empty($prefix) && is_string($prefix) ? $prefix : self::getPrefixDefault();
$this->prefixCache = $this->isVersioning() ?
str_replace('%VERSION%', $this->getVersionForPrefix(), $prefix) :
$prefix;
}
return $this->prefixCache;
}
/**
* Get the configured control characters.
*
* @return array
*/
public function getControlCharacters(?string $type = null): array
{
$defaults = self::getControlCharactersDefault();
$characters = self::getControlCharactersDefault();
if (! is_null($type)) {
throw_if(! array_key_exists($type, $characters),
'Control characters do not exist for $type: "'.(empty($type) ? '(empty)' : $type).'".');
return $characters[$type];
}
return $characters;
}
/**
* Get the singleton of this class.
*
* @return EncryptionHelper
*/
public static function getInstance(): self
{
return EncryptionFacade::getInstance();
}
}