PHP code example of ekojs / otp

1. Go to this page and download the library: Download ekojs/otp 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/ */

    

ekojs / otp example snippets



Ekojs\Otp\TOTP;

$ejsotp = TOTP::getInstance();
$ejsotp->createOTP();
$ejsotp->otp->setLabel("[email protected]");
$ejsotp->otp->setIssuer("My Service");

echo "The TOTP secret is: {$ejsotp->otp->getSecret()}".PHP_EOL;
echo "The current TOTP is: {$ejsotp->otp->now()}".PHP_EOL;
echo "The Uri is: {$ejsotp->otp->getProvisioningUri()}".PHP_EOL;

$ejsotp->createOTP(["secret" => "VE7RDW7LC45QHKVZI6SPHDQK254TKO7CPG6KHPQ4RYN4MGBBA6EAAHVYHRVAGO5LPF6XNDPAOLE3KYQHBBHPB62VFVNZURWRZUDER4A"]);
$ejsotp->otp->setLabel('[email protected]');
$ejsotp->otp->setIssuer("My Service");

echo 'Current TOTP: ' . $ejsotp->otp->now() . PHP_EOL;
var_dump($ejsotp->otp->verify("988942"));



use Ekojs\Otp\HOTP;

$ejshotp = HOTP::getInstance();
$ejshotp->createOTP(["counter" => 1000]);
$ejshotp->otp->setLabel("[email protected]");
$ejshotp->otp->setIssuer("My Service HOTP");

echo "The HOTP secret is: {$ejshotp->otp->getSecret()}".PHP_EOL;
echo "The current HOTP is: {$ejshotp->otp->at($ejshotp->otp->getCounter())}".PHP_EOL;
echo "The Uri is: {$ejshotp->otp->getProvisioningUri()}".PHP_EOL;

$ejshotp->createOTP([
    "secret" => "HZHL2VE2RWMT2KHDQCYCLPXJRJC7T63SZFNDTLEEEJISHLQS5Y6CRDTW4D7D3GA35VMSA32NAGLXEEFDSRT63E332JQOCTDAVK4HZHI",
    "counter" => 1000
]);
$ejshotp->otp->setLabel("[email protected]");
$ejshotp->otp->setIssuer("My Service HOTP");

echo 'Current OTP: ' . $ejshotp->otp->at(1001) . PHP_EOL;
var_dump($ejshotp->otp->verify("598162",1001));

echo "Hash secret : ". hash("md5","mysecret").PHP_EOL; // 06c219e5bc8378f3a8a3f83b4b7e4649
echo "Backup codes: ". implode(" ",$ejsotp->generateBackupCodes("mysecret")).PHP_EOL;

echo "Reverse Mnemonic: ".$ejsotp->reverseMnemonic("almost awkward just jungle daring keep penalty lecture deputy fossil muscle nasty").PHP_EOL; // 06c219e5bc8378f3a8a3f83b4b7e4649

$result = $ejsotp->generateQr();
echo "<img src='".$result->getDataUri()."' />";

$result = $ejsotp->generateQr("path_to_logo.png",true);
echo "<img src='".$result->getDataUri()."' />";

$result = $ejsotp->generateQr(null,true);
echo "<img src='".$result->getDataUri()."' />";

$result = $ejsotp->generateQr();
$result->saveToFile("path/qrcode-totp.png");


Ekojs\Otp\TOTP;

$ejsotp = TOTP::getInstance();
$ejsotp->createOTP(["secret" => "VZCKGWRLS7CINEYALENYPH5T442LJUAFGSNCBTBQEHMN5GSVGTJCD2B7NHCZFK5FZ3QHTQ66JYDMNUI2UBWZJAYHI62VYVHVUGTO6SQ"]);
$ejsotp->otp->setLabel('[email protected]');
$ejsotp->otp->setIssuer("My Service");

var_dump($ejsotp->otp->verify("input code from your Google Authenticator")); // if true the code is valid
echo 'Current OTP: ' . $ejsotp->otp->now() . PHP_EOL;