PHPackages                             verdient/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. verdient/google-authenticator

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

verdient/google-authenticator
=============================

google authenticator

0.1.0(5y ago)0177MITPHPPHP &gt;=7.0.0

Since Jan 29Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Verdient/google-authenticator)[ Packagist](https://packagist.org/packages/verdient/google-authenticator)[ Docs](https://github.com/Verdient/google-authenticator)[ RSS](/packages/verdient-google-authenticator/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (3)Versions (9)Used By (0)

GoogleAuthenticator
===================

[](#googleauthenticator)

**谷歌验证器**

创建验证器实例
-------

[](#创建验证器实例)

```
use Verdient\GoogleAuthenticator\GoogleAuthenticator;

/**
 * 密钥长度 (可选)
 * 必须为8的倍数的正整数
 * 默认为32
 */
$sercetLength = 32;

/**
 * 类型 (可选)
 * 可选值 totp(基于时间)，hotp（基于计数器）
 * 默认为totp
 */
$type = 'totp';

/**
 * issuer (可选)
 * 发行方
 * 默认为null
 */
$issuer = null;

/**
 * 算法 (可选)
 * 可选 SHA1，SHA256，SHA512
 * 默认为SHA1
 */
$algorithm = 'SHA1';

/**
 * 位数 (可选)
 * 可选6，8
 * 默认为6
 */
$digits = 6;

/**
 * 周期 (可选，仅在type为totp时有效)
 * 默认为30
 */
$period = 30;

/**
 * 二维码生成器 (可选)
 * 默认Verdient\GoogleAuthenticator\QrImageGenerator\EndroidGenerator
 * EndroidGenerator需要通过 composer require endroid/qr-code 安装endroid/qr-code
 * 可选值 Verdient\GoogleAuthenticator\QrImageGenerator\GoQRGenerator,仅支持HTTP
 * 可选值 Verdient\GoogleAuthenticator\QrImageGenerator\TopscanGenerator,仅支持HTTPS
 */
$qrImageGenerator = 'Verdient\GoogleAuthenticator\QrImageGenerator\EndroidGenerator';

$authenticator = new GoogleAuthenticator([
	'sercetLength' => $sercetLength,
	'qrImageGenerator' => $qrImageGenerator
]);
```

生成密钥
----

[](#生成密钥)

```
/**
 * 秘钥长度(可选)
 * 该参数为空时秘钥长度以sercetLength的配置为准
 */
$length = null;
$secret = $authenticator->generateSecret($length);
```

获取绑定数据
------

[](#获取绑定数据)

首先准备基础数据

```
$name = '${名称}'; //名称
$secret = '${secret}'; //密钥，这里一般是用上面生成的密钥
$options = [
	'issuer' => '${issuer}', // 发行方
	'algorithm' => '${algorithm}', // 算法
	'digits' => '${digits}', // 位数
	'counter' => '${counter}', // 计数（仅当type为hotp时有效）
	'period' => '${period}' // 周期（仅当type为totp时有效）
]; // 选项，用于覆盖全局配置，一般情况下不用传
```

### 前端生成二维码

[](#前端生成二维码)

```
$data = $authenticator->getUri($name, $secret, $options);
```

`$data` 为用于生成二维码的数据，可将`$data`和`$secret`一起返回给前端，由前端根据`$data`生成二维码并展示秘钥 `$secret` 来应对二维码扫描不了的情况

### 后端生成二维码

[](#后端生成二维码)

```
$data = $authenticator->getQrImageUri($name, $secret, $options);
```

`$data`为生成好的二维码URI，前端直接``就可以了

> 推荐使用前端生成二维码

验证
--

[](#验证)

```
/**
 * 验证码 (必填)
 * 由用户输入，用于验证
 */
$captcha = '123456';

/**
 * 秘钥 (必填)
 * 一般从数据库中获取
 */
$secret = '';

/**
 * 允许向前和向后偏移的时间窗口 (可选)
 * 默认为1
 * 必须为0或正整数
 */
$window = 1;

$authenticator->validate($captcha, $secret, $window);
```

> 验证器每`30`秒为一个窗口，如果`$window`为`0`, 则在窗口交替的时候会有验证不通过的问题 比如当前窗口的验证码为`1`，前一窗口的验证码为`2`，后一窗口的验证码为`3`则在`$window`为`0`时，只有`1`可以通过验证 而在`$window`为`1`时，`1`，`2`，`3`都可以通过验证 以此类推

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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 ~64 days

Recently: every ~107 days

Total

8

Last Release

1847d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/606eaa4bc61b11fa19f25526a3084758b7086ceb8b11cdbcafd5df11871f2437?d=identicon)[Verdient。](/maintainers/Verdient%E3%80%82)

---

Top Contributors

[![Verdient](https://avatars.githubusercontent.com/u/22468867?v=4)](https://github.com/Verdient "Verdient (12 commits)")

---

Tags

googleauthenticator

### Embed Badge

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

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

###  Alternatives

[google/auth

Google Auth Library for PHP

1.4k272.7M162](/packages/google-auth)[hwi/oauth-bundle

Support for authenticating users using both OAuth1.0a and OAuth2 in Symfony.

2.4k21.5M69](/packages/hwi-oauth-bundle)[hybridauth/hybridauth

PHP Social Authentication Library

3.4k8.5M94](/packages/hybridauth-hybridauth)[robthree/twofactorauth

Two Factor Authentication

1.2k10.5M66](/packages/robthree-twofactorauth)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

42121.2M118](/packages/league-oauth2-google)[kreait/firebase-tokens

A library to work with Firebase tokens

24040.8M14](/packages/kreait-firebase-tokens)

PHPackages © 2026

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