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
|
|
|
} |
79
|
44 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Internal method for grouping salting modes that add to the back of the input data. |
83
|
|
|
* |
84
|
|
|
* @param string $data The input data for hashing. |
85
|
|
|
* |
86
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
87
|
|
|
*/ |
88
|
154 |
|
private function saltAtBack(&$data) |
89
|
|
|
{ |
90
|
154 |
|
switch ($this->saltingMode) { |
91
|
154 |
|
case self::SALTING_MODE_APPEND: // passwordSALT |
92
|
154 |
|
$data .= $this->salt; |
93
|
154 |
|
break; |
94
|
22 |
|
case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS |
95
|
22 |
|
$data .= StringBuilder::stringReverse($this->salt); |
96
|
22 |
|
break; |
97
|
22 |
|
case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS |
98
|
22 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($this->salt); |
99
|
22 |
|
break; |
100
|
|
|
} |
101
|
154 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Internal method for grouping salting modes that use complex manipulations of the salt and input data. |
105
|
|
|
* |
106
|
|
|
* @param string $data The input data for hashing. |
107
|
|
|
* |
108
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
109
|
|
|
*/ |
110
|
22 |
|
private function saltAtSpecial(&$data) |
111
|
|
|
{ |
112
|
22 |
|
switch ($this->saltingMode) { |
113
|
22 |
|
case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS |
114
|
22 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($this->salt); |
115
|
22 |
|
break; |
116
|
22 |
|
case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap |
117
|
22 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($data); |
118
|
22 |
|
break; |
119
|
22 |
|
case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS |
120
|
22 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($data); |
121
|
22 |
|
$data .= StringBuilder::stringReverse($this->salt); |
122
|
22 |
|
break; |
123
|
|
|
} |
124
|
22 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Internal method for adding the salt string to the input data via the chosen salting mode. |
128
|
|
|
* |
129
|
|
|
* @param string $data The input data for hashing. |
130
|
|
|
* |
131
|
|
|
* @return string The input data with proper salting. |
132
|
|
|
*/ |
133
|
176 |
|
protected function addSaltString($data) |
134
|
|
|
{ |
135
|
176 |
|
if ($this->saltingMode === self::SALTING_MODE_NONE) { |
136
|
44 |
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
176 |
|
if (in_array($this->saltingMode, $this->inBackCases)) { |
140
|
154 |
|
$this->saltAtBack($data); |
141
|
44 |
|
} elseif (in_array($this->saltingMode, $this->inFrontCases)) { |
142
|
44 |
|
$this->saltAtFront($data); |
143
|
22 |
|
} elseif (in_array($this->saltingMode, $this->inSpecialCases)) { |
144
|
22 |
|
$this->saltAtSpecial($data); |
145
|
|
|
} |
146
|
|
|
|
147
|
176 |
|
return $data; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Setter for the salt string property. |
152
|
|
|
* |
153
|
|
|
* @param string $salt The salt string. |
154
|
|
|
* |
155
|
|
|
* @return $this The hash algorithm object. |
156
|
|
|
* @throw \Exception Validation errors. |
157
|
|
|
*/ |
158
|
88 |
|
public function setSalt($salt) |
159
|
|
|
{ |
160
|
88 |
|
if (!is_string($salt)) { |
161
|
22 |
|
throw new \InvalidArgumentException('Salt must be of type string.'); |
162
|
|
|
} |
163
|
|
|
|
164
|
66 |
|
$this->salt = $salt; |
165
|
|
|
|
166
|
66 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Getter for the salt string property. |
171
|
|
|
* |
172
|
|
|
* @return string The salt string. |
173
|
|
|
*/ |
174
|
44 |
|
public function getSalt() |
175
|
|
|
{ |
176
|
44 |
|
return $this->salt; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Setter for the salting mode code property. |
181
|
|
|
* |
182
|
|
|
* @param int $saltingMode The salting mode code. |
183
|
|
|
* |
184
|
|
|
* @return $this The hash algorithm object. |
185
|
|
|
* @throw \Exception Validation errors. |
186
|
|
|
*/ |
187
|
88 |
|
public function setSaltingMode($saltingMode) |
188
|
|
|
{ |
189
|
88 |
|
$saltingMode = filter_var( |
190
|
88 |
|
$saltingMode, |
191
|
88 |
|
FILTER_VALIDATE_INT, |
192
|
|
|
[ |
193
|
|
|
"options" => [ |
194
|
88 |
|
"min_range" => self::SALTING_MODE_NONE, // -1 |
195
|
88 |
|
"max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
196
|
|
|
], |
197
|
|
|
] |
198
|
|
|
); |
199
|
|
|
|
200
|
88 |
|
if ($saltingMode === false) { |
201
|
22 |
|
throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
202
|
|
|
} |
203
|
|
|
|
204
|
66 |
|
$this->saltingMode = $saltingMode; |
205
|
|
|
|
206
|
66 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Getter for the salt mode code property. |
211
|
|
|
* |
212
|
|
|
* @return int The salt mode code. |
213
|
|
|
*/ |
214
|
44 |
|
public function getSaltingMode() |
215
|
|
|
{ |
216
|
44 |
|
return $this->saltingMode; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|