vjolenz/php-otp

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (v1.0.0) of this package.

PHP OTP (One Time Password) implementation

v1.0.0 2017-12-20 21:03 UTC

This package is auto-updated.

Last update: 2023-07-29 01:36:32 UTC


README

Build Status StyleCI

A PHP library to generate and verify one-time passwords. It is compatible with HOTP and TOTP.

Prerequisites

This library needs at least PHP 7.0.

Installation

You can install via composer.

$ composer require vjolenz/php-otp

HOTP Usage

Generation and verification requires a moving factor that changes on per use. You can use a login counter as moving factor.

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

TOTP Usage

Unlike HOTP generation and verification, you don't need a moving factor since the current timestamp is used for these operations

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

License

The MIT License (MIT). Please see License File for more information.