当前位置:首页 > 开发 > 正文内容

Google Auth

hackcode2023年10月09日 03:35开发460
<?php
require_once 'GoogleAuthenticator.php';

$ga = new \PHPGangsta_GoogleAuthenticator();
// 1.创建秘钥
$secret = $ga->createSecret();
//echo "Secret is: ".$secret."\n\n";
echo "密匙" . $secret . "<br>";

// 2. 生成秘钥对应的二维码
$qrCodeUrl = $ga->getQRCodeGoogleUrl('Blog', $secret);
//echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."<br>";
echo "二维码 url  " . $qrCodeUrl . "<br>";

// 3.生成秘钥对应的动态码
$oneCode = $ga->getCode($secret);

//echo "Checking Code '$oneCode' and Secret '$secret'<br>";
echo "被检查的动态码: '$oneCode' and 密码是  '$secret'<br>";

// 4.检查传递的秘钥与动态码是否匹配。最后一个参数应该是时间。值是30秒的倍数。
// 但是谷歌验证器 app  的动态码好像是30s 变化一次,这个我没注意
$checkResult = $ga->verifyCode($secret, $oneCode, 2);    // 2 = 2*30sec clock tolerance
if ($checkResult) {
    echo 'OK';
} else {
    echo 'FAILED';
}

GoogleAuthenticator.zip