一次性密码,根据RFC4226和RFC6238的hotp和totp
2.7.0
2021-02-23 20:13 UTC
Requires
- php: >=5.6.0
- paragonie/constant_time_encoding: ^1|^2
- paragonie/random_compat: >=1
- symfony/polyfill-php56: ^1
Requires (Dev)
- phpunit/phpunit: ^5.7.11 || ^6.0.5
README
你喜欢这个吗?赞助它
安装
使用 composer 并在 composer.json
中要求库
{
"require": {
"christian-riesen/otp": "^2.0",
}
}
使用方法
<?php use Otp\Otp; use Otp\GoogleAuthenticator; use ParagonIE\ConstantTime\Encoding; // Get a Pseudo Secret // Defaults to 16 characters $secret = GoogleAuthenticator::generateRandom(); // Url for the QR code // Using totp method $url = GoogleAuthenticator::getQrCodeUrl('totp', 'Label like user@host.com', $secret); // Save the secret with the users account // Display QR Code to the user // Now how to check $otp = new Otp(); // $key is a 6 digit number, coming from the User // Assuming this is present and sanitized // Allows for a 1 code time drift by default // Third parameter can alter that behavior if ($otp->checkTotp(Encoding::base32DecodeUpper($secret), $key)) { // Correct key // IMPORTANT! Note this key as being used // so nobody could launch a replay attack. // Cache that for the next minutes and you // should be good. } else { // Wrong key } // Just to create a key for display (testing) $key = $otp->totp(Encoding::base32DecodeUpper($secret));
在 example
文件夹中的示例脚本。需要会话才能工作(用于密钥存储)。
类 Otp
实现了根据RFC4226的hotp和根据RFC6238的totp(仅sha1、sha256和sha512算法)。一旦你有了密钥,你就可以直接在这个类中使用它来创建密码本身(主要用于调试)或使用检查函数来安全地检查密钥的有效性。`checkTotp` 函数还包括一个辅助功能来对抗时间漂移。
类 GoogleAuthenticator
静态函数类,用于生成二维码的正确URL,你可以用你的设备轻松扫描。Google Authenticator是iPhone和Android上的应用程序。通过使用这些类,这减少了网站开发者为创建此类应用程序的负担。
还有适用于iPhone和Android的Google Authenticator应用程序的较旧的开源版本,分别可以在iPhone和Android上找到。
关于
要求
PHP >= 5.4.0
使用paragonie/random_compat和paragonie/constant_time_encoding。
如果你要运行测试,需要PHPUnit >= 4.8.35。
作者
Christian Riesen chris.riesen@gmail.com http://christianriesen.com
致谢
这些类受到了许多关于otp和Google Authenticator的讨论的启发。感谢大家的帮助。
项目设置想法明显借鉴自https://github.com/Seldaek/monolog