# `Ex2FA.TOTP`
[🔗](https://github.com/Ultraluv/ex_2FA/blob/v0.1.0/lib/ex_2fa/totp.ex#L1)

RFC 6238 Time-based One-Time Password (TOTP) implementation in pure Elixir.

Supports configurable time intervals, token lengths, hash algorithms
(`:sha`, `:sha256`, `:sha512`), clock-skew windows, and replay protection
via `:since` / `:last_used_at` / `:last_step`.

# `option`

```elixir
@type option() ::
  {:token_length, pos_integer()}
  | {:digits, pos_integer()}
  | {:interval_length, pos_integer()}
  | {:period, pos_integer()}
  | {:window, non_neg_integer()}
  | {:addwindow, integer()}
  | {:algorithm, :sha | :sha1 | :sha256 | :sha512}
  | {:digest_method, :sha | :sha1 | :sha256 | :sha512}
  | {:secret_encoding, :base32 | :raw}
  | {:time, time_input()}
  | {:timestamp, time_input()}
  | {:since, time_input() | nil}
  | {:last_used_at, time_input() | nil}
  | {:last_step, integer() | nil}
```

# `time_input`

```elixir
@type time_input() ::
  integer()
  | DateTime.t()
  | NaiveDateTime.t()
  | {non_neg_integer(), non_neg_integer(), non_neg_integer()}
```

# `generate`

```elixir
@spec generate(binary(), [option()]) :: binary()
```

Generates a TOTP token string for the given `secret`.

## Options

  * `:token_length` (alias `:digits`) - number of digits (default: `6`)
  * `:interval_length` (alias `:period`) - time step duration in seconds (default: `30`)
  * `:algorithm` (alias `:digest_method`) - `:sha` (`:sha1`), `:sha256`, or `:sha512` (default: `:sha`)
  * `:time` (alias `:timestamp`) - Unix seconds, `DateTime`, or Erlang `{megasecs, secs, microsecs}` tuple (default: current time)
  * `:addwindow` - integer offset of time steps added to the current step (default: `0`)
  * `:secret_encoding` - `:base32` (default) or `:raw`

# `time_step`

```elixir
@spec time_step(time_input(), pos_integer()) :: non_neg_integer()
```

Returns the current TOTP time step for the given timestamp and interval length.

# `validate`

```elixir
@spec validate(term(), term(), [option()]) ::
  {:ok, :pass} | {:error, :invalid_token} | {:error, binary()}
```

Validates a TOTP `token` against `secret`, returning `{:ok, :pass}` or
`{:error, :invalid_token}` (or `{:error, "Both secret and token must be strings"}`
for non-binary inputs, matching `nio_google_authenticator`).

# `verify`

```elixir
@spec verify(binary(), binary(), [option()]) ::
  {:ok, non_neg_integer()} | {:error, :invalid_token}
```

Verifies a TOTP `token` against `secret`, returning `{:ok, matched_step}` on
success or `{:error, :invalid_token}` on failure.

The returned `matched_step` (or the current timestamp) can be stored and later
passed as `:last_step` (or `:since` / `:last_used_at`) to prevent token replay
within the same or earlier time windows.

## Replay Protection Options

  * `:since` (alias `:last_used_at`) - timestamp (Unix seconds, `DateTime`, or Erlang timestamp tuple) of the last successful authentication. Any token belonging to a time step `<= div(since, interval_length)` is rejected.
  * `:last_step` - exact last-accepted TOTP step integer. Any token belonging to a time step `<= last_step` is rejected.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
