|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of the salting capabilities for digestion algorithms. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageDigestion; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface as SaltingCapabilitiesSpecification; |
|
10
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait SaltingCapabilitiesTrait - Reusable implementation of `SaltingCapabilitiesInterface`. |
|
14
|
|
|
* |
|
15
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface The abstract specification. |
|
16
|
|
|
* |
|
17
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
|
18
|
|
|
* |
|
19
|
|
|
* @property string $salt The salt string property storage. |
|
20
|
|
|
* @property int $saltingMode The salting mode property storage. |
|
21
|
|
|
* |
|
22
|
|
|
* @mixin SaltingCapabilitiesSpecification |
|
23
|
|
|
*/ |
|
24
|
|
|
trait SaltingCapabilitiesTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* List of salting modes that add the salt at the front side of the input data. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array Salting mode codes. |
|
30
|
|
|
*/ |
|
31
|
|
|
private $inFrontCases = [ |
|
32
|
|
|
self::SALTING_MODE_PREPEND, |
|
33
|
|
|
self::SALTING_MODE_REVERSE_PREPEND, |
|
34
|
|
|
self::SALTING_MODE_DUPLICATE_PREFIX |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List of salting modes that add the salt at the back side of the input data. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array Salting mode codes. |
|
41
|
|
|
*/ |
|
42
|
|
|
private $inBackCases = [ |
|
43
|
|
|
self::SALTING_MODE_APPEND, |
|
44
|
|
|
self::SALTING_MODE_REVERSE_APPEND, |
|
45
|
|
|
self::SALTING_MODE_DUPLICATE_SUFFIX |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* List of salting modes that use complex salt and input data manipulation procedures. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array Salting mode codes. |
|
52
|
|
|
*/ |
|
53
|
|
|
private $inSpecialCases = [ |
|
54
|
|
|
self::SALTING_MODE_INFIX_INPUT, |
|
55
|
|
|
self::SALTING_MODE_INFIX_SALT, |
|
56
|
|
|
self::SALTING_MODE_PALINDROME_MIRRORING |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Internal method for grouping salting modes that add to the front of the input data. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $data The input data for hashing. |
|
63
|
|
|
* |
|
64
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
|
65
|
|
|
*/ |
|
66
|
44 |
|
private function saltAtFront(&$data) |
|
67
|
|
|
{ |
|
68
|
44 |
|
switch ($this->saltingMode) { |
|
69
|
44 |
|
case self::SALTING_MODE_PREPEND: // SALTpassword |
|
70
|
44 |
|
$data = $this->salt . $data; |
|
71
|
44 |
|
break; |
|
72
|
22 |
|
case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword |
|
73
|
22 |
|
$data = StringBuilder::stringReverse($this->salt) . $data; |
|
74
|
22 |
|
break; |
|
75
|
22 |
|
case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword |
|
76
|
22 |
|
$data = $this->salt . StringBuilder::stringReverse($this->salt) . $data; |
|
77
|
22 |
|
break; |
|
78
|
|
|
default: |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
44 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Internal method for grouping salting modes that add to the back of the input data. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $data The input data for hashing. |
|
87
|
|
|
* |
|
88
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
|
89
|
|
|
*/ |
|
90
|
154 |
|
private function saltAtBack(&$data) |
|
91
|
|
|
{ |
|
92
|
154 |
|
switch ($this->saltingMode) { |
|
93
|
154 |
|
case self::SALTING_MODE_APPEND: // passwordSALT |
|
94
|
154 |
|
$data .= $this->salt; |
|
95
|
154 |
|
break; |
|
96
|
22 |
|
case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS |
|
97
|
22 |
|
$data .= StringBuilder::stringReverse($this->salt); |
|
98
|
22 |
|
break; |
|
99
|
22 |
|
case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS |
|
100
|
22 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($this->salt); |
|
101
|
22 |
|
break; |
|
102
|
|
|
default: |
|
103
|
|
|
break; |
|
104
|
|
|
} |
|
105
|
154 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Internal method for grouping salting modes that use complex manipulations of the salt and input data. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $data The input data for hashing. |
|
111
|
|
|
* |
|
112
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
|
113
|
|
|
*/ |
|
114
|
22 |
|
private function saltAtSpecial(&$data) |
|
115
|
|
|
{ |
|
116
|
22 |
|
switch ($this->saltingMode) { |
|
117
|
22 |
|
case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS |
|
118
|
22 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($this->salt); |
|
119
|
22 |
|
break; |
|
120
|
22 |
|
case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap |
|
121
|
22 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($data); |
|
122
|
22 |
|
break; |
|
123
|
22 |
|
case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS |
|
124
|
22 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($data); |
|
125
|
22 |
|
$data .= StringBuilder::stringReverse($this->salt); |
|
126
|
22 |
|
break; |
|
127
|
|
|
default: |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
22 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Internal method for adding the salt string to the input data via the chosen salting mode. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $data The input data for hashing. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string The input data with proper salting. |
|
138
|
|
|
*/ |
|
139
|
176 |
|
protected function addSaltString($data) |
|
140
|
|
|
{ |
|
141
|
176 |
|
if ($this->saltingMode === self::SALTING_MODE_NONE) { |
|
|
|
|
|
|
142
|
44 |
|
return $data; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
176 |
|
if (in_array($this->saltingMode, $this->inBackCases)) { |
|
146
|
154 |
|
$this->saltAtBack($data); |
|
147
|
44 |
|
} elseif (in_array($this->saltingMode, $this->inFrontCases)) { |
|
148
|
44 |
|
$this->saltAtFront($data); |
|
149
|
22 |
|
} elseif (in_array($this->saltingMode, $this->inSpecialCases)) { |
|
150
|
22 |
|
$this->saltAtSpecial($data); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
176 |
|
return $data; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Setter for the salt string property. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $salt The salt string. |
|
160
|
|
|
* |
|
161
|
|
|
* @return $this The hash algorithm object. |
|
162
|
|
|
* @throw \Exception Validation errors. |
|
163
|
|
|
*/ |
|
164
|
88 |
|
public function setSalt($salt) |
|
165
|
|
|
{ |
|
166
|
88 |
|
if (!is_string($salt)) { |
|
167
|
22 |
|
throw new \InvalidArgumentException('Salt must be of type string.'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
66 |
|
$this->salt = $salt; |
|
171
|
|
|
|
|
172
|
66 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Getter for the salt string property. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string The salt string. |
|
179
|
|
|
*/ |
|
180
|
44 |
|
public function getSalt() |
|
181
|
|
|
{ |
|
182
|
44 |
|
return $this->salt; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Setter for the salting mode code property. |
|
187
|
|
|
* |
|
188
|
|
|
* @param int $saltingMode The salting mode code. |
|
189
|
|
|
* |
|
190
|
|
|
* @return $this The hash algorithm object. |
|
191
|
|
|
* @throw \Exception Validation errors. |
|
192
|
|
|
*/ |
|
193
|
88 |
|
public function setSaltingMode($saltingMode) |
|
194
|
|
|
{ |
|
195
|
88 |
|
$saltingMode = filter_var( |
|
196
|
88 |
|
$saltingMode, |
|
197
|
88 |
|
FILTER_VALIDATE_INT, |
|
198
|
|
|
[ |
|
199
|
|
|
"options" => [ |
|
200
|
88 |
|
"min_range" => self::SALTING_MODE_NONE, // -1 |
|
201
|
88 |
|
"max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
|
202
|
|
|
], |
|
203
|
|
|
] |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
88 |
|
if ($saltingMode === false) { |
|
207
|
22 |
|
throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
66 |
|
$this->saltingMode = $saltingMode; |
|
211
|
|
|
|
|
212
|
66 |
|
return $this; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Getter for the salt mode code property. |
|
217
|
|
|
* |
|
218
|
|
|
* @return int The salt mode code. |
|
219
|
|
|
*/ |
|
220
|
44 |
|
public function getSaltingMode() |
|
221
|
|
|
{ |
|
222
|
44 |
|
return $this->saltingMode; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|