1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Utility class for cryptography key and token generation. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Utilities; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\Containers\AbstractRandomnessInjectable as RandomnessContainer; |
10
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\EncryptionKeyGenerationInterface as EncryptionKeyGeneration; |
11
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\HashingKeyGenerationInterface as HashingKeyGeneration; |
12
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\TokenGenerationInterface as TokenStringGeneration; |
13
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TokenGenerator - Utility class for cryptography token generation. |
17
|
|
|
* |
18
|
|
|
* @package CryptoManana\Utilities |
19
|
|
|
*/ |
20
|
|
|
class TokenGenerator extends RandomnessContainer implements |
21
|
|
|
TokenStringGeneration, |
22
|
|
|
HashingKeyGeneration, |
23
|
|
|
EncryptionKeyGeneration |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Internal method for validation of positive output length. |
27
|
|
|
* |
28
|
|
|
* @param int $length The output length value for validation. |
29
|
|
|
* |
30
|
|
|
* @throws \Exception Validation errors. |
31
|
|
|
*/ |
32
|
|
|
protected function applyLengthValidation($length) |
33
|
|
|
{ |
34
|
|
|
$length = filter_var( |
35
|
|
|
$length, |
36
|
|
|
FILTER_VALIDATE_INT, |
37
|
|
|
[ |
38
|
|
|
"options" => [ |
39
|
|
|
"min_range" => 1, |
40
|
|
|
"max_range" => PHP_INT_MAX, |
41
|
|
|
], |
42
|
|
|
] |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if ($length === false) { |
46
|
|
|
throw new \LengthException( |
47
|
|
|
'The length of the desired output data must me at least 1 character long.' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generate a random token string in alphanumeric or hexadecimal format. |
54
|
|
|
* |
55
|
|
|
* Note: This method can generate HEX output if the `$useAlphaNumeric` parameter is set to `false`. |
56
|
|
|
* |
57
|
|
|
* @param int $length The desired output length (default => 40). |
58
|
|
|
* @param bool|int $useAlphaNumeric Flag for switching to alphanumerical (default => true). |
59
|
|
|
* |
60
|
|
|
* @return string Randomly generated alphanumeric/hexadecimal token string. |
61
|
|
|
* @throws \Exception Validation errors. |
62
|
|
|
*/ |
63
|
|
|
public function getTokenString($length = 40, $useAlphaNumeric = true) |
64
|
|
|
{ |
65
|
|
|
$this->applyLengthValidation($length); |
66
|
|
|
|
67
|
|
|
if ($useAlphaNumeric) { |
68
|
|
|
$token = $this->randomnessSource->getAlphaNumeric($length, true); |
|
|
|
|
69
|
|
|
} else { |
70
|
|
|
$token = $this->randomnessSource->getHex($length, true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return StringBuilder::stringReverse($token); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generate a random password string. |
78
|
|
|
* |
79
|
|
|
* Note: This method can use more special symbols on generation if the `$stronger` parameter is set to `true`. |
80
|
|
|
* |
81
|
|
|
* @param int $length The desired output length (default => 10). |
82
|
|
|
* @param bool|int $stronger Flag for using all printable ASCII characters (default => true). |
83
|
|
|
* |
84
|
|
|
* @return string Randomly generated password string. |
85
|
|
|
* @throws \Exception Validation errors. |
86
|
|
|
*/ |
87
|
|
|
public function getPasswordString($length = 10, $stronger = true) |
88
|
|
|
{ |
89
|
|
|
$this->applyLengthValidation($length); |
90
|
|
|
|
91
|
|
|
if ($stronger) { |
92
|
|
|
$password = $this->randomnessSource->getAscii($length); |
93
|
|
|
} else { |
94
|
|
|
$password = ''; |
95
|
|
|
|
96
|
|
|
for ($i = 1; $i <= $length; $i++) { |
97
|
|
|
$tmp = $this->randomnessSource->getInt(1, 4); |
98
|
|
|
|
99
|
|
|
switch ($tmp) { |
100
|
|
|
case 1: |
101
|
|
|
$password .= $this->randomnessSource->getDigit(true); |
102
|
|
|
break; |
103
|
|
|
case 2: |
104
|
|
|
$password .= $this->randomnessSource->getLetter(false); |
105
|
|
|
break; |
106
|
|
|
case 3: |
107
|
|
|
$password .= StringBuilder::stringToUpper($this->randomnessSource->getLetter(false)); |
108
|
|
|
break; |
109
|
|
|
default: |
110
|
|
|
$password .= $this->randomnessSource->getString(1, ['!', '@', '#', '$', '%', '^']); |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $password; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Generate a random HMAC key for hashing purposes. |
121
|
|
|
* |
122
|
|
|
* Note: The output string can be in raw bytes of the `$printable` parameter is set to `true`. |
123
|
|
|
* |
124
|
|
|
* @param int $length The desired output length (default => 128). |
125
|
|
|
* @param bool|int $printable Flag for using only printable characters instead of bytes (default => true). |
126
|
|
|
* |
127
|
|
|
* @return string Randomly generated HMAC key. |
128
|
|
|
* @throws \Exception Validation errors. |
129
|
|
|
*/ |
130
|
|
|
public function getHashingKey($length = 128, $printable = true) |
131
|
|
|
{ |
132
|
|
|
$this->applyLengthValidation($length); |
133
|
|
|
|
134
|
|
|
return ($printable) ? $this->randomnessSource->getAscii($length) : $this->randomnessSource->getBytes($length); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate a random salt string for hashing purposes. |
139
|
|
|
* |
140
|
|
|
* Note: The output string can be in raw bytes of the `$printable` parameter is set to `true`. |
141
|
|
|
* |
142
|
|
|
* @param int $length The desired output length (default => 128). |
143
|
|
|
* @param bool|int $printable Flag for using only printable characters instead of bytes (default => true). |
144
|
|
|
* |
145
|
|
|
* @return string Randomly generated hashing salt. |
146
|
|
|
* @throws \Exception Validation errors. |
147
|
|
|
*/ |
148
|
|
|
public function getHashingSalt($length = 128, $printable = true) |
149
|
|
|
{ |
150
|
|
|
$this->applyLengthValidation($length); |
151
|
|
|
|
152
|
|
|
return ($printable) ? $this->randomnessSource->getAscii($length) : $this->randomnessSource->getBytes($length); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Generate a random encryption key for symmetrical cyphers. |
157
|
|
|
* |
158
|
|
|
* Note: The output string can be in raw bytes of the `$printable` parameter is set to `true`. |
159
|
|
|
* |
160
|
|
|
* @param int $length The desired output length (default => 128). |
161
|
|
|
* @param bool|int $printable Flag for using only printable characters instead of bytes (default => true). |
162
|
|
|
* |
163
|
|
|
* @return string Randomly generated encryption key. |
164
|
|
|
* @throws \Exception Validation errors. |
165
|
|
|
*/ |
166
|
|
|
public function getEncryptionKey($length = 128, $printable = true) |
167
|
|
|
{ |
168
|
|
|
$this->applyLengthValidation($length); |
169
|
|
|
|
170
|
|
|
return ($printable) ? $this->randomnessSource->getAscii($length) : $this->randomnessSource->getBytes($length); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Generate a random initialization vector (IV) for encryption purposes. |
175
|
|
|
* |
176
|
|
|
* Note: The output string can be in raw bytes of the `$printable` parameter is set to `true`. |
177
|
|
|
* |
178
|
|
|
* @param int $length The desired output length (default => 128). |
179
|
|
|
* @param bool|int $printable Flag for using only printable characters instead of bytes (default => true). |
180
|
|
|
* |
181
|
|
|
* @return string Randomly generated encryption initialization vector. |
182
|
|
|
* @throws \Exception Validation errors. |
183
|
|
|
*/ |
184
|
|
|
public function getEncryptionInitializationVector($length = 128, $printable = true) |
185
|
|
|
{ |
186
|
|
|
$this->applyLengthValidation($length); |
187
|
|
|
|
188
|
|
|
return ($printable) ? $this->randomnessSource->getAscii($length) : $this->randomnessSource->getBytes($length); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.