微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

phpqrcode和google身份验证器-不确定事物的格式以及如何使其正确验证

如何解决phpqrcode和google身份验证器-不确定事物的格式以及如何使其正确验证

我正在使用phpqrcode创建一个用于二维验证的qrcode。我一直将Google Authenticator应用程序用于我的2FA的所有需求。虽然我可以创建qrcode,但不确定确切的格式,因此无法正确验证。到目前为止,当我尝试对其进行扫描时,我会从应用程序中获取“无效条形码”。

生成qrcode时,我是否使用密码,URL或两者的组合?我想念一些愚蠢的东西,我确定这是因为我不知道在哪里以及如何使用params和otpauth:// url。

require $_SERVER['DOCUMENT_ROOT'].'/assets/PHPqrcode/PHPqrcode.PHP';

//get params
$secret = create2FASecret();
$name = 'somename';
$issuer = 'example.com';

//url encode,but not sure where or how I use this
$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'&issuer='.$issuer.'');

//create the qrcode,base64 it,output it
ob_start();
QRCode::png($urlencoded,null,QR_ECLEVEL_L,3,4);
$newpng = base64_encode( ob_get_contents() );
ob_end_clean();

$src = 'data: image/png; base64,'.$newpng;


//show secret created and the qrcode
echo 'This is the secret that was generated : '.$secret,'<br>';
echo '<img src="' . $src . '" />';

//create a secret
function create2FASecret($secretLength = 16)
{
    $validChars = array(
        'A','B','C','D','E','F','G','H',//  7
        'I','J','K','L','M','N','O','P',// 15
        'Q','R','S','T','U','V','W','X',// 23
        'Y','Z','2','3','4','5','6','7',// 31
        '='  // padding char
    );
    
    unset($validChars[32]);

    $secret = '';
    for ($i = 0; $i < $secretLength; $i++) {
        $secret .= $validChars[array_rand($validChars)];
    }
    return $secret;
}

解决方法

事实证明,只有$ name和$ issuer应该使用urlencode编码,并且可以正常工作。我还根据https://github.com/google/google-authenticator/wiki/Key-Uri-Format

更改了网址格式
$name = urlencode($name);
$issuer = urlencode($issuer);

//%3A is encoded colon
$url = 'otpauth://totp/'.$issuer.'%3A'.$name.'?secret='.$secret.'&issuer='.$issuer.'&algorithm=SHA1&digits=6&period=30';


//create the qrcode,base64 it,output it
ob_start();
QRCode::png($url,null,QR_ECLEVEL_L,3,4);
$newpng = base64_encode( ob_get_contents() );
ob_end_clean();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。