@@ -27,7 +27,7 @@ class GoogleAuthenticator
27
27
* @return string
28
28
* @throws Exception
29
29
*/
30
- public function createSecret ($ secretLength = 16 )
30
+ public function createSecret (int $ secretLength = 16 ) : string
31
31
{
32
32
$ validChars = self ::base32LookupTable ();
33
33
@@ -40,11 +40,6 @@ public function createSecret($secretLength = 16)
40
40
$ rnd = false ;
41
41
if (function_exists ('random_bytes ' )) {
42
42
$ rnd = random_bytes ($ secretLength );
43
- } elseif (function_exists ('openssl_random_pseudo_bytes ' )) {
44
- $ rnd = openssl_random_pseudo_bytes ($ secretLength , $ cryptoStrong );
45
- if (!$ cryptoStrong ) {
46
- $ rnd = false ;
47
- }
48
43
}
49
44
50
45
if (!$ rnd ) {
@@ -68,7 +63,7 @@ public function createSecret($secretLength = 16)
68
63
* @return string
69
64
* @throws Exception
70
65
*/
71
- public function getCode ($ secret , $ timeSlice = null )
66
+ public function getCode (string $ secret , int $ timeSlice = null ) : string
72
67
{
73
68
if ($ timeSlice === null ) {
74
69
$ timeSlice = floor (time () / 30 );
@@ -107,7 +102,7 @@ public function getCode($secret, $timeSlice = null)
107
102
* @return string
108
103
* @throws Exception on encoding error
109
104
*/
110
- public function getQRCodeUrl ($ name , $ secret )
105
+ public function getQRCodeUrl (string $ name , string $ secret ) : string
111
106
{
112
107
$ uri = "otpauth://totp/ $ name?secret= $ secret " ;
113
108
return 'data:image/png;base64, ' . base64_encode ($ this ->getQRCodeSRC ($ uri ));
@@ -119,12 +114,12 @@ public function getQRCodeUrl($name, $secret)
119
114
* @param string $uri to encode into a QRCode
120
115
* @return string binary data of the PNG of the QRCode
121
116
*/
122
- protected function getQRCodeSRC ($ uri )
117
+ protected function getQRCodeSRC (string $ uri ) : string
123
118
{
124
119
$ qr_code = new QrCode ($ uri );
125
120
$ qr_code ->setSize (260 );
126
121
$ qr_code ->setMargin (10 );
127
- $ qr_code ->setErrorCorrectionLevel (ErrorCorrectionLevel::LOW );
122
+ $ qr_code ->setErrorCorrectionLevel (ErrorCorrectionLevel::LOW () );
128
123
$ qr_code ->setForegroundColor (['r ' => 0 , 'g ' => 0 , 'b ' => 0 ]);
129
124
$ qr_code ->setBackgroundColor (['r ' => 255 , 'g ' => 255 , 'b ' => 255 ]);
130
125
$ qr_code ->setValidateResult (false );
@@ -140,7 +135,7 @@ protected function getQRCodeSRC($uri)
140
135
* @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
141
136
* @return bool
142
137
*/
143
- public function verifyCode ($ secret , $ code , $ discrepancy = 1 )
138
+ public function verifyCode (string $ secret , string $ code , int $ discrepancy = 1 ) : bool
144
139
{
145
140
$ currentTimeSlice = floor (time () / 30 );
146
141
@@ -151,7 +146,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
151
146
for ($ i = -$ discrepancy ; $ i <= $ discrepancy ; $ i ++) {
152
147
try {
153
148
$ calculatedCode = $ this ->getCode ($ secret , $ currentTimeSlice + $ i );
154
- } catch (\ Exception $ e ) {
149
+ } catch (Exception $ e ) {
155
150
return false ;
156
151
}
157
152
@@ -169,7 +164,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
169
164
* @param int $length
170
165
* @return self
171
166
*/
172
- public function setCodeLength ($ length )
167
+ public function setCodeLength (int $ length ) : self
173
168
{
174
169
$ this ->_codeLength = $ length ;
175
170
return $ this ;
@@ -179,9 +174,9 @@ public function setCodeLength($length)
179
174
* Helper class to decode base32
180
175
*
181
176
* @param string $secret
182
- * @return bool| string
177
+ * @return string
183
178
*/
184
- private static function base32Decode ($ secret )
179
+ private static function base32Decode (string $ secret ) : string
185
180
{
186
181
if (empty ($ secret )) {
187
182
return '' ;
@@ -193,13 +188,13 @@ private static function base32Decode($secret)
193
188
$ paddingCharCount = substr_count ($ secret , $ base32chars [32 ]);
194
189
$ allowedValues = [6 , 4 , 3 , 1 , 0 ];
195
190
if (!in_array ($ paddingCharCount , $ allowedValues )) {
196
- return false ;
191
+ return '' ;
197
192
}
198
193
199
194
for ($ i = 0 ; $ i < 4 ; $ i ++){
200
195
if ($ paddingCharCount == $ allowedValues [$ i ] &&
201
196
substr ($ secret , -($ allowedValues [$ i ])) != str_repeat ($ base32chars [32 ], $ allowedValues [$ i ])) {
202
- return false ;
197
+ return '' ;
203
198
}
204
199
}
205
200
@@ -208,13 +203,13 @@ private static function base32Decode($secret)
208
203
$ binaryString = "" ;
209
204
for ($ i = 0 ; $ i < count ($ secret ); $ i = $ i +8 ) {
210
205
if (!in_array ($ secret [$ i ], $ base32chars )) {
211
- return false ;
206
+ return '' ;
212
207
}
213
208
214
209
$ x = "" ;
215
210
for ($ j = 0 ; $ j < 8 ; $ j ++) {
216
- $ secretChar = isset ( $ secret [$ i + $ j ]) ? $ secret [ $ i + $ j ] : 0 ;
217
- $ base = isset ( $ base32charsFlipped [$ secretChar ]) ? $ base32charsFlipped [ $ secretChar ] : 0 ;
211
+ $ secretChar = $ secret [$ i + $ j ] ?? 0 ;
212
+ $ base = $ base32charsFlipped [$ secretChar ] ?? 0 ;
218
213
$ x .= str_pad (base_convert ($ base , 10 , 2 ), 5 , '0 ' , STR_PAD_LEFT );
219
214
}
220
215
$ eightBits = str_split ($ x , 8 );
@@ -231,7 +226,7 @@ private static function base32Decode($secret)
231
226
*
232
227
* @return array
233
228
*/
234
- private static function base32LookupTable ()
229
+ private static function base32LookupTable () : array
235
230
{
236
231
return [
237
232
'A ' , 'B ' , 'C ' , 'D ' , 'E ' , 'F ' , 'G ' , 'H ' , // 7
0 commit comments