PHPackages                             hejunjie/google-authenticator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. hejunjie/google-authenticator

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

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.

v1.0.4(1mo ago)21.2k↓42.9%1MITPHPPHP &gt;=8.1CI failing

Since Apr 21Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/zxc7563598/php-google-authenticator)[ Packagist](https://packagist.org/packages/hejunjie/google-authenticator)[ RSS](/packages/hejunjie-google-authenticator/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (1)

hejunjie/google-authenticator
=============================

[](#hejunjiegoogle-authenticator)

English ｜ [简体中文](./README.zh-CN.md)

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](https://zread.ai/zxc7563598/php-google-authenticator).

[![Packagist Version](https://camo.githubusercontent.com/ef30daac1d45c9cc69a8123b3d4296f4e74a8463944cd6afc6c7f2548166efde/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656a756e6a69652f676f6f676c652d61757468656e74696361746f72)](https://packagist.org/packages/hejunjie/google-authenticator)[![PHP Version](https://camo.githubusercontent.com/73887872623e65fe110ddb2d8af8074b9b3e33f01dff779ce92b1ae5668a9195/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f68656a756e6a69652f676f6f676c652d61757468656e74696361746f72)](https://packagist.org/packages/hejunjie/google-authenticator)[![License](https://camo.githubusercontent.com/89070e3f13a0e6be4031adb0ab07d50882c461422fee664b80a981d9eed45120/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f68656a756e6a69652f676f6f676c652d61757468656e74696361746f72)](LICENSE)

Features
--------

[](#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 &gt;= 8.1, with strict typing support

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Composer

Installation
------------

[](#installation)

```
composer require hejunjie/google-authenticator
```

Quick Start
-----------

[](#quick-start)

### Generate a Secret

[](#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)

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
);
```

ParameterDescriptionDefault`$issuer`Application name, displayed as "issuer: label" in AuthenticatorRequired`$label`User identifier, displayed as "issuer: label" in AuthenticatorRequired`$secret`TOTP secretRequired`$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-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
-----

[](#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
-----------------

[](#project-structure)

```
src/
├── GoogleAuthenticator.php    # Main class: secret generation, QR code creation, OTP verification
└── Support/
    └── Base32.php             # Base32 encoding/decoding

```

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance93

Actively maintained with recent releases

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~112 days

Total

5

Last Release

34d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b65d4b40ae456172fb38f63f84bf737ac88031484b1f228b1cc8d71baa80adf?d=identicon)[苏青安](/maintainers/%E8%8B%8F%E9%9D%92%E5%AE%89)

---

Top Contributors

[![zxc7563598](https://avatars.githubusercontent.com/u/46590942?v=4)](https://github.com/zxc7563598 "zxc7563598 (13 commits)")

---

Tags

2faauthenticationcomposergoogle-authenticatorotpphpqr-codesecuritytotp

### Embed Badge

![Health badge](/badges/hejunjie-google-authenticator/health.svg)

```
[![Health](https://phpackages.com/badges/hejunjie-google-authenticator/health.svg)](https://phpackages.com/packages/hejunjie-google-authenticator)
```

###  Alternatives

[leantime/leantime

Open source project management system for non-project managers. Simple like Trello, powerful like Jira. Built with neurodiversity in mind.

11.3k4.0k](/packages/leantime-leantime)[kimai/kimai

Kimai - Time Tracking

4.8k9.4k1](/packages/kimai-kimai)[nativephp/mobile

NativePHP for Mobile

1.1k102.1k146](/packages/nativephp-mobile)[n1ebieski/ksef-php-client

PHP API client that allows you to interact with the API Krajowego Systemu e-Faktur

9082.3k](/packages/n1ebieski-ksef-php-client)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

9626.1k68](/packages/open-dxp-opendxp)[vectorface/googleauthenticator

Google Authenticator 2-factor authentication

19267.2k1](/packages/vectorface-googleauthenticator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
