jp3cki/totp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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).

License Latest Stable Version CI

Requirements

  • PHP (64-bits): PHP 8.2 or later
  • PHP Extensions: hash

Install

  1. Set up Composer, the de facto standard package manager.
  2. 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, and sha512. Do not forward an untrusted hash parameter into calc() / verify() from user input.

License

The MIT 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() and Totp::verify() no longer accept arbitrary hash algorithms from hash_algos(). Only sha1, sha256, and sha512 (the algorithms defined by RFC 6238) are allowed; any other value now throws InvalidArgumentException. Callers that previously passed values such as md5 or crc32 must switch to one of the supported algorithms.
    • The default verification window of Totp::verify() is narrowed. $acceptStepPast now defaults to 1 (was 2), matching the maximum drift recommended by RFC 6238 §5.2. To restore the previous behaviour, pass $acceptStepPast: 2 explicitly.
    • 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. Pass Totp::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 use random_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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jp3cki/totp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-08