totp

RFC 4226 HOTP & RFC 6238 TOTP 2FA generator with Base32 and otpauth URIs.

v1.0.0Security & encodingbundledimport "totp";
Overview Installation Documentation Examples

Overview

Zero-dependency RFC 4226 HOTP and RFC 6238 TOTP two-factor authentication (2FA) library for HolyPHP, featuring Base32 encoding/decoding, clock-drift window verification, and otpauth:// URI generation.

bundled — works offline zero dependencies hotp otp

Quick start

import "totp";

$secret = "JBSWY3DPEHPK3PXP";

// Generate a 6-digit TOTP code for the current 30-second window
$code = totp_generate($secret);
println("Current TOTP: " . $code);

// Verify a user-supplied code (allowing ±1 time step of clock drift)
if (totp_verify($secret, $code)) {
    println("Code is valid!");
}

// Build an otpauth:// URI for QR code provisioning
$uri = totp_uri($secret, "alice@example.com", "MyApp");
println("Provisioning URI: " . $uri);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install totp

This adds the library to hphp.yaml, installs it into .hphp/packages/totp/ and pins the exact version in hphp.lock:

require:
  totp: "^1.0"

Use it

import "totp";

totp is bundled with the compiler, so import "totp"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install totp          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install totp
Packagetotp
Version1.0.0 (follows the compiler)
Entry filetotp.hphp
Sourcepackages/lib/totp ↗

Documentation

API Reference

hotp_generate(string $secret, int $counter, int $digits = 6): string

Generates an RFC 4226 HMAC-SHA1 counter-based one-time password.

totp_generate(string $secret, $timestamp = null, int $period = 30, int $digits = 6): string

Generates an RFC 6238 time-based one-time password.

totp_verify(string $secret, string $code, $timestamp = null, int $period = 30, int $digits = 6, int $window = 1): bool

Verifies a TOTP code using constant-time comparison across [counter - window, counter + window].

totp_uri(string $secret, string $account, string $issuer = "", int $period = 30, int $digits = 6): string

Builds a standards-compliant otpauth://totp/... provisioning URI suitable for QR codes (Google Authenticator, Authy, 1Password, etc.).

totp_base32_decode(string $secret): array

Decodes an RFC 4648 Base32 string into an array of byte integers (0–255). Ignores whitespace, hyphens, and trailing = padding, and is case-insensitive.

totp_base32_encode(string $data): string

Encodes a raw byte string into an uppercase unpadded Base32 string.

Error Handling

All functions validate their inputs and throw Exception with clear, actionable messages prefixed with totp::

try {
    totp_generate("INVALID189");
} catch (Exception $e) {
    println("Error: " . $e->getMessage());
}

Examples

01Quick start

Bundled with the compiler — import "totp"; works offline in any file, or add it to a project with hphp install totp.

import "totp";

$secret = "JBSWY3DPEHPK3PXP";

// Generate a 6-digit TOTP code for the current 30-second window
$code = totp_generate($secret);
println("Current TOTP: " . $code);

// Verify a user-supplied code (allowing ±1 time step of clock drift)
if (totp_verify($secret, $code)) {
    println("Code is valid!");
}

// Build an otpauth:// URI for QR code provisioning
$uri = totp_uri($secret, "alice@example.com", "MyApp");
println("Provisioning URI: " . $uri);
Try it
hphp install totp
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_totp.hphp.