@@ -64,7 +64,8 @@ discard block |
||
| 64 | 64 | { |
| 65 | 65 | $this->buildLookup(); |
| 66 | 66 | |
| 67 | - if ($initKey !== null) { |
|
| 67 | + if ($initKey !== null) |
|
| 68 | + { |
|
| 68 | 69 | $this->setInitKey($initKey); |
| 69 | 70 | } |
| 70 | 71 | } |
@@ -98,7 +99,8 @@ discard block |
||
| 98 | 99 | */ |
| 99 | 100 | public function setRange($range) |
| 100 | 101 | { |
| 101 | - if (!is_numeric($range)) { |
|
| 102 | + if (!is_numeric($range)) |
|
| 103 | + { |
|
| 102 | 104 | throw new \InvalidArgumentException('Invalid window range'); |
| 103 | 105 | } |
| 104 | 106 | $this->range = $range; |
@@ -114,7 +116,8 @@ discard block |
||
| 114 | 116 | */ |
| 115 | 117 | public function setInitKey($key) |
| 116 | 118 | { |
| 117 | - if (empty(preg_match('/^[' . implode('', array_keys($this->getLookup())) . ']+$/', $key))) { |
|
| 119 | + if (empty(preg_match('/^[' . implode('', array_keys($this->getLookup())) . ']+$/', $key))) |
|
| 120 | + { |
|
| 118 | 121 | throw new \InvalidArgumentException('Invalid base32 hash!'); |
| 119 | 122 | } |
| 120 | 123 | $this->initKey = $key; |
@@ -140,7 +143,8 @@ discard block |
||
| 140 | 143 | */ |
| 141 | 144 | public function setLookup($lookup) |
| 142 | 145 | { |
| 143 | - if (!is_array($lookup)) { |
|
| 146 | + if (!is_array($lookup)) |
|
| 147 | + { |
|
| 144 | 148 | throw new \InvalidArgumentException('Lookup value must be an array'); |
| 145 | 149 | } |
| 146 | 150 | $this->lookup = $lookup; |
@@ -176,7 +180,8 @@ discard block |
||
| 176 | 180 | */ |
| 177 | 181 | public function setRefresh($seconds) |
| 178 | 182 | { |
| 179 | - if (!is_numeric($seconds)) { |
|
| 183 | + if (!is_numeric($seconds)) |
|
| 184 | + { |
|
| 180 | 185 | throw new \InvalidArgumentException('Seconds must be numeric'); |
| 181 | 186 | } |
| 182 | 187 | $this->refreshSeconds = $seconds; |
@@ -217,7 +222,8 @@ discard block |
||
| 217 | 222 | */ |
| 218 | 223 | public function validateCode($code, $initKey = null, $timestamp = null, $range = null) |
| 219 | 224 | { |
| 220 | - if (strlen($code) !== $this->getCodeLength()) { |
|
| 225 | + if (strlen($code) !== $this->getCodeLength()) |
|
| 226 | + { |
|
| 221 | 227 | throw new \InvalidArgumentException('Incorrect code length'); |
| 222 | 228 | } |
| 223 | 229 | |
@@ -227,8 +233,10 @@ discard block |
||
| 227 | 233 | |
| 228 | 234 | $binary = $this->base32_decode($initKey); |
| 229 | 235 | |
| 230 | - for ($time = ($timestamp - $range); $time <= ($timestamp + $range); $time++) { |
|
| 231 | - if ($this->generateOneTime($binary, $time) == $code) { |
|
| 236 | + for ($time = ($timestamp - $range); $time <= ($timestamp + $range); $time++) |
|
| 237 | + { |
|
| 238 | + if ($this->generateOneTime($binary, $time) == $code) |
|
| 239 | + { |
|
| 232 | 240 | return true; |
| 233 | 241 | } |
| 234 | 242 | } |
@@ -269,7 +277,8 @@ discard block |
||
| 269 | 277 | $lookup = implode('', array_keys($this->getLookup())); |
| 270 | 278 | $code = ''; |
| 271 | 279 | |
| 272 | - for ($i = 0; $i < $length; $i++) { |
|
| 280 | + for ($i = 0; $i < $length; $i++) |
|
| 281 | + { |
|
| 273 | 282 | $code .= $lookup[mt_rand(0, strlen($lookup) - 1)]; |
| 274 | 283 | } |
| 275 | 284 | |
@@ -315,7 +324,8 @@ discard block |
||
| 315 | 324 | { |
| 316 | 325 | $lookup = $this->getLookup(); |
| 317 | 326 | |
| 318 | - if (empty(preg_match('/^[' . implode('', array_keys($lookup)) . ']+$/', $hash))) { |
|
| 327 | + if (empty(preg_match('/^[' . implode('', array_keys($lookup)) . ']+$/', $hash))) |
|
| 328 | + { |
|
| 319 | 329 | throw new \InvalidArgumentException('Invalid base32 hash!'); |
| 320 | 330 | } |
| 321 | 331 | |
@@ -324,12 +334,14 @@ discard block |
||
| 324 | 334 | $length = 0; |
| 325 | 335 | $binary = ''; |
| 326 | 336 | |
| 327 | - for ($i = 0; $i < strlen($hash); $i++) { |
|
| 337 | + for ($i = 0; $i < strlen($hash); $i++) |
|
| 338 | + { |
|
| 328 | 339 | $buffer = $buffer << 5; |
| 329 | 340 | $buffer += $lookup[$hash[$i]]; |
| 330 | 341 | $length += 5; |
| 331 | 342 | |
| 332 | - if ($length >= 8) { |
|
| 343 | + if ($length >= 8) |
|
| 344 | + { |
|
| 333 | 345 | $length -= 8; |
| 334 | 346 | $binary .= chr(($buffer & (0xFF << $length)) >> $length); |
| 335 | 347 | } |