lfkeitel/phptotp
Composer 安装命令:
composer require lfkeitel/phptotp
包简介
TOTP/HOTP library for PHP
README 文档
README
This is a simple PHP library and script that will generate HOTP and TOTP tokens. The library fully conforms to RFCs 4226 and 6238. All hashing algorithms are supported as well as the length of a token and the start time for TOTP.
Installation
Add the following to your composer.json:
{
"require": {
"lfkeitel/phptotp": "^1.0"
}
}
And run composer install.
Usage
<?php use lfkeitel\phptotp\{Base32,Totp}; # Generate a new secret key # Note: GenerateSecret returns a string of random bytes. It must be base32 encoded before displaying to the user. You should store the unencoded string for later use. $secret = Totp::GenerateSecret(16); # Display new key to user so they can enter it in Google Authenticator or Authy echo Base32::encode($secret); # Generate the current TOTP key # Note: GenerateToken takes a base32 decoded string of bytes. $key = (new Totp())->GenerateToken($secret); # Check if user submitted correct key if ($user_submitted_key !== $key) { exit(); }
Documentation
lfkeitel\phptotp\Totp extends Hotp
__construct($algo = 'sha1', $start = 0, $ti = 30): Totp$algo: Algorithm to use when generating token$start: The beginning of time$ti: Time interval between tokens
GenerateToken($key, $time = null, $length = 6): string$key: Secret key as bytes, base32 decoded$time: Time to use for the token, defaults to now$length: Length of token
lfkeitel\phptotp\Hotp
__construct($algo = 'sha1'): Hotp$algo: Algorithm to use when generating token
GenerateToken($key, $count = 0, $length = 6): string$key: Secret key as bytes, base32 decoded$count: HOTP counter$length: Length of token
GenerateSecret($length = 16): string$length: Length of string in bytesReturn: This method returns a string of random bytes, use Base32::encode when displaying to the user.
lfkeitel\phptotp\Base32
static encode($data): string$data: Data to base32 encode
static decode($data): string$data: Data to base32 decode
generate.php
generate.php is a script that acts exactly like Google Authenticator. It takes a secret key, either as an argument or can be entered when prompted on standard input, and generates a token assuming SHA1, Unix timestamp for start, and 30 second time intervals. The secret key should be base32 encoded.
$ ./generate.php Enter secret key: turtles Token: 338914 $
License
This software is released under the MIT license which can be found in LICENSE.
lfkeitel/phptotp 适用场景与选型建议
lfkeitel/phptotp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 479.57k 次下载、GitHub Stars 达 85, 最近一次更新时间为 2017 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「two-factor」 「hotp」 「totp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lfkeitel/phptotp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lfkeitel/phptotp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lfkeitel/phptotp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Security tools for PHP
Simple HOTP/TOTP generation library
Filament Two Factor Authentication: Google 2FA + Passkey Authentication
A Google Authenticator Bundle for Symfony
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
统计信息
- 总下载量: 479.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 85
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-06