PHP code example of vjolenz / php-otp

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

    

vjolenz / php-otp example snippets


    $user = User::find(1);
    
    $authenticator = new \vjolenz\OtpAuth\HotpAuthenticator();
    $authenticator->setSecret('12345678901234567890'); // Default: null
    $authenticator->setAlgorithm('SHA256'); // Default: SHA1
    $authenticator->setWindowSize(3); // Default: 1
    $authenticator->setPasswordLength(9); // Default: 6
    
    $password = $authenticator->generatePassword($user->getLoginCounter());
    
    $user->advanceLoginCounter();

    $user = User::find(1);
    
    $authenticator = new \vjolenz\OtpAuth\HotpAuthenticator();
    $authenticator->setSecret('12345678901234567890'); // Default: null
    $authenticator->setAlgorithm('SHA256'); // Default: SHA1
    $authenticator->setWindowSize(3); // Default: 1
    $authenticator->setPasswordLength(9); // Default: 6
    
    $authenticator->verifyPassword($password, $user->getLoginCounter());

    $authenticator = new \vjolenz\OtpAuth\TotpAuthenticator();
    $authenticator->setSecret('12345678901234567890'); // Default: null
    $authenticator->setAlgorithm('SHA256'); // Default: SHA1
    $authenticator->setWindowSize(3); // Default: 1
    $authenticator->setPasswordLength(9); // Default: 6
    $authenticator->setInterval(60); // Default: 30
    
    $password = $authenticator->generatePassword();

    $authenticator = new \vjolenz\OtpAuth\TotpAuthenticator();
    $authenticator->setSecret('12345678901234567890'); // Default: null
    $authenticator->setAlgorithm('SHA256'); // Default: SHA1
    $authenticator->setWindowSize(3); // Default: 1
    $authenticator->setPasswordLength(9); // Default: 6
    $authenticator->setInterval(60); // Default: 30
    
    $authenticator->verifyPassword($password);
 bash
$ composer