RFC 4226 HMAC-based One-Time Password (HOTP) implementation in pure Elixir.
Summary
Functions
Generates an HOTP token string for the given secret and counter.
Validates an HOTP token against secret and counter, returning
{:ok, :pass} or {:error, :invalid_token}.
Validates an HOTP token against secret starting at counter.
Types
@type option() :: {:token_length, pos_integer()} | {:digits, pos_integer()} | {:algorithm, :sha | :sha1 | :sha256 | :sha512} | {:digest_method, :sha | :sha1 | :sha256 | :sha512} | {:secret_encoding, :base32 | :raw} | {:window, non_neg_integer()}
Functions
@spec generate(binary(), non_neg_integer(), [option()]) :: binary()
Generates an HOTP token string for the given secret and counter.
Options
:token_length(alias:digits) - number of digits in the generated token (default:6):algorithm(alias:digest_method) - HMAC hash algorithm::sha(:sha1),:sha256, or:sha512(default::sha):secret_encoding-:base32(default) or:raw
Examples
iex> Ex2FA.HOTP.generate("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", 0)
"755224"
@spec validate(term(), term(), term(), [option()]) :: {:ok, :pass} | {:error, :invalid_token} | {:error, binary()}
Validates an HOTP token against secret and counter, returning
{:ok, :pass} or {:error, :invalid_token}.
@spec verify(binary(), binary(), non_neg_integer(), [option()]) :: {:ok, non_neg_integer()} | {:error, :invalid_token}
Validates an HOTP token against secret starting at counter.
Supports look-ahead resynchronization via the :window option (default: 0),
which checks counters from counter through counter + window.
Returns {:ok, matched_counter} when valid, or {:error, :invalid_token} otherwise.