Skip to content

v1.0.0

Choose a tag to compare

@zxc7563598 zxc7563598 released this 21 Apr 11:54
· 13 commits to main since this release

主要功能

  • Google Authenticator 集成:通过简单的方法生成和验证 Google Authenticator 的 TOTP 验证码。
  • 密钥生成:自动生成符合 TOTP 标准的 16 字节 Base32 编码的密钥,并支持生成用于 QR 码的 URL。
  • 二维码生成:支持通过生成的密钥生成 Google Authenticator 扫描的二维码。

更新内容

初次发布,提供 Google Authenticator 所需的基本功能,包括密钥生成、验证码验证、二维码生成等。
简化了 Google Authenticator 集成过程,只需少量代码即可实现。

安装方法

通过 composer 安装

composer require hejunjie/google-authenticator

使用方法

  1. 生成密钥和二维码
use Hejunjie\GoogleAuthenticator\GoogleAuthenticator;

$secret = GoogleAuthenticator::generateSecret();  // 生成密钥
echo "Secret: " . $secret . PHP_EOL;

$issuer = '应用名'; // Google Authenticator 中的显示的名称为「issuer: label」
$label = '用户名'; // Google Authenticator 中的显示的名称为「issuer: label」
$secret = '用户 TOTP 密钥'; // 可自传或者使用 GoogleAuthenticator::generateSecret() 生成的密钥
$path = '/www/wwwroot/xxxxx.png'; // 二维码文件存储路径(带名字)
$width = 300; // [非必传]二维码图片宽度,默认300
$logo = '/www/wwwroot/xxxxx.png'; // [非必传]logo文件路径(如果不需要logo给空字符串即可),默认空字符串
$logo_width = 50; // [非必传]二维码图片宽度如果logo为空则无效),默认50

$qrCodeFile = GoogleAuthenticator::getQRCodeFile($issuer, $label, $secret, $path, $width, $logo, $logo_width);
echo "QR Code File Path: " . $qrCodeFile . PHP_EOL;
  1. 验证验证码
$code = '123456';  // 用户输入的验证码
$isValid = GoogleAuthenticator::checkCode($secret, $code);  // 验证验证码
echo $isValid ? '验证码有效' : '验证码无效';