Skip to content

Commit 5f5427a

Browse files
Use ** operator, add range check
1 parent 09fd743 commit 5f5427a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Traits/TOTP.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ trait TOTP
2121
* @param int $length How many digits should each TOTP be?
2222
* @param string $algo Hash function to use
2323
* @return string
24+
* @throws \OutOfRangeException
2425
*/
2526
public function getTOTPCode(
2627
string $sharedSecret,
@@ -30,6 +31,11 @@ public function getTOTPCode(
3031
int $length = 6,
3132
string $algo = 'sha1'
3233
): 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+
}
3339
$msg = $this->getTValue($unixTimestamp, $timeZero, $timeStep, true);
3440
$bytes = \hash_hmac($algo, $msg, $sharedSecret, true);
3541

@@ -50,10 +56,10 @@ public function getTOTPCode(
5056
| (($unpacked[3] & 0xff) )
5157
);
5258

53-
$intValue %= pow(10, $length);
59+
$intValue %= 10 ** $length;
5460

5561
return \str_pad(
56-
'' . $intValue,
62+
(string) $intValue,
5763
$length,
5864
'0',
5965
\STR_PAD_LEFT

0 commit comments

Comments
 (0)