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\Handlers; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Contracts\Security\EncrypterInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Classe de base pour les gestionnaires de chiffrement |
18
|
|
|
*/ |
19
|
|
|
abstract class BaseHandler implements EncrypterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Clé de démarrage |
23
|
|
|
*/ |
24
|
|
|
protected string $key = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructeur |
28
|
|
|
*/ |
29
|
|
|
public function __construct(?object $config = null) |
30
|
|
|
{ |
31
|
|
|
$config ??= (object) config('encryption'); |
32
|
|
|
|
33
|
|
|
// rendre les paramètres facilement accessibles |
34
|
|
|
foreach (get_object_vars($config) as $key => $value) { |
35
|
|
|
if (property_exists($this, $key)) { |
36
|
|
|
$this->{$key} = $value; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function getKey(): string |
45
|
|
|
{ |
46
|
|
|
return $this->key; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Byte-safe substr() |
51
|
|
|
*/ |
52
|
|
|
protected static function substr(string $str, int $start, ?int $length = null): string |
53
|
|
|
{ |
54
|
|
|
return mb_substr($str, $start, $length, '8bit'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fourni un accès en lecture seule à certaines de nos propriétés |
59
|
|
|
* |
60
|
|
|
* @param string $key Nom de la propriete |
61
|
|
|
* |
62
|
|
|
* @return array|bool|int|string|null |
63
|
|
|
*/ |
64
|
|
|
public function __get($key) |
65
|
|
|
{ |
66
|
|
|
if ($this->__isset($key)) { |
67
|
|
|
return $this->{$key}; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Assure la vérification de certaines de nos propriétés |
75
|
|
|
* |
76
|
|
|
* @param string $key Nom de la propriete |
77
|
|
|
*/ |
78
|
|
|
public function __isset($key): bool |
79
|
|
|
{ |
80
|
|
|
return property_exists($this, $key); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|