veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

auth/totp

lib/auth/totp.vk

lib/auth/totp.vk Pure-Veko RFC 4648 Base32 codec + RFC 4226 HOTP & RFC 6238 TOTP (SHA-1 / SHA-256) two-factor authentication helpers + otpauth:// provisioning URI builder. Pure functions require zero capabilities; totp-now / verify-now require .clocks.

signature
b32-encode = fn [data bytes] str

Encodes data into RFC 4648 Base32 (without '=' padding).

Executable Example
veko
use [totp = auth/totp]
e = totp/b32-encode (bytes/from-str 'foobar')
if e != 'MZXW6YTBOI' or (bytes/to-str (totp/b32-decode e)) != 'foobar' [ err 'base32' ] else [ .ok ]
signature
b32-decode = fn [s str] bytes

Decodes RFC 4648 Base32 (case-insensitive, ignores spaces/-/=).

hotp fn
signature
hotp = fn [secret bytes, counter i64, alg enum, digits i64] str

Computes an RFC 4226 HOTP code (alg = .sha1 or .sha256, digits typically 6 or 8).

Executable Example
veko
use [totp = auth/totp]
// RFC 4226 Appendix D test vector (secret = "12345678901234567890", counter = 0 -> "755224")
code = totp/hotp (bytes/from-str '12345678901234567890') 0 .sha1 6
if code != '755224' [ err 'rfc4226 vector' ] else [ .ok ]
signature
totp-at = fn [secret bytes, unix_sec i64, period i64, alg enum, digits i64] str

Computes an RFC 6238 TOTP code at Unix timestamp unix_sec with period (typically 30).

Executable Example
veko
use [totp = auth/totp]
// RFC 6238 Table 1 SHA-256 vector (t = 59s, period = 30, 8 digits -> "46119246")
sec = bytes/from-str '12345678901234567890123456789012'
if (totp/totp-at sec 59 30 .sha256 8) != '46119246' [ err 'rfc6238 vector' ] else [ .ok ]
totp-now fn caps: .clocks
signature
totp-now = fn [secret bytes] str

Computes the current 6-digit SHA-1 TOTP code (30s period) using live time/unix.

signature
verify-at = fn [secret bytes, code str, unix_sec i64, period i64, alg enum, digits i64, skew_steps i64] bool

Verifies a TOTP code at unix_sec within +/- skew_steps periods using constant-time comparison.

verify-now fn caps: .clocks
signature
verify-now = fn [secret bytes, code str] bool

Verifies a 6-digit SHA-1 TOTP code against current UTC time allowing +/- 1 time step (30s).

signature
otpauth-uri = fn [issuer str, account str, secret bytes, alg enum, digits i64, period i64] str

Builds an otpauth://totp/... provisioning URI for authenticator apps.