phpqrcode和google authenticator-不确定内容的格式以及如何使其正确验证

phpqrcode和google authenticator-不确定内容的格式以及如何使其正确验证,php,qr-code,two-factor-authentication,google-2fa,Php,Qr Code,Two Factor Authentication,Google 2fa,我正在使用创建用于双因素身份验证的qrcode。我一直使用Google Authenticator应用程序满足我的所有2FA需求。虽然我可以创建qrcode,但我不确定确切的格式,因此它可以正确验证。现在,当我尝试扫描它时,我从应用程序中得到了“无效条形码” 生成qrcode时,我是否使用机密、url或两者的组合?我遗漏了一些愚蠢的东西,我确信这是因为我不知道在哪里以及如何使用params和otpauth://url require $_SERVER['DOCUMENT_ROOT'].'/ass

我正在使用创建用于双因素身份验证的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;
}
require$_SERVER['DOCUMENT_ROOT']./assets/phpqrcode/phpqrcode.php';
//获取参数
$secret=create2FASecret();
$name='somename';
$issuer='example.com';
//url编码,但不确定在何处或如何使用
$urlencoded=urlencode('otpauth://totp/“.$name.”secret='.$secret.&issuer='.$issuer.');
//创建qrcode,base64它,输出它
ob_start();
QRCode::png($urlencoded,null,QR\u ECLEVEL\u L,3,4);
$newpng=base64_编码(ob_get_contents());
ob_end_clean();
$src='数据:图像/png;base64'。$newpng;
//显示创建的密码和qrcode
echo“这是生成的秘密:”.$secret,
”; 回声'; //制造秘密 函数create2FASecret($secretLength=16) { $validChars=数组( “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 '='//填充字符 ); 未结算($validChars[32]); $secret=''; 对于($i=0;$i<$secretLength;$i++){ $secret.=$validChars[array_rand($validChars)]; } 返回$secret; }
事实证明,只有$name和$issuer应该是URL编码的,并且它可以正常工作。我还根据需要更改了url的格式

$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();