liquid-cats / g2fa
Google 2FA
v1.0.1
2023-12-19 17:12 UTC
Requires
- php: >=8.2
- paragonie/constant_time_encoding: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~v3.41
- phpunit/phpunit: ^9.6
README
此仓库受到 https://github.com/antonioribeiro/google2fa 的启发。这个库提供了PHP类来生成和验证基于时间的单次密码(TOTP),符合G2FA(Google双因素认证)标准。它包括TOTP和SecretKey类,旨在协同工作,提供强大的双因素认证解决方案。
安装
composer require liquid-cats/g2fa
1. TOTP
此类负责根据密钥和时间生成和验证TOTP。
功能
根据给定的时间戳生成TOTP。验证TOTP与当前或提供的时间戳。可自定义算法、长度、纪元和周期。使用方法
Copy code use LiquidCats\G2FA\TOTP; use LiquidCats\G2FA\Enums\Algorithm; use LiquidCats\G2FA\ValueObjects\SecretKey; $totp = new TOTP(Algorithm::SHA512, 6, 30, 1); $secretKey = new SecretKey('YOUR_SECRET_KEY'); $otp = $totp->now($secretKey); // To verify OTP $isValid = $totp->verify($secretKey, $otp);
2. SecretKey
SecretKey类用于处理生成TOTP所需的密钥。
功能
验证密钥的格式和大小。从Base32编码解码密钥。静态方法用于生成新的密钥。
使用方法
Copy code use LiquidCats\G2FA\ValueObjects\SecretKey; // Creating a SecretKey $secretKey = new SecretKey('YOUR_SECRET_KEY'); // Decoding a SecretKey $decodedKey = $secretKey->decode(); // Generating a new SecretKey $newSecretKey = SecretKey::generate();
贡献
欢迎对这个库的贡献和改进。请提交拉取请求或打开问题报告任何错误、功能请求或增强。
许可证
该项目开源许可协议为MIT许可。请参阅LICENSE文件以获取更多详细信息。