pamellayamada / otphp
Native, ultra-fast, hardened security and strictly typed engine for 2FA Tokens (TOTP/HOTP) with 27 i18n languages in PHP 8.3+.
1.0.5
2026-08-02 03:45 UTC
Requires
- php: ^8.3
- ext-hash: *
- ext-json: *
- ext-mbstring: *
- ext-sodium: *
Requires (Dev)
- laravel/pint: ^1.16
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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.