sebastiandevs/simplethenticator

基于 https://github.com/PHPGangsta/GoogleAuthenticator 的 TOTP,由于原作者不活跃,已更新并重构以支持 php8.2。

1.0 2023-12-09 17:00 UTC

This package is auto-updated.

Last update: 2024-09-18 11:50:36 UTC


README

Software License Total Downloads Latest Stable Version

此类可以生成密钥,生成代码,验证代码,并展示用于扫描密钥的二维码。它根据 RFC6238 实现 TOTP。

示例

查看 tests 中的函数 TestGenerator()

(目前除 SHA1 以外的哈希函数仅适用于 Google-Authenticator!)

$auth = new SimpleAuthenticator(6, 'SHA1');
try
{
    $secret = $auth->createSecret();
}
catch (Exception $e)
{
    echo $e->getMessage();
    exit();
}
echo 'Secret is: ' . $secret . '\n';

$qrCodeUrl = $auth->getQRCodeGoogleUrl($secret, 'Testo@test.test', 'Business');
echo 'QR-Code: ' . $qrCodeUrl . '\n';

$oneCode = $auth->getCode($secret);
echo 'Checking Code ' . $oneCode . ' and Secret ' . $secret . ':\n';

echo $auth->verifyCode($secret, $oneCode, 2)? 'OK': 'NOT OK';

输出

Secret is: S4VWK6CWPA3PMU2HZM2YEDZGSF2DQL3V

Google Charts URL for the QR-Code: https://api.qrserver.com/v1/create-qr-code/?data=otpauth%3A%2F%2Ftotp%2FVintage+Story%3ATesto%40test.test%3Fsecret%3DS4VWK6CWPA3PMU2HZM2YEDZGSF2DQL3V%26algorithm%3DSHA256%26issuer%3DVintage+Story&size=200x200&ecc=M

Checking Code '439195' and Secret 'S4VWK6CWPA3PMU2HZM2YEDZGSF2DQL3V':
OK

安装

Composer

  • 要使用此包,请执行以下命令

composer require sebastiandevs/simplethenticator

简单使用

运行测试

  • 所有测试都在 tests 文件夹中。
  • 执行 composer run-script build-dev 以安装所有依赖项
  • 执行 composer run-script test 以运行测试文件夹中的所有测试

更好的库