hejunjie/google-authenticator
Composer 安装命令:
composer require hejunjie/google-authenticator
包简介
一个用于生成和验证时间基础一次性密码(TOTP)的 PHP 包,支持 Google Authenticator 及类似应用。功能包括密钥生成、二维码创建和 OTP 验证 | A PHP library for generating and verifying Time-Based One-Time Passwords (TOTP). Compatible with Google Authenticator and similar apps, with features like secret generation, QR code creation, and OTP verification.
README 文档
README
English | 简体中文
A lightweight PHP TOTP authentication library for generating secrets, creating QR codes, and verifying Google Authenticator-compatible one-time passwords.
🔗 Quickly understand this project's structure and code logic via Zread.
Features
- 🔑 Generate secure Base32-encoded TOTP secrets
- 📱 Create QR code images with customizable size and logo
- ✅ Verify 6-digit OTP codes entered by users
- 🪶 Only depends on
endroid/qr-code, no other unnecessary dependencies - 🐘 Requires PHP >= 8.1, with strict typing support
Requirements
- PHP >= 8.1
- Composer
Installation
composer require hejunjie/google-authenticator
Quick Start
Generate a Secret
Generate a unique TOTP secret for each user. It is recommended to encrypt it before storing in the database.
use Hejunjie\GoogleAuthenticator\GoogleAuthenticator; $secret = GoogleAuthenticator::generateSecret(); // => "3PVPN3ASEIM457VR5VNUONDQB4"
Generate a QR Code
Generate a QR code image that users can scan with Google Authenticator to add the account.
use Hejunjie\GoogleAuthenticator\GoogleAuthenticator; $issuer = 'MyApp'; // Application name $label = 'user@example.com'; // User identifier $secret = '3PVPN3ASEIM457VR5VNUONDQB4'; // Basic usage (with default parameters) GoogleAuthenticator::getQRCodeFile($issuer, $label, $secret, '/path/to/qrcode.png'); // Custom size and logo GoogleAuthenticator::getQRCodeFile( issuer: 'MyApp', label: 'user@example.com', secret: $secret, path: '/path/to/qrcode.png', width: 400, logo: '/path/to/logo.png', logo_width: 60 );
| Parameter | Description | Default |
|---|---|---|
$issuer |
Application name, displayed as "issuer: label" in Authenticator | Required |
$label |
User identifier, displayed as "issuer: label" in Authenticator | Required |
$secret |
TOTP secret | Required |
$path |
File path for saving the QR code image (including filename) | Required |
$width |
Image width in pixels | 300 |
$logo |
Logo file path, pass an empty string to disable | '' |
$logo_width |
Logo width in pixels, ignored when $logo is empty |
50 |
Verify OTP
Verify the 6-digit code entered by the user.
use Hejunjie\GoogleAuthenticator\GoogleAuthenticator; $secret = '3PVPN3ASEIM457VR5VNUONDQB4'; $code = '123456'; if (GoogleAuthenticator::checkCode($secret, $code)) { echo 'Verification passed'; } else { echo 'Verification failed'; }
The third optional parameter $timeWindow controls the time step (default 30 seconds), matching the Google Authenticator default setting. It generally does not need to be changed.
Notes
- Clock synchronization: TOTP relies on server time. Ensure NTP time synchronization is configured. The current implementation only verifies the current time window — a significant time skew between the server and user device may cause verification failures.
- Secret security: The secret is equivalent to a password. Store it securely (e.g., encrypted in the database). If leaked, an attacker can directly generate valid OTPs.
- File permissions: When generating QR codes, ensure the target directory exists and PHP has write permissions.
Project Structure
src/
├── GoogleAuthenticator.php # Main class: secret generation, QR code creation, OTP verification
└── Support/
└── Base32.php # Base32 encoding/decoding
hejunjie/google-authenticator 适用场景与选型建议
hejunjie/google-authenticator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.01k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hejunjie/google-authenticator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hejunjie/google-authenticator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-21