Karta.Accounts.TOTP (Karta v1.0.0)

Time-based one-time passwords (RFC 6238, on HOTP, RFC 4226), as authenticator apps make them: HMAC-SHA-1, 6 digits, 30-second steps, a 20-byte secret shown as base32.

verify/3 accepts the code of the current step and of the step before and after (RFC 6238 §5.2 allows one step of clock drift each way), and never a step at or before after (the last step accepted for the user), so that a code cannot be used twice. Codes are compared in constant time.

Summary

Functions

The code for secret at time (Unix seconds). Options: digits (default 6), algorithm (:sha, :sha256 or :sha512; default :sha).

The secret as base32 without padding, as authenticator apps take it.

A new secret: 20 random bytes (RFC 4226 §4 recommends 160 bits).

The otpauth:// URI for an authenticator app (the format Google Authenticator defined and other apps follow): label issuer:account.

Checks a code at time (option time, default now): {:ok, step} with the step it belongs to, or :error. Option after: steps up to and including it are not accepted. Spaces in the code are ignored.

Functions

code(secret, time, opts \\ [])

@spec code(binary(), integer(), keyword()) :: String.t()

The code for secret at time (Unix seconds). Options: digits (default 6), algorithm (:sha, :sha256 or :sha512; default :sha).

encode(secret)

@spec encode(binary()) :: String.t()

The secret as base32 without padding, as authenticator apps take it.

new_secret()

@spec new_secret() :: binary()

A new secret: 20 random bytes (RFC 4226 §4 recommends 160 bits).

uri(secret, account, issuer)

@spec uri(binary(), String.t(), String.t()) :: String.t()

The otpauth:// URI for an authenticator app (the format Google Authenticator defined and other apps follow): label issuer:account.

verify(secret, code, opts \\ [])

@spec verify(binary(), String.t(), keyword()) :: {:ok, integer()} | :error

Checks a code at time (option time, default now): {:ok, step} with the step it belongs to, or :error. Option after: steps up to and including it are not accepted. Spaces in the code are ignored.