lorddashme / php-two-factor-auth
一个用于RFC 4226和RFC 6238的PHP库。
1.0.2
2020-05-19 06:04 UTC
Requires
- php: >=5.3 || >=5.4 || >=5.6 || >=7.0 || >=7.1 || >=7.2
Requires (Dev)
- mockery/mockery: 1.*
- phpunit/phpunit: 5.* || 6.* || 7.*
This package is auto-updated.
Last update: 2024-09-30 01:48:53 UTC
README
一个用于RFC 4226和RFC 6238的PHP库。
需求
- PHP版本从5.6.*到最新版本。
安装
- 建议使用Composer安装。使用以下命令安装包
composer require lorddashme/php-two-factor-auth
使用方法
HOTP
- 用于生成基于HMAC的一次性密码算法
<?php require __DIR__ . '/vendor/autoload.php'; use LordDashMe\TwoFactorAuth\RFC4226\HOTP; use LordDashMe\TwoFactorAuth\Utility\Base32; $secret = Base32::encode('P@ssw0rd!'); $hotp = new HOTP($secret); $hotp->setLength(6) ->setAlgorithm('sha1') ->prepare() ->generate(); echo $hotp->get(); // 444555
- 用于验证生成的HOTP
<?php require __DIR__ . '/vendor/autoload.php'; use LordDashMe\TwoFactorAuth\RFC4226\HOTP; use LordDashMe\TwoFactorAuth\Utility\Base32; $secret = Base32::encode('P@ssw0rd!'); $hotp = new HOTP($secret); $hotp->setLength(6) ->setAlgorithm('sha1') ->prepare(); echo $hotp->verify('444555'); // true
TOTP
- 用于生成基于时间的一次性密码算法
<?php require __DIR__ . '/vendor/autoload.php'; use LordDashMe\TwoFactorAuth\RFC6238\TOTP; use LordDashMe\TwoFactorAuth\Utility\Base32; $secret = Base32::encode('P@ssw0rd!'); $totp = new TOTP($secret); $totp->setTimeZone('Asia/Manila') ->setTimeRemainingInSeconds(30) ->setTimeAdjustments(10) ->setLength(6) ->setAlgorithm('sha1') ->prepare() ->generate(); echo $totp->get(); // 552344
- 用于验证生成的TOTP
<?php require __DIR__ . '/vendor/autoload.php'; use LordDashMe\TwoFactorAuth\RFC6238\TOTP; use LordDashMe\TwoFactorAuth\Utility\Base32; $secret = Base32::encode('P@ssw0rd!'); $totp = new TOTP($secret); $totp->setTimeZone('Asia/Manila') ->setTimeRemainingInSeconds(30) ->setTimeAdjustments(10) ->setLength(6) ->setAlgorithm('sha1') ->prepare(); echo $totp->verify('552344'); // true
Google Authenticator条码生成
- 用于生成将用于Google Authenticator移动应用的条码图像
<?php require __DIR__ . '/vendor/autoload.php'; use LordDashMe\TwoFactorAuth\Utility\Base32; use LordDashMe\TwoFactorAuth\GoogleAuthenticator\BarcodeURL; use LordDashMe\TwoFactorAuth\GoogleAuthenticator\TOTPFormat; $secret = Base32::encode('P@ssw0rd!'); $accountUser = 'reyesjoshuaclifford@gmail.com'; $issuer = 'TwoFactorAuth'; $digits = 6; $period = 30; $algorithm = 'sha1'; $format = new TOTPFormat($period); $barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $format); $barcodeURL->setAlgorithm($algorithm) // sha1 (Default), sha256, sha512 ->setDigits($digits) ->build(); echo $barcodeURL->get(); // https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/TwoFactorAuth:reyesjoshuaclifford@gmail.com?secret=KBAHG43XGBZGIII&algorithm=SHA1&digits=6&period=30&issuer=TwoFactorAuth
其他参考
许可证
本软件包是开源软件,许可协议为MIT许可证。