Total Complexity | 16 |
Total Lines | 113 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | protected function addSaltString($data) |
||
33 | { |
||
34 | switch ($this->getSaltingMode()) { |
||
35 | case self::SALTING_MODE_APPEND: // passwordSALT |
||
1 ignored issue
–
show
|
|||
36 | $data .= $this->salt; |
||
37 | break; |
||
38 | case self::SALTING_MODE_PREPEND: // SALTpassword |
||
1 ignored issue
–
show
|
|||
39 | $data = $this->salt . $data; |
||
40 | break; |
||
41 | case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS |
||
1 ignored issue
–
show
|
|||
42 | $data = $this->salt . $data . strrev($this->salt); |
||
43 | break; |
||
44 | case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap |
||
1 ignored issue
–
show
|
|||
45 | $data = $data . $this->salt . strrev($data); |
||
46 | break; |
||
47 | case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS |
||
1 ignored issue
–
show
|
|||
48 | $data .= strrev($this->salt); |
||
49 | break; |
||
50 | case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword |
||
1 ignored issue
–
show
|
|||
51 | $data = strrev($this->salt) . $data; |
||
52 | break; |
||
53 | case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS |
||
1 ignored issue
–
show
|
|||
54 | $data = $data . $this->salt . strrev($this->salt); |
||
55 | break; |
||
56 | case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword |
||
1 ignored issue
–
show
|
|||
57 | $data = $this->salt . strrev($this->salt) . $data; |
||
58 | break; |
||
59 | case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS |
||
1 ignored issue
–
show
|
|||
60 | $data = $this->salt . $data . strrev($data) . strrev($this->salt); |
||
61 | break; |
||
62 | default: // case self::SALTING_MODE_NONE: |
||
63 | break; |
||
64 | } |
||
65 | |||
66 | return $data; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Setter for the salt string property. |
||
71 | * |
||
72 | * @param string $salt The salt string. |
||
73 | * |
||
74 | * @return $this The hash algorithm object. |
||
75 | * @throw \Exception Validation errors. |
||
76 | */ |
||
77 | public function setSalt($salt) |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Getter for the salt string property. |
||
90 | * |
||
91 | * @return string The salt string. |
||
92 | */ |
||
93 | public function getSalt() |
||
94 | { |
||
95 | return $this->salt; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Setter for the salting mode code property. |
||
100 | * |
||
101 | * @param int $saltingMode The salting mode code. |
||
102 | * |
||
103 | * @return $this The hash algorithm object. |
||
104 | * @throw \Exception Validation errors. |
||
105 | */ |
||
106 | public function setSaltingMode($saltingMode) |
||
107 | { |
||
108 | $saltingMode = filter_var( |
||
109 | $saltingMode, |
||
110 | FILTER_VALIDATE_INT, |
||
111 | [ |
||
112 | "options" => [ |
||
113 | "min_range" => self::SALTING_MODE_NONE, // -1 |
||
1 ignored issue
–
show
|
|||
114 | "max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8 |
||
1 ignored issue
–
show
|
|||
115 | ], |
||
116 | ] |
||
117 | ); |
||
118 | |||
119 | if ($saltingMode === false) { |
||
120 | throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.'); |
||
121 | } |
||
122 | |||
123 | $this->saltingMode = $saltingMode; |
||
124 | |||
125 | return $this; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Getter for the salt mode code property. |
||
130 | * |
||
131 | * @return int The salt mode code. |
||
132 | */ |
||
133 | public function getSaltingMode() |
||
136 | } |
||
137 | } |
||
138 |