1 | <?php |
||
8 | trait Base32 |
||
9 | { |
||
10 | /** |
||
11 | * Enforce Google Authenticator compatibility. |
||
12 | */ |
||
13 | protected $enforceGoogleAuthenticatorCompatibility = true; |
||
14 | |||
15 | /** |
||
16 | * Generate a digit secret key in base32 format. |
||
17 | * |
||
18 | * @param int $length |
||
19 | * |
||
20 | * @return string |
||
21 | */ |
||
22 | public function generateBase32RandomKey($length = 16, $prefix = '') |
||
32 | |||
33 | /** |
||
34 | * Decodes a base32 string into a binary string. |
||
35 | * |
||
36 | * @param string $b32 |
||
37 | * |
||
38 | * @throws InvalidCharactersException |
||
39 | * |
||
40 | * @return int |
||
41 | */ |
||
42 | public function base32Decode($b32) |
||
50 | |||
51 | /** |
||
52 | * Pad string with random base 32 chars. |
||
53 | * |
||
54 | * @param $string |
||
55 | * @param $length |
||
56 | * @return string |
||
57 | */ |
||
58 | private function strPadBase32($string, $length) |
||
66 | |||
67 | /** |
||
68 | * Encode a string to Base32. |
||
69 | * |
||
70 | * @param $string |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function toBase32($string) |
||
80 | |||
81 | /** |
||
82 | * Get a random number. |
||
83 | * |
||
84 | * @param $from |
||
85 | * @param $to |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | protected function getRandomNumber($from = 0, $to = 31) |
||
93 | |||
94 | /** |
||
95 | * Validate the secret. |
||
96 | * |
||
97 | * @param $b32 |
||
98 | */ |
||
99 | protected function validateSecret($b32) |
||
105 | |||
106 | /** |
||
107 | * Check if the secret key is compatible with Google Authenticator. |
||
108 | * |
||
109 | * @param $b32 |
||
110 | * |
||
111 | * @throws IncompatibleWithGoogleAuthenticatorException |
||
112 | */ |
||
113 | protected function checkGoogleAuthenticatorCompatibility($b32) |
||
119 | |||
120 | /** |
||
121 | * Check if all secret key characters are valid. |
||
122 | * |
||
123 | * @param $b32 |
||
124 | * |
||
125 | * @throws InvalidCharactersException |
||
126 | */ |
||
127 | protected function checkForValidCharacters($b32) |
||
133 | } |
||
134 |