BoxTurtle.Security.Password (BoxTurtle v0.1.0)

Copy Markdown View Source

Generates random passwords containing at least one lowercase letter, one uppercase letter, one digit and one special character.

All randomness comes from :crypto.strong_rand_bytes/1 (through :crypto.rand_seed_s/0), so the passwords are suitable for credentials. The caller's :rand seed is not touched.

alias BoxTurtle.Security.Password

{:ok, password} = Password.generate(16)
String.length(password)  #=> 16
{:error, :too_short} = Password.generate(4)

# e.g. a temporary password for an account created by an admin
def create_invited_user(attrs) do
  {:ok, password} = Password.generate(20)
  attrs |> Map.put(:password, password) |> MyApp.Accounts.register_user()
end

The approach is inspired by the Rosetta Code Password generator task.

Summary

Functions

Generates a random password of length characters (default 8).

Returns the minimum password length accepted by generate/1 (8).

Functions

generate(length \\ 8)

@spec generate(integer()) :: {:ok, String.t()} | {:error, :too_short}

Generates a random password of length characters (default 8).

Returns {:ok, password}, or {:error, :too_short} when length is not an integer of at least 8.

min_length()

@spec min_length() :: pos_integer()

Returns the minimum password length accepted by generate/1 (8).