haikiri / simple-php-totp
Lightweight PHP TOTP (RFC 6238) and Base32 (RFC 4648) generation/decoding for 2FA authentication
1.2.1
2026-02-14 20:53 UTC
Requires
- php: ^7.0 || >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.6 || ^8.5 || ^6.5
Conflicts
- doctrine/instantiator: >=2.0
README
Language: English | Русский
A lightweight and vanilla PHP library for generating TOTP (2FA) codes.
Compatibility
- PHP 7+ && PHP 8+
- Tests:
phpunit/phpunit(^9.6 || ^8.5 || ^6.5)
Installation
composer require haikiri/simple-php-totp
Usage example
<?php require __DIR__ . '/vendor/autoload.php'; use Haikiri\SimplePhpTotp\TOTP; # Generate a secret for storing in the DB. $secret = TOTP::generateSecret(); # Generate a 6-digit OTP code based on the secret. $code = TOTP::generate($secret); echo $secret . PHP_EOL; # Secret for the user. (needs be stored in the user's DB) echo $code . PHP_EOL; # TOTP-code with which we should compare the code provided by the user from the 2FA-app.
Get OTPAuth URL
<?php use Haikiri\SimplePhpTotp\TOTP; $url = TOTP::getTotpUrl( 'GJQTCOBSMI2TGZTD', # OTP-secret (Base32) 'user@example.com', # Username / client identifier 'My Service' # Issuer / organization name ); echo $url . PHP_EOL; # otpauth://totp/My%20Service:user%40example.com?secret=GJQTCOBSMI2TGZTD&issuer=My%20Service