|
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\Hashing; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Contracts\Security\HasherInterface; |
|
15
|
|
|
use BlitzPHP\Exceptions\HashingException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Gestionnaire de hashage de BlitzPHP |
|
19
|
|
|
*/ |
|
20
|
|
|
class Hasher implements HasherInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Le hasheur que nous utilisons |
|
24
|
|
|
*/ |
|
25
|
|
|
protected ?HasherInterface $hasher = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Le pilote utilisé |
|
29
|
|
|
*/ |
|
30
|
|
|
protected string $driver = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Pilotes aux classes de gestionnaires, par ordre de préférence |
|
34
|
|
|
*/ |
|
35
|
|
|
protected array $drivers = [ |
|
36
|
|
|
'bcrypt', |
|
37
|
|
|
'argon', |
|
38
|
|
|
'argon2id', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructeur |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(protected ?object $config = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$config ??= (object) config('hashing'); |
|
47
|
|
|
|
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->driver = $config->driver; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function info(string $hashedValue): array |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->driver()->info($hashedValue); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function make(string $value, array $options = []): string |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->driver()->make($value, $options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritDoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function check(string $value, string $hashedValue, array $options = []): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->driver()->check($value, $hashedValue, $options); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritDoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function needsRehash(string $hashedValue, array $options = []): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->driver()->needsRehash($hashedValue, $options); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Détermine si une chaîne donnée est déjà hachée. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isHashed(string $value): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->driver()->info($value)['algo'] !== null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Vérifie que la configuration est inférieure ou égale à ce qui est configuré. |
|
94
|
|
|
* |
|
95
|
|
|
* @internal |
|
96
|
|
|
*/ |
|
97
|
|
|
public function verifyConfiguration(array $value): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->driver()->verifyConfiguration($value); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Initialiser ou réinitialiser le hasheur |
|
104
|
|
|
* |
|
105
|
|
|
* @throws HashingException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function initialize(?object $config = null): HasherInterface |
|
108
|
|
|
{ |
|
109
|
|
|
if ($config) { |
|
110
|
|
|
$this->driver = $config->driver; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->driver === '') { |
|
114
|
|
|
throw HashingException::noDriverRequested(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (! in_array($this->driver, $this->drivers, true)) { |
|
118
|
|
|
throw HashingException::unKnownHandler($this->driver); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$handlerName = 'BlitzPHP\\Security\\Hashing\\Handlers\\' . ucfirst($this->driver) . 'Handler'; |
|
122
|
|
|
$params = (array) $config; |
|
123
|
|
|
$this->hasher = new $handlerName($params[$this->driver]); |
|
124
|
|
|
|
|
125
|
|
|
return $this->hasher; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function driver(): HasherInterface |
|
129
|
|
|
{ |
|
130
|
|
|
if (null === $this->hasher) { |
|
131
|
|
|
$this->hasher = $this->initialize($this->config); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->hasher; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|