jp3cki/totp
Composer 安装命令:
composer require jp3cki/totp
包简介
RFC 6238 / TOTP: Time-Based One-Time Password Algorithm
README 文档
README
PHP implementation of RFC6238 (TOTP: Time-Based One-Time Password Algorithm).
Requirements
- PHP (64-bits): PHP 8.2 or later
- PHP Extensions: hash
Install
- Set up Composer, the de facto standard package manager.
php composer.phar require jp3cki/totp
Usage
<?php declare(strict_types=1); use jp3cki\totp\Totp; require_once('vendor/autoload.php'); // Generate new shared-secret key (for each user) $secret = Totp::generateKey(); echo "secret: {$secret}\n"; echo "\n"; // Make URI for importing from QRCode. $uri = Totp::createKeyUriForGoogleAuthenticator($secret, 'theuser@example.com', 'Issuer Name'); echo "uri: {$uri}\n"; echo "\n"; // Verify user input $userInput = '123456'; // $_POST['totp'] $isValid = Totp::verify($userInput, $secret, time()); var_dump($isValid);
Security Notes
- Replay protection is the caller's responsibility.
Totp::verify()does not remember which one-time codes have already been used. Per RFC 6238 §5.2, a server MUST reject any TOTP value that was previously accepted within the validity window. Store the most recently accepted time-step (or the accepted code itself) per user, and reject any submission that is equal to or older than it. - Rate-limit verification attempts. A 6-digit code with the default ±1-step window leaves roughly 3 codes valid at any moment, i.e. a 3-in-1,000,000 chance per guess. Without throttling, an attacker can brute force a valid code in minutes. Apply per-account lockout or exponential back-off in the calling application.
- Restrict the hash algorithm to RFC 6238 values. The library only accepts
sha1,sha256, andsha512. Do not forward an untrustedhashparameter intocalc()/verify()from user input.
License
Copyright (c) 2015-2026 AIZAWA Hina <hina@fetus.jp>
Contributing
Patches and/or report issues are welcome.
- Please create new branch for each issue or feature. (should not work in master branch)
- Please write and run test.
$ make test - Please run check-style for static code analysis and coding rule checking.
$ make check-style - Please clean up commits.
- Please create new pull-request for each issue or feature.
- Please use Japanese or very simple English to create new pull-request or issue.
Breaking Changes
-
v4.0.0
- Minimum environment is now PHP 8.2
Totp::calc()andTotp::verify()no longer accept arbitrary hash algorithms fromhash_algos(). Onlysha1,sha256, andsha512(the algorithms defined by RFC 6238) are allowed; any other value now throwsInvalidArgumentException. Callers that previously passed values such asmd5orcrc32must switch to one of the supported algorithms.- The default verification window of
Totp::verify()is narrowed.$acceptStepPastnow defaults to1(was2), matching the maximum drift recommended by RFC 6238 §5.2. To restore the previous behaviour, pass$acceptStepPast: 2explicitly. Totp::generateKey()now defaults to a 160-bit shared secret (was 80 bits), matching the HMAC-SHA1 output length recommended by RFC 4226 §4 R6. Existing keys keep working; only newly generated keys are longer. PassTotp::generateKey(80)to restore the previous size.
-
v3.0.0
- Minimum environment is now PHP 8.1
-
v2.0.0
- Minimum environment is now PHP 7.2
- Argument types are now strictly enforced
- Removed
Random::generate*(). Always userandom_bytes()now.
jp3cki/totp 适用场景与选型建议
jp3cki/totp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.09k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「password」 「otp」 「totp」 「rfc6238」 「onetime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jp3cki/totp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jp3cki/totp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jp3cki/totp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The PHP class PasswordGenerator serves as a password generator to create memorable passwords like the keychain of macOS ≤ 10.14 did.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
A library for reading KeePass 2.x databases
Google Authenticator 2-factor authentication
统计信息
- 总下载量: 24.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-08