@@ -27,7 +27,7 @@ class GoogleAuthenticator
2727 * @return string
2828 * @throws Exception
2929 */
30- public function createSecret ($ secretLength = 16 )
30+ public function createSecret (int $ secretLength = 16 ) : string
3131 {
3232 $ validChars = self ::base32LookupTable ();
3333
@@ -40,11 +40,6 @@ public function createSecret($secretLength = 16)
4040 $ rnd = false ;
4141 if (function_exists ('random_bytes ' )) {
4242 $ 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- }
4843 }
4944
5045 if (!$ rnd ) {
@@ -68,7 +63,7 @@ public function createSecret($secretLength = 16)
6863 * @return string
6964 * @throws Exception
7065 */
71- public function getCode ($ secret , $ timeSlice = null )
66+ public function getCode (string $ secret , int $ timeSlice = null ) : string
7267 {
7368 if ($ timeSlice === null ) {
7469 $ timeSlice = floor (time () / 30 );
@@ -107,7 +102,7 @@ public function getCode($secret, $timeSlice = null)
107102 * @return string
108103 * @throws Exception on encoding error
109104 */
110- public function getQRCodeUrl ($ name , $ secret )
105+ public function getQRCodeUrl (string $ name , string $ secret ) : string
111106 {
112107 $ uri = "otpauth://totp/ $ name?secret= $ secret " ;
113108 return 'data:image/png;base64, ' . base64_encode ($ this ->getQRCodeSRC ($ uri ));
@@ -119,12 +114,12 @@ public function getQRCodeUrl($name, $secret)
119114 * @param string $uri to encode into a QRCode
120115 * @return string binary data of the PNG of the QRCode
121116 */
122- protected function getQRCodeSRC ($ uri )
117+ protected function getQRCodeSRC (string $ uri ) : string
123118 {
124119 $ qr_code = new QrCode ($ uri );
125120 $ qr_code ->setSize (260 );
126121 $ qr_code ->setMargin (10 );
127- $ qr_code ->setErrorCorrectionLevel (ErrorCorrectionLevel::LOW );
122+ $ qr_code ->setErrorCorrectionLevel (ErrorCorrectionLevel::LOW () );
128123 $ qr_code ->setForegroundColor (['r ' => 0 , 'g ' => 0 , 'b ' => 0 ]);
129124 $ qr_code ->setBackgroundColor (['r ' => 255 , 'g ' => 255 , 'b ' => 255 ]);
130125 $ qr_code ->setValidateResult (false );
@@ -140,7 +135,7 @@ protected function getQRCodeSRC($uri)
140135 * @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
141136 * @return bool
142137 */
143- public function verifyCode ($ secret , $ code , $ discrepancy = 1 )
138+ public function verifyCode (string $ secret , string $ code , int $ discrepancy = 1 ) : bool
144139 {
145140 $ currentTimeSlice = floor (time () / 30 );
146141
@@ -151,7 +146,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
151146 for ($ i = -$ discrepancy ; $ i <= $ discrepancy ; $ i ++) {
152147 try {
153148 $ calculatedCode = $ this ->getCode ($ secret , $ currentTimeSlice + $ i );
154- } catch (\ Exception $ e ) {
149+ } catch (Exception $ e ) {
155150 return false ;
156151 }
157152
@@ -169,7 +164,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
169164 * @param int $length
170165 * @return self
171166 */
172- public function setCodeLength ($ length )
167+ public function setCodeLength (int $ length ) : self
173168 {
174169 $ this ->_codeLength = $ length ;
175170 return $ this ;
@@ -179,9 +174,9 @@ public function setCodeLength($length)
179174 * Helper class to decode base32
180175 *
181176 * @param string $secret
182- * @return bool| string
177+ * @return string
183178 */
184- private static function base32Decode ($ secret )
179+ private static function base32Decode (string $ secret ) : string
185180 {
186181 if (empty ($ secret )) {
187182 return '' ;
@@ -193,13 +188,13 @@ private static function base32Decode($secret)
193188 $ paddingCharCount = substr_count ($ secret , $ base32chars [32 ]);
194189 $ allowedValues = [6 , 4 , 3 , 1 , 0 ];
195190 if (!in_array ($ paddingCharCount , $ allowedValues )) {
196- return false ;
191+ return '' ;
197192 }
198193
199194 for ($ i = 0 ; $ i < 4 ; $ i ++){
200195 if ($ paddingCharCount == $ allowedValues [$ i ] &&
201196 substr ($ secret , -($ allowedValues [$ i ])) != str_repeat ($ base32chars [32 ], $ allowedValues [$ i ])) {
202- return false ;
197+ return '' ;
203198 }
204199 }
205200
@@ -208,13 +203,13 @@ private static function base32Decode($secret)
208203 $ binaryString = "" ;
209204 for ($ i = 0 ; $ i < count ($ secret ); $ i = $ i +8 ) {
210205 if (!in_array ($ secret [$ i ], $ base32chars )) {
211- return false ;
206+ return '' ;
212207 }
213208
214209 $ x = "" ;
215210 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 ;
218213 $ x .= str_pad (base_convert ($ base , 10 , 2 ), 5 , '0 ' , STR_PAD_LEFT );
219214 }
220215 $ eightBits = str_split ($ x , 8 );
@@ -231,7 +226,7 @@ private static function base32Decode($secret)
231226 *
232227 * @return array
233228 */
234- private static function base32LookupTable ()
229+ private static function base32LookupTable () : array
235230 {
236231 return [
237232 'A ' , 'B ' , 'C ' , 'D ' , 'E ' , 'F ' , 'G ' , 'H ' , // 7
0 commit comments