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
|
|
|
protected static $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
|
|
|
protected static $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
|
|
|
protected static $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
|
|
|
* @note The parameter is passed via reference from the main logical method for performance reasons. |
65
|
|
|
*/ |
66
|
154 |
|
protected function saltAtFront(&$data) |
67
|
|
|
{ |
68
|
154 |
|
switch ($this->saltingMode) { |
69
|
154 |
|
case self::SALTING_MODE_PREPEND: // SALTpassword |
70
|
124 |
|
$data = $this->salt . $data; |
71
|
124 |
|
break; |
72
|
92 |
|
case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword |
73
|
92 |
|
$data = StringBuilder::stringReverse($this->salt) . $data; |
74
|
92 |
|
break; |
75
|
62 |
|
case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword |
76
|
62 |
|
$data = $this->salt . StringBuilder::stringReverse($this->salt) . $data; |
77
|
62 |
|
break; |
78
|
|
|
} |
79
|
|
|
} |
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
|
|
|
* @note The parameter is passed via reference from the main logical method for performance reasons. |
87
|
|
|
*/ |
88
|
562 |
|
protected function saltAtBack(&$data) |
89
|
|
|
{ |
90
|
562 |
|
switch ($this->saltingMode) { |
91
|
562 |
|
case self::SALTING_MODE_APPEND: // passwordSALT |
92
|
562 |
|
$data .= $this->salt; |
93
|
562 |
|
break; |
94
|
62 |
|
case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS |
95
|
62 |
|
$data .= StringBuilder::stringReverse($this->salt); |
96
|
62 |
|
break; |
97
|
62 |
|
case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS |
98
|
62 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($this->salt); |
99
|
62 |
|
break; |
100
|
|
|
} |
101
|
|
|
} |
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
|
|
|
* @note The parameter is passed via reference from the main logical method for performance reasons. |
109
|
|
|
*/ |
110
|
92 |
|
protected function saltAtSpecial(&$data) |
111
|
|
|
{ |
112
|
92 |
|
switch ($this->saltingMode) { |
113
|
92 |
|
case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS |
114
|
62 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($this->salt); |
115
|
62 |
|
break; |
116
|
92 |
|
case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap |
117
|
92 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($data); |
118
|
92 |
|
break; |
119
|
62 |
|
case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS |
120
|
62 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($data); |
121
|
62 |
|
$data .= StringBuilder::stringReverse($this->salt); |
122
|
62 |
|
break; |
123
|
|
|
} |
124
|
|
|
} |
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
|
625 |
|
protected function addSaltString($data) |
134
|
|
|
{ |
135
|
625 |
|
if ($this->saltingMode === self::SALTING_MODE_NONE) { |
136
|
93 |
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
624 |
|
if (in_array($this->saltingMode, self::$inBackCases)) { |
140
|
562 |
|
$this->saltAtBack($data); |
141
|
154 |
|
} elseif (in_array($this->saltingMode, self::$inFrontCases)) { |
142
|
154 |
|
$this->saltAtFront($data); |
143
|
92 |
|
} elseif (in_array($this->saltingMode, self::$inSpecialCases)) { |
144
|
92 |
|
$this->saltAtSpecial($data); |
145
|
|
|
} |
146
|
|
|
|
147
|
624 |
|
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
|
|
|
* @throws \Exception Validation errors. |
157
|
|
|
*/ |
158
|
197 |
|
public function setSalt($salt) |
159
|
|
|
{ |
160
|
197 |
|
if (!is_string($salt)) { |
161
|
62 |
|
throw new \InvalidArgumentException('Salt must be of type string.'); |
162
|
|
|
} |
163
|
|
|
|
164
|
135 |
|
$this->salt = $salt; |
165
|
|
|
|
166
|
135 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Getter for the salt string property. |
171
|
|
|
* |
172
|
|
|
* @return string The salt string. |
173
|
|
|
*/ |
174
|
68 |
|
public function getSalt() |
175
|
|
|
{ |
176
|
68 |
|
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
|
|
|
* @throws \Exception Validation errors. |
186
|
|
|
*/ |
187
|
217 |
|
public function setSaltingMode($saltingMode) |
188
|
|
|
{ |
189
|
217 |
|
$saltingMode = filter_var( |
190
|
217 |
|
$saltingMode, |
191
|
217 |
|
FILTER_VALIDATE_INT, |
192
|
217 |
|
[ |
193
|
217 |
|
"options" => [ |
194
|
217 |
|
"min_range" => self::SALTING_MODE_NONE, // -1 |
195
|
217 |
|
"max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
196
|
217 |
|
], |
197
|
217 |
|
] |
198
|
217 |
|
); |
199
|
|
|
|
200
|
217 |
|
if ($saltingMode === false) { |
201
|
62 |
|
throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
202
|
|
|
} |
203
|
|
|
|
204
|
155 |
|
$this->saltingMode = $saltingMode; |
205
|
|
|
|
206
|
155 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Getter for the salt mode code property. |
211
|
|
|
* |
212
|
|
|
* @return int The salt mode code. |
213
|
|
|
*/ |
214
|
62 |
|
public function getSaltingMode() |
215
|
|
|
{ |
216
|
62 |
|
return $this->saltingMode; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|