1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/2fa-library project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\TwoFA\Support; |
13
|
|
|
|
14
|
|
|
use Da\TwoFA\Contracts\TotpEncoderInterface; |
15
|
|
|
use Da\TwoFA\Exception\InvalidSecretKeyException; |
16
|
|
|
use Da\TwoFA\Traits\SecretValidationTrait; |
17
|
|
|
use Da\TwoFA\Validator\SecretKeyValidator; |
18
|
|
|
use Exception; |
19
|
|
|
use ParagonIE\ConstantTime\Base32; |
20
|
|
|
|
21
|
|
|
class Encoder implements TotpEncoderInterface |
22
|
|
|
{ |
23
|
|
|
use SecretValidationTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Encoder constructor. |
27
|
|
|
* |
28
|
|
|
* @param SecretKeyValidator|null $secretKeyValidator |
29
|
|
|
*/ |
30
|
|
|
public function __construct(SecretKeyValidator $secretKeyValidator = null) |
31
|
|
|
{ |
32
|
|
|
$this->secretKeyValidator = $secretKeyValidator ?: new SecretKeyValidator(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* @throws InvalidSecretKeyException |
38
|
|
|
*/ |
39
|
|
|
public function generateBase32RandomKey(int $length = 16, string $prefix = ''): string |
40
|
|
|
{ |
41
|
|
|
$secret = $prefix ? $this->toBase32($prefix) : ''; |
42
|
|
|
$secret = $this->strPadBase32($secret, $length); |
43
|
|
|
|
44
|
|
|
$this->validateSecret($secret); |
45
|
|
|
|
46
|
|
|
return $secret; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function toBase32(string $value): string |
53
|
|
|
{ |
54
|
|
|
$encoded = Base32::encodeUpper($value); |
55
|
|
|
|
56
|
|
|
return str_replace('=', '', $encoded); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
* @throws InvalidSecretKeyException |
62
|
|
|
*/ |
63
|
|
|
public function fromBase32(string $value): string |
64
|
|
|
{ |
65
|
|
|
$value = strtoupper($value); |
66
|
|
|
|
67
|
|
|
$this->validateSecret($value); |
68
|
|
|
|
69
|
|
|
return Base32::decodeUpper($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get a random number. |
74
|
|
|
* |
75
|
|
|
* @param $from |
76
|
|
|
* @param $to |
77
|
|
|
* |
78
|
|
|
* @throws Exception |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
protected function getRandomNumber(int $from = 0, int $to = 31): int |
82
|
|
|
{ |
83
|
|
|
return random_int($from, $to); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Pad string with random base 32 chars. |
88
|
|
|
* |
89
|
|
|
* @param $string |
90
|
|
|
* @param $length |
91
|
|
|
* |
92
|
|
|
* @throws Exception |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
private function strPadBase32(string $string, int $length): string |
96
|
|
|
{ |
97
|
|
|
for ($i = 0; $i < $length; $i++) { |
98
|
|
|
$string .= substr('234567QWERTYUIOPASDFGHJKLZXCVBNM', $this->getRandomNumber(), 1); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $string; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|