File tree 1 file changed +8
-2
lines changed
1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ trait TOTP
21
21
* @param int $length How many digits should each TOTP be?
22
22
* @param string $algo Hash function to use
23
23
* @return string
24
+ * @throws \OutOfRangeException
24
25
*/
25
26
public function getTOTPCode (
26
27
string $ sharedSecret ,
@@ -30,6 +31,11 @@ public function getTOTPCode(
30
31
int $ length = 6 ,
31
32
string $ algo = 'sha1 '
32
33
): string {
34
+ if ($ length < 1 || $ length > 10 ) {
35
+ throw new \OutOfRangeException (
36
+ 'Length must be between 1 and 10, as a consequence of RFC 6238. '
37
+ );
38
+ }
33
39
$ msg = $ this ->getTValue ($ unixTimestamp , $ timeZero , $ timeStep , true );
34
40
$ bytes = \hash_hmac ($ algo , $ msg , $ sharedSecret , true );
35
41
@@ -50,10 +56,10 @@ public function getTOTPCode(
50
56
| (($ unpacked [3 ] & 0xff ) )
51
57
);
52
58
53
- $ intValue %= pow ( 10 , $ length) ;
59
+ $ intValue %= 10 ** $ length ;
54
60
55
61
return \str_pad (
56
- '' . $ intValue ,
62
+ ( string ) $ intValue ,
57
63
$ length ,
58
64
'0 ' ,
59
65
\STR_PAD_LEFT
You can’t perform that action at this time.
0 commit comments