vjolenz/php-otp

该软件包已被 废弃 并不再维护。未建议替代软件包。
该软件包最新版本(v1.0.0)没有提供许可证信息。

PHP OTP(一次性密码)实现

v1.0.0 2017-12-20 21:03 UTC

This package is auto-updated.

Last update: 2023-07-29 01:36:32 UTC


README

Build Status StyleCI

一个用于生成和验证一次性密码的PHP库。它与 HOTPTOTP 兼容。

先决条件

此库至少需要 PHP 7.0

安装

您可以通过composer安装。

$ composer require vjolenz/php-otp

HOTP使用

生成和验证需要一个每次使用都变化的移动因素。您可以使用登录计数器作为移动因素。

创建密码
   $user = User::find(1);
   
   $authenticator = new \vjolenz\OtpAuth\HotpAuthenticator();
   $authenticator->setSecret('12345678901234567890'); // Default: null
   $authenticator->setAlgorithm('SHA256'); // Default: SHA1
   $authenticator->setWindowSize(3); // Default: 1
   $authenticator->setPasswordLength(9); // Default: 6
   
   $password = $authenticator->generatePassword($user->getLoginCounter());
   
   $user->advanceLoginCounter();
验证密码
   $user = User::find(1);
   
   $authenticator = new \vjolenz\OtpAuth\HotpAuthenticator();
   $authenticator->setSecret('12345678901234567890'); // Default: null
   $authenticator->setAlgorithm('SHA256'); // Default: SHA1
   $authenticator->setWindowSize(3); // Default: 1
   $authenticator->setPasswordLength(9); // Default: 6
   
   $authenticator->verifyPassword($password, $user->getLoginCounter());

TOTP使用

与HOTP的生成和验证不同,您不需要移动因素,因为这些操作使用当前时间戳。

创建密码
   $authenticator = new \vjolenz\OtpAuth\TotpAuthenticator();
   $authenticator->setSecret('12345678901234567890'); // Default: null
   $authenticator->setAlgorithm('SHA256'); // Default: SHA1
   $authenticator->setWindowSize(3); // Default: 1
   $authenticator->setPasswordLength(9); // Default: 6
   $authenticator->setInterval(60); // Default: 30
   
   $password = $authenticator->generatePassword();
验证密码
   $authenticator = new \vjolenz\OtpAuth\TotpAuthenticator();
   $authenticator->setSecret('12345678901234567890'); // Default: null
   $authenticator->setAlgorithm('SHA256'); // Default: SHA1
   $authenticator->setWindowSize(3); // Default: 1
   $authenticator->setPasswordLength(9); // Default: 6
   $authenticator->setInterval(60); // Default: 30
   
   $authenticator->verifyPassword($password);

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件