You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
class Totp
|
|
{
|
|
const BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
const DIGITS = 6;
|
|
const PERIOD = 30;
|
|
|
|
public static function generateSecret($bytes = 20)
|
|
{
|
|
$random = random_bytes($bytes);
|
|
$bits = '';
|
|
for ($i = 0; $i < strlen($random); $i++) {
|
|
$bits .= str_pad(decbin(ord($random[$i])), 8, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
$secret = '';
|
|
for ($i = 0; $i < strlen($bits); $i += 5) {
|
|
$chunk = substr($bits, $i, 5);
|
|
if (strlen($chunk) < 5) {
|
|
$chunk = str_pad($chunk, 5, '0', STR_PAD_RIGHT);
|
|
}
|
|
$secret .= self::BASE32_ALPHABET[bindec($chunk)];
|
|
}
|
|
|
|
return $secret;
|
|
}
|
|
|
|
public static function normalizeSecret($secret)
|
|
{
|
|
$secret = strtoupper(preg_replace('/[\s=]+/', '', (string) $secret));
|
|
if (!preg_match('/^[A-Z2-7]{16,}$/', $secret)) {
|
|
return '';
|
|
}
|
|
|
|
return $secret;
|
|
}
|
|
|
|
public static function otpauthUri($issuer, $account, $secret)
|
|
{
|
|
$issuer = trim((string) $issuer) !== '' ? trim((string) $issuer) : 'App';
|
|
$account = trim((string) $account) !== '' ? trim((string) $account) : 'user';
|
|
$secret = self::normalizeSecret($secret);
|
|
$label = rawurlencode($issuer . ':' . $account);
|
|
|
|
return 'otpauth://totp/' . $label
|
|
. '?secret=' . rawurlencode($secret)
|
|
. '&issuer=' . rawurlencode($issuer)
|
|
. '&algorithm=SHA1&digits=' . self::DIGITS
|
|
. '&period=' . self::PERIOD;
|
|
}
|
|
|
|
public static function verify($secret, $code, $window = 1, $time = null)
|
|
{
|
|
$secret = self::normalizeSecret($secret);
|
|
$code = preg_replace('/\s+/', '', (string) $code);
|
|
if ($secret === '' || !preg_match('/^\d{6}$/', $code)) {
|
|
return false;
|
|
}
|
|
|
|
$time = $time === null ? time() : (int) $time;
|
|
$counter = (int) floor($time / self::PERIOD);
|
|
$window = max(0, (int) $window);
|
|
|
|
for ($offset = -$window; $offset <= $window; $offset++) {
|
|
$expected = self::generateCodeAtCounter($secret, $counter + $offset);
|
|
if ($expected !== '' && hash_equals($expected, $code)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static function generateCodeAtCounter($secret, $counter)
|
|
{
|
|
$key = self::base32Decode($secret);
|
|
if ($key === '' || $counter < 0) {
|
|
return '';
|
|
}
|
|
|
|
$binaryCounter = pack('N*', 0, (int) $counter);
|
|
$hash = hash_hmac('sha1', $binaryCounter, $key, true);
|
|
$offset = ord($hash[19]) & 0x0f;
|
|
$truncated = ((ord($hash[$offset]) & 0x7f) << 24)
|
|
| ((ord($hash[$offset + 1]) & 0xff) << 16)
|
|
| ((ord($hash[$offset + 2]) & 0xff) << 8)
|
|
| (ord($hash[$offset + 3]) & 0xff);
|
|
|
|
return str_pad((string) ($truncated % 1000000), self::DIGITS, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
private static function base32Decode($secret)
|
|
{
|
|
$secret = self::normalizeSecret($secret);
|
|
if ($secret === '') {
|
|
return '';
|
|
}
|
|
|
|
$bits = '';
|
|
for ($i = 0; $i < strlen($secret); $i++) {
|
|
$value = strpos(self::BASE32_ALPHABET, $secret[$i]);
|
|
if ($value === false) {
|
|
return '';
|
|
}
|
|
$bits .= str_pad(decbin($value), 5, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
$bytes = '';
|
|
for ($i = 0; $i + 8 <= strlen($bits); $i += 8) {
|
|
$bytes .= chr(bindec(substr($bits, $i, 8)));
|
|
}
|
|
|
|
return $bytes;
|
|
}
|
|
}
|