PHP code example of nocksapp / google2fa

1. Go to this page and download the library: Download nocksapp/google2fa library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nocksapp / google2fa example snippets


use PragmaRX\Google2FA\Google2FA;
    
$google2fa = new Google2FA();
    
return $google2fa->generateSecretKey();

$user->google2fa_secret = $google2fa->generateSecretKey();

$google2fa_url = $google2fa->getQRCodeGoogleUrl(
    'YourCompany',
    $user->email,
    $user->google2fa_secret
);

/// and in your view:

<img src="{{ $google2fa_url }}" alt="">

$secret = $request->input('secret');

$valid = $google2fa->verifyKey($user->google2fa_secret, $secret);

    $google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey)

<div class="visible-print text-center">
    {!! QrCode::size(100)->generate($google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey)); !!}
    <p>Scan me to return to the original page.</p>
</div>

$secret = $request->input('secret');

$window = 8; // 8 keys (respectively 4 minutes) past and future

$valid = $google2fa->verifyKey($user->google2fa_secret, $secret, $window);

$secret = $request->input('secret');

$timestamp = $google2fa->verifyKeyNewer($user->google2fa_secret, $secret, $user->google2fa_ts);

if ($timestamp !== false) {
    $user->update(['google2fa_ts' => $timestamp]);
    // successful
} else {
    // failed
}

$secretKey = $google2fa->generateSecretKey(32); // defaults to 16 bytes

$prefix = strpad($userId, 10, 'X');

$secretKey = $google2fa->generateSecretKey(16, $prefix);

$secretKey = $google2fa->setWindow(4);

$isValid = $google2fa->verifyKey($seed, $key, 4);

$google2fa->setKeyRegeneration(40);

$inlineUrl = $google2fa->getQRCodeInline(
    $companyName,
    $companyEmail,
    $secretKey
);

$secretKey = $google2fa->generateSecretKey(16, $userId);

$secretKey = '123456789';
  
$secretKey = str_pad($secretKey, pow(2,ceil(log(strlen($secretKey),2))), 'X');

$google2fa->setEnforceGoogleAuthenticatorCompatibility(false);