Skip to content
Jennifer Programming Language

totp API reference

Time-based one-time passwords (RFC 6238 TOTP over RFC 4226 HOTP) - the six-digit two-factor codes authenticator apps show. A shared secret (a base32 string, the same one an app stores) plus the current time yields a short numeric code that both sides compute independently. Built on hash.hmac (HMAC-SHA1 by default; SHA-256 / SHA-512 optional), encoding (base32 secrets), and time (the 30-second step); the dynamic-truncation step uses bytes + bitwise operators. Pure .j, runs on both binaries.

generate / verify read the clock; generateAt / verifyAt take an explicit Unix time (deterministic - use them in tests). verify accepts a +/-1-step clock-skew window. uri builds the otpauth:// provisioning string an authenticator app scans as a QR code.

Import with import "totp.j" as totp;. See the totp guide for prose and examples.

Contents

NameWhat it is
generateCompute the TOTP code for the current time.
generateAtCompute the TOTP code for an explicit Unix time (seconds).
generateSecretGenerate a fresh random shared secret with the RFC 6238-recommended length for SHA-1 TOTP (20 bytes / 160 b...
generateSecretNGenerate a fresh random shared secret of an explicit byte length, as a base32 string (unpadded, the authent...
hotpThe raw RFC 4226 HOTP: the six-digit HMAC-SHA1 code for an explicit counter, the counter-based building blo...
uriBuild the otpauth://totp/... provisioning URI an authenticator app scans (as a QR code) to enrol an account.
verifyVerify a code against the current time, allowing a +/-1-step skew.
verifyAtVerify a code against an explicit Unix time, allowing a +/-1-step skew (so a code from the previous or next...
verifyWindowVerify a code against the current time, allowing a window-step skew on each side of now.
verifyWindowAtVerify a code against an explicit Unix time, allowing a window-step skew on each side of now (so window...
Optionsstruct: TOTP parameters.
Algorithmenum: The HMAC digest algorithm behind a TOTP code: Sha1 (the default), Sha256, or Sha512.

Functions

generate

jennifer
totp.generate(secret as string, opts as Options)

Compute the TOTP code for the current time.

secret
the base32 shared secret
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns string
the zero-padded numeric code

generateAt

jennifer
totp.generateAt(secret as string, unixSeconds as int, opts as Options)

Compute the TOTP code for an explicit Unix time (seconds). Deterministic.

secret
the base32 shared secret
unixSeconds
the Unix time in seconds
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns string
the zero-padded numeric code

generateSecret

jennifer
totp.generateSecret()

Generate a fresh random shared secret with the RFC 6238-recommended length for SHA-1 TOTP (20 bytes / 160 bits), as an unpadded base32 string.

Returns string
the base32-encoded secret

generateSecretN

jennifer
totp.generateSecretN(nbytes as int)

Generate a fresh random shared secret of an explicit byte length, as a base32 string (unpadded, the authenticator format decodeSecret accepts). Randomness is crypto-grade (crypto.randBytes), so the secret is unguessable. Round-trips: the result works with generate / verify.

nbytes
the number of random bytes, at least 10 (RFC 6238 recommends >= 20 for SHA-1)
Returns string
the base32-encoded secret

hotp

jennifer
totp.hotp(secret as string, counter as int)

The raw RFC 4226 HOTP: the six-digit HMAC-SHA1 code for an explicit counter, the counter-based building block TOTP layers a clock over. Use it directly for HOTP (event-based OTP) or to pin against the RFC 4226 test vectors; TOTP itself is just this over unixSeconds // period.

secret
the base32 shared secret
counter
the RFC 4226 moving-factor counter (an unsigned 64-bit value, so >= 0)
Returns string
the six-digit zero-padded numeric code
Throws Error
kind "totp" on a negative counter or a bad secret

uri

jennifer
totp.uri(
    issuer as string,
    account as string,
    secret as string,
    opts as Options)

Build the otpauth://totp/... provisioning URI an authenticator app scans (as a QR code) to enrol an account. The label is issuer:account.

issuer
the service name (e.g. "ACME")
account
the account name (e.g. "jane@acme.example")
secret
the base32 shared secret
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns string
the otpauth provisioning URI
Throws Error
kind "totp" on a secret that is not valid base32 or too short, or on invalid digits / period

verify

jennifer
totp.verify(secret as string, code as string, opts as Options)

Verify a code against the current time, allowing a +/-1-step skew.

secret
the base32 shared secret
code
the code to check
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns bool
true if the code matches

verifyAt

jennifer
totp.verifyAt(
    secret as string,
    code as string,
    unixSeconds as int,
    opts as Options)

Verify a code against an explicit Unix time, allowing a +/-1-step skew (so a code from the previous or next window still passes). Deterministic.

secret
the base32 shared secret
code
the code to check
unixSeconds
the Unix time in seconds
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns bool
true if the code matches this step or an adjacent one

verifyWindow

jennifer
totp.verifyWindow(
    secret as string,
    code as string,
    window as int,
    opts as Options)

Verify a code against the current time, allowing a window-step skew on each side of now.

secret
the base32 shared secret
code
the code to check
window
the number of period-steps to accept on each side of now
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns bool
true if the code matches any step within the window

verifyWindowAt

jennifer
totp.verifyWindowAt(
    secret as string,
    code as string,
    unixSeconds as int,
    window as int,
    opts as Options)

Verify a code against an explicit Unix time, allowing a window-step skew on each side of now (so window steps of clock drift in either direction still pass). window 0 checks only the current step; window 1 is the +/-1-step default verifyAt uses. Deterministic.

secret
the base32 shared secret
code
the code to check
unixSeconds
the Unix time in seconds
window
the number of period-steps to accept on each side of now
opts
digits / periodSec / algorithm (zero-value = defaults)
Returns bool
true if the code matches any step within the window

Structs

Options

TOTP parameters. A zero-value struct (def o as totp.Options;) means the common defaults: 6 digits, a 30-second step, and HMAC-SHA1.

jennifer
def struct Options {
    digits as int,
    periodSec as int,
    algorithm as Algorithm
};
digits
the code length 6-8; 0 means 6 (anything else throws kind "totp")
periodSec
the time step in seconds; 0 means 30 (negative throws kind "totp")
algorithm
the HMAC digest: totp.Algorithm.Sha1 (the zero-value default), .Sha256, or .Sha512

Enums

Algorithm

The HMAC digest algorithm behind a TOTP code: Sha1 (the default), Sha256, or Sha512. Selected through Options.algorithm; the zero value is Sha1.