yaman-shahbander-dev/totp-generator
Composer 安装命令:
composer require yaman-shahbander-dev/totp-generator
包简介
A PHP implementation of Time-Based One-Time Password (TOTP), using Base32 encoding and HMAC hashing. Generates and verifies OTPs for secure authentication.
README 文档
README
A Laravel package for generating and verifying Time-Based One-Time Passwords (TOTP) compatible with RFC 6238. Provides both facade and contract-based implementations.
Features
- Generate TOTP codes
- Verify TOTP codes with configurable window
- Base32 encoding/decoding utilities
- Configurable parameters (period, digits, verification window)
- Laravel facade and service provider integration
Installation
- Install via Composer:
composer require yaman-shahbander-dev/laravel-totp
- Publish the configuration file (optional):
php artisan vendor:publish --provider="TotpGenerator\Providers\TotpServiceProvider" --tag="config"
Configuration
Default values in config/totp.php:
'default_period' => 30, // Time step in seconds 'default_digits' => 6, // Number of digits in OTP 'verification_window' => 1, // Number of periods to check before/after
Usage
Using the Facade
use TotpGenerator\Facades\Totp; // Generate a random secret (store this securely) $secret = Str::random(16); $base32Secret = Totp::encodeBase32($secret); // Generate current TOTP $code = Totp::generate($base32Secret); // Verify a code $isValid = Totp::verify($userCode, $base32Secret); // With custom window $isValid = Totp::verify($userCode, $base32Secret, 2);
Using Dependency Injection
use TotpGenerator\Contracts\TotpGeneratorContract; class AuthController { public function __construct( protected TotpGeneratorContract $totp ) {} public function verifyCode(Request $request) { $isValid = $this->totp->verify( $request->code, $user->totp_secret ); } }
Direct Usage
$totp = app(TotpGeneratorContract::class); $code = $totp->generate($base32Secret);
Base32 Utilities
// Encode binary to Base32 $base32Secret = Totp::encodeBase32(random_bytes(16)); // Decode Base32 to binary $binarySecret = Totp::decodeBase32($base32Secret);
Security Considerations
- Always store secrets securely (encrypted at rest)
- Use secure random bytes for secret generation
- Consider rate limiting verification attempts
- The package uses SHA-1 by default (compatible with most authenticator apps)
License
MIT License (see LICENSE file)
yaman-shahbander-dev/totp-generator 适用场景与选型建议
yaman-shahbander-dev/totp-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 yaman-shahbander-dev/totp-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yaman-shahbander-dev/totp-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-15