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
|
|
|
* Internal method for adding the salt string to the input data via the chosen salting mode. |
28
|
|
|
* |
29
|
|
|
* @param string $data The input data for hashing. |
30
|
|
|
* |
31
|
|
|
* @return string The input data with proper salting. |
32
|
|
|
*/ |
33
|
176 |
|
protected function addSaltString($data) |
34
|
|
|
{ |
35
|
176 |
|
switch ($this->saltingMode) { |
36
|
176 |
|
case self::SALTING_MODE_APPEND: // passwordSALT |
37
|
154 |
|
$data .= $this->salt; |
38
|
154 |
|
break; |
39
|
66 |
|
case self::SALTING_MODE_PREPEND: // SALTpassword |
40
|
44 |
|
$data = $this->salt . $data; |
41
|
44 |
|
break; |
42
|
44 |
|
case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS |
43
|
22 |
|
$data = $this->salt . $data . StringBuilder::stringReverse($this->salt); |
44
|
22 |
|
break; |
45
|
44 |
|
case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap |
46
|
22 |
|
$data = $data . $this->salt . StringBuilder::stringReverse($data); |
47
|
22 |
|
break; |
48
|
44 |
|
case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS |
49
|
22 |
|
$data .= StringBuilder::stringReverse($this->salt); |
50
|
22 |
|
break; |
51
|
44 |
|
case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword |
52
|
22 |
|
return StringBuilder::stringReverse($this->salt) . $data; |
53
|
|
|
break; |
|
|
|
|
54
|
44 |
|
case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS |
55
|
22 |
|
return $data . $this->salt . StringBuilder::stringReverse($this->salt); |
56
|
|
|
break; |
57
|
44 |
|
case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword |
58
|
22 |
|
return $this->salt . StringBuilder::stringReverse($this->salt) . $data; |
59
|
|
|
break; |
60
|
|
|
default: // case self::SALTING_MODE_NONE: |
61
|
44 |
|
break; |
62
|
|
|
} |
63
|
|
|
|
64
|
176 |
|
return $data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Setter for the salt string property. |
69
|
|
|
* |
70
|
|
|
* @param string $salt The salt string. |
71
|
|
|
* |
72
|
|
|
* @return $this The hash algorithm object. |
73
|
|
|
* @throw \Exception Validation errors. |
74
|
|
|
*/ |
75
|
88 |
|
public function setSalt($salt) |
76
|
|
|
{ |
77
|
88 |
|
if (!is_string($salt)) { |
78
|
22 |
|
throw new \InvalidArgumentException('Salt must be of type string.'); |
79
|
|
|
} |
80
|
|
|
|
81
|
66 |
|
$this->salt = $salt; |
82
|
|
|
|
83
|
66 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Getter for the salt string property. |
88
|
|
|
* |
89
|
|
|
* @return string The salt string. |
90
|
|
|
*/ |
91
|
44 |
|
public function getSalt() |
92
|
|
|
{ |
93
|
44 |
|
return $this->salt; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Setter for the salting mode code property. |
98
|
|
|
* |
99
|
|
|
* @param int $saltingMode The salting mode code. |
100
|
|
|
* |
101
|
|
|
* @return $this The hash algorithm object. |
102
|
|
|
* @throw \Exception Validation errors. |
103
|
|
|
*/ |
104
|
88 |
|
public function setSaltingMode($saltingMode) |
105
|
|
|
{ |
106
|
88 |
|
$saltingMode = filter_var( |
107
|
88 |
|
$saltingMode, |
108
|
88 |
|
FILTER_VALIDATE_INT, |
109
|
|
|
[ |
110
|
|
|
"options" => [ |
111
|
88 |
|
"min_range" => self::SALTING_MODE_NONE, // -1 |
112
|
88 |
|
"max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
113
|
|
|
], |
114
|
|
|
] |
115
|
|
|
); |
116
|
|
|
|
117
|
88 |
|
if ($saltingMode === false) { |
118
|
22 |
|
throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
66 |
|
$this->saltingMode = $saltingMode; |
122
|
|
|
|
123
|
66 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Getter for the salt mode code property. |
128
|
|
|
* |
129
|
|
|
* @return int The salt mode code. |
130
|
|
|
*/ |
131
|
44 |
|
public function getSaltingMode() |
132
|
|
|
{ |
133
|
44 |
|
return $this->saltingMode; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.