| 1 |
|
defmodule WalletAuth.Otp.OtpChallenge do |
| 2 |
|
@moduledoc """ |
| 3 |
|
OTP challenge generation logic. |
| 4 |
|
|
| 5 |
|
Generates a 6-digit numeric OTP code using cryptographically secure randomness. |
| 6 |
|
The code is unguessable without brute force, which is controlled by OtpStore's |
| 7 |
|
attempt limit. |
| 8 |
|
|
| 9 |
|
OTP codes are NOT stored here — only generated. The store (OtpStore) manages |
| 10 |
|
lifecycle state. |
| 11 |
|
""" |
| 12 |
|
|
| 13 |
|
@code_digits 6 |
| 14 |
|
|
| 15 |
|
@type code :: String.t() |
| 16 |
|
|
| 17 |
|
@doc """ |
| 18 |
|
Generates a 6-digit numeric OTP code. |
| 19 |
|
Returns a zero-padded string of exactly #{@code_digits} digits. |
| 20 |
|
""" |
| 21 |
|
@spec generate() :: code() |
| 22 |
|
def generate do |
| 23 |
31 |
max = trunc(:math.pow(10, @code_digits)) |
| 24 |
|
:crypto.strong_rand_bytes(4) |
| 25 |
|
|> :binary.decode_unsigned() |
| 26 |
|
|> rem(max) |
| 27 |
|
|> Integer.to_string() |
| 28 |
31 |
|> String.pad_leading(@code_digits, "0") |
| 29 |
|
end |
| 30 |
|
end |