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

RFC 4226 HMAC-based One-Time Password (HOTP) implementation in pure Elixir.

# `option`

```elixir
@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()}
```

# `generate`

```elixir
@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"

# `validate`

```elixir
@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}`.

# `verify`

```elixir
@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.

---

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