MMOframework
Why AI Playground Libraries Developers Agency
FR EN
Build a project live
Index

Totp

MMO\oauth\helpers · 1 symbol · generated 30 Nov 2026

class

Totp

Totp.php:15

Time-based one-time passwords (RFC 6238, over RFC 4226's HOTP): the 6 digits an authenticator app (Google Authenticator, FreeOTP, a password manager) shows for a secret, changing every 30 seconds. HMAC-SHA1, as every app reads an otpauth:// link without algorithm. No dependency: a secret is base32, as the apps take it

final class MMO\oauth\helpers\Totp
Namespace
MMO\oauth\helpers
$secret = Totp::Secret(); // 'JBSWY3DPEHPK3PXP...' Totp::Verify($secret, $_POST['code'], $user->lastStep); // the step it matched, null for a wrong code

Index

ALPHABET
DIGITS
PERIOD
Base32Decode($text) static
Base32Encode($data) static
Code($secret, $step) static
Secret($bytes) static
Step($time) static
Uri($secret, $issuer, $account) static
Verify($secret, $code, $lastStep, $window, $time) staticCheck a code typed: the one of the current step, or of the step before or after (a clock a little off, a code typed as it changed), and of a step after $lastStep only, so that a code seen once open…

Constants3

ALPHABET

const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'

DIGITS

const DIGITS = 6

PERIOD

const PERIOD = 30

Methods7

Base32Decode($text)

staticTotp.php:86
static function Base32Decode(string $text): string
$textstring
returnsstringthe bytes of a base32 string, case, spaces and padding ignored
throwsfor a character out of base32

Base32Encode($data)

staticTotp.php:74
static function Base32Encode(string $data): string
$datastring
returnsstring$data in base32 (RFC 4648), without padding

Code($secret, $step)

staticTotp.php:35
static function Code(string $secret, int $step): string
$secretstringbase32, as Secret makes it (spaces and case ignored)
$stepint
returnsstringthe code of $secret at $step, DIGITS long
Totp::Code('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', 1); // '287082': RFC 6238's vector at 59 s

Secret($bytes)

staticTotp.php:21
static function Secret(int $bytes = 20): string
$bytes = 20int
returnsstringa new random secret of $bytes (160 bits by default, RFC 4226's advice), base32

Step($time)

staticTotp.php:26
static function Step(?int $time = null): int
$time = nullnull | int
returnsintthe step of $time (unix seconds): how many PERIOD since 1970

Uri($secret, $issuer, $account)

staticTotp.php:68
static function Uri(string $secret, string $issuer, string $account): string
$secretstring
$issuerstringthe site, as the app titles the entry
$accountstringthe account, as the app shows it under the title
returnsstringthe otpauth:// link an app reads from a QR code (Google Authenticator's Key Uri Format)
Totp::Uri($secret, 'Oviepro', 'ada@example.com'); // 'otpauth://totp/Oviepro:ada%40example.com?secret=...&issuer=Oviepro'

Verify($secret, $code, $lastStep, $window, $time)

staticTotp.php:51

Check a code typed: the one of the current step, or of the step before or after (a clock a little off, a code typed as it changed), and of a step after $lastStep only, so that a code seen once opens nothing (replay)

static function Verify( string $secret, string $code, ?int $lastStep = null, int $window = 1, ?int $time = null ): ?int
$secretstring
$codestringwhat was typed: spaces ignored
$lastStep = nullnull | intthe step of the last code accepted for this secret
$window = 1inthow many steps before and after the current one are accepted
$time = nullnull | int
returnsnull | intthe step matched, to keep as the next $lastStep; null for a wrong, replayed or out of window code
Totp::Verify($secret, '287 082', null, 1, 59); // 1