Documentation

Totp
in package

Time based one time passwords as authenticator apps expect them (RFC 6238): HMAC-SHA1, six digits, a new code every 30 seconds.

Table of Contents

PERIOD  = 30
ALPHABET  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
DIGITS  = 6
$issuer  : string
$label  : string
$secret  : string
__construct()  : mixed
at()  : string
The code of a point in time
create()  : self
generateSecret()  : string
A fresh secret of 160 bits, the length RFC 4226 recommends
getProvisioningUri()  : string
The uri an authenticator reads from a QR code
getSecret()  : string
now()  : string
verify()  : bool
Check a code, tolerating a clock that is a little off
base32Decode()  : string
base32Encode()  : string

Constants

PERIOD

public mixed PERIOD = 30

ALPHABET

private mixed ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"

DIGITS

private mixed DIGITS = 6

Properties

$issuer

private string $issuer

$label

private string $label

$secret

private string $secret

Methods

__construct()

public __construct(string $secret[, string $label = "" ][, string $issuer = "" ]) : mixed
Parameters
$secret : string

The shared secret, Base32 encoded

$label : string = ""

The account the authenticator shows, usually the email

$issuer : string = ""

The application the authenticator shows

Return values
mixed —

at()

The code of a point in time

public at(int $timestamp) : string
Parameters
$timestamp : int

Unix timestamp

Return values
string —

Six digits, with leading zeros

create()

public static create(string $secret[, string $label = "" ][, string $issuer = "" ]) : self
Parameters
$secret : string
$label : string = ""
$issuer : string = ""
Return values
self —

generateSecret()

A fresh secret of 160 bits, the length RFC 4226 recommends

public static generateSecret() : string
Return values
string —

32 Base32 characters

getProvisioningUri()

The uri an authenticator reads from a QR code

public getProvisioningUri() : string
Return values
string —

An otpauth:// uri carrying the secret, the label and the issuer

getSecret()

public getSecret() : string
Return values
string —

now()

public now() : string
Return values
string —

verify()

Check a code, tolerating a clock that is a little off

public verify(string $code[, int $drift = 1 ][, int|null $timestamp = null ]) : bool
Parameters
$code : string

The code from the authenticator

$drift : int = 1

Periods before and after the timestamp that are accepted as well

$timestamp : int|null = null

Unix timestamp, now when left out

Return values
bool —

Whether the code matched

base32Decode()

private static base32Decode(string $encoded) : string
Parameters
$encoded : string
Return values
string —

base32Encode()

private static base32Encode(string $bytes) : string
Parameters
$bytes : string
Return values
string —

Search results