PHP code example of lorddashme / php-two-factor-auth

1. Go to this page and download the library: Download lorddashme/php-two-factor-auth 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/ */

    

lorddashme / php-two-factor-auth example snippets




ordDashMe\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



ordDashMe\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



ordDashMe\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



ordDashMe\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



ordDashMe\TwoFactorAuth\Utility\Base32;
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\BarcodeURL;
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\TOTPFormat;

$secret = Base32::encode('P@ssw0rd!');
$accountUser = '[email protected]';
$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:[email protected]?secret=KBAHG43XGBZGIII&algorithm=SHA1&digits=6&period=30&issuer=TwoFactorAuth