Search by

Native, ultra-fast, hardened security and strictly typed engine for 2FA Tokens (TOTP/HOTP) with 27 i18n languages in PHP 8.3+.

Package info

github.com/PamellaYamada/OTPHP

pkg:composer/pamellayamada/otphp

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.5 2026-08-02 03:45 UTC

This package is auto-updated.

Last update: 2026-09-02 16:38:52 UTC


README

Latest Version on Packagist GitHub Tests Action Status PHPStan Level 8 License: MIT PHP Version

Native, high-performance, zero-dependency, internationalized, and strictly typed OTP (TOTP/HOTP) authentication engine for PHP 8.3+.

⚑ Key Features

  • πŸš€ Zero Dependencies: Built entirely with native PHP features.
  • 🎯 Strictly Typed & PSR Compliant: PHPStan Level 8 static analysis and PSR-12/PER compliant.
  • 🌐 Internationalization (I18n): Native multi-language support for exception messages and responses.
  • πŸ“± Native QR Code: Renders clean SVG vector tags without external heavy image libraries (GD/Imagick).
  • βš™οΈ Multi-Provider Support: Pre-configured settings for Google Authenticator, Microsoft, Bitwarden, Steam Guard, YubiKey, Aegis, and more.

πŸ“¦ Installation

Install the package via Composer:

composer require pamellayamada/otphp

πŸš€ Usage

1. Generating a Base32 Secret

use PamellaYamada\OTPHP\OTPHP;

// Generate a secure Base32 secret key
$secret = OTPHP::createSecret(32); 

2. Generating an OTP Code (TOTP)

use PamellaYamada\OTPHP\OTPHP;
use PamellaYamada\OTPHP\Enums\OTPProvider;

// Generate the current 6-digit time-based code (Default: Google Authenticator)
$code = OTPHP::generate($secret);

// Or specify a target provider (e.g., Aegis with 8 digits)
$aegisCode = OTPHP::generate($secret, OTPProvider::AEGIS);

3. Verifying User Input

use PamellaYamada\OTPHP\OTPHP;

$userCode = '123456';

// Verify the code considering time drift (window parameter)
$isValid = OTPHP::verify($userCode,$secret, window: 1);

if ($isValid) {
    // Authentication successful
}

4. Rendering an SVG QR Code

use PamellaYamada\OTPHP\OTPHP;

// Returns a clean <svg> string ready to embed into HTML, Blade, or PHP views
$svgXml = OTPHP::renderQrCodeSvg(
    secret: $secret,
    accountName: 'user@email.com',
    issuer: 'MyApp',
    sizePixels: 200
);

echo $svgXml;

5. Changing the Locale (I18n)

use PamellaYamada\OTPHP\OTPHP;
use PamellaYamada\OTPHP\Enums\OTPLanguage;

// Set global locale for exception messages and output
OTPHP::setLocale(OTPLanguage::EN_US);

πŸ› οΈ Development & Quality Assurance

This repository includes Composer scripts to ensure code quality:

# Run code formatting (Pint), static analysis (PHPStan Level 8), and unit tests (PHPUnit)
composer check

# Run unit tests only
composer test

# Format code using Laravel Pint
composer format

# Run PHPStan analysis
composer analyse

πŸ“„ License

This project is open-sourced software licensed under the MIT License.