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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait SaltingCapabilitiesTrait - Reusable implementation of `SaltingCapabilitiesInterface`. |
13
|
|
|
* |
14
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface The abstract specification. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
17
|
|
|
* |
18
|
|
|
* @property string $salt The salt string property storage. |
19
|
|
|
* @property int $saltingMode The salting mode property storage. |
20
|
|
|
* |
21
|
|
|
* @mixin SaltingCapabilitiesSpecification |
22
|
|
|
*/ |
23
|
|
|
trait SaltingCapabilitiesTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Internal method for adding the salt string to the input data via the chosen salting mode. |
27
|
|
|
* |
28
|
|
|
* @param string $data The input data for hashing. |
29
|
|
|
* |
30
|
|
|
* @return string The input data with proper salting. |
31
|
|
|
*/ |
32
|
|
|
abstract protected function addSaltString($data); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Setter for the salt string property. |
36
|
|
|
* |
37
|
|
|
* @param string $salt The salt string. |
38
|
|
|
* |
39
|
|
|
* @return $this The hash algorithm object. |
40
|
|
|
* @throw \Exception Validation errors. |
41
|
|
|
*/ |
42
|
88 |
|
public function setSalt($salt) |
43
|
|
|
{ |
44
|
88 |
|
if (!is_string($salt)) { |
45
|
22 |
|
throw new \InvalidArgumentException('Salt must be of type string.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
66 |
|
$this->salt = $salt; |
49
|
|
|
|
50
|
66 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Getter for the salt string property. |
55
|
|
|
* |
56
|
|
|
* @return string The salt string. |
57
|
|
|
*/ |
58
|
44 |
|
public function getSalt() |
59
|
|
|
{ |
60
|
44 |
|
return $this->salt; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Setter for the salting mode code property. |
65
|
|
|
* |
66
|
|
|
* @param int $saltingMode The salting mode code. |
67
|
|
|
* |
68
|
|
|
* @return $this The hash algorithm object. |
69
|
|
|
* @throw \Exception Validation errors. |
70
|
|
|
*/ |
71
|
88 |
|
public function setSaltingMode($saltingMode) |
72
|
|
|
{ |
73
|
88 |
|
$saltingMode = filter_var( |
74
|
88 |
|
$saltingMode, |
75
|
88 |
|
FILTER_VALIDATE_INT, |
76
|
|
|
[ |
77
|
|
|
"options" => [ |
78
|
88 |
|
"min_range" => self::SALTING_MODE_NONE, // -1 |
79
|
88 |
|
"max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
80
|
|
|
], |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
|
84
|
88 |
|
if ($saltingMode === false) { |
85
|
22 |
|
throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
66 |
|
$this->saltingMode = $saltingMode; |
89
|
|
|
|
90
|
66 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Getter for the salt mode code property. |
95
|
|
|
* |
96
|
|
|
* @return int The salt mode code. |
97
|
|
|
*/ |
98
|
44 |
|
public function getSaltingMode() |
99
|
|
|
{ |
100
|
44 |
|
return $this->saltingMode; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|