Current section

Files

Jump to
blossom lib blossom passwords.ex
Raw

lib/blossom/passwords.ex

defmodule Blossom.Passwords do
alias Blossom.Passwords
def hash(string) do
Bcrypt.Base.hash_password(string, Bcrypt.Base.gen_salt())
end
def validate_length(string, opts) do
if String.length(string) >= opts[:min_password_length] do
{:ok, nil}
else
{:error, :short_password}
end
end
def validate(string, opts) do
[
Passwords.validate_length(string, opts)
]
|> Enum.reduce_while({:ok, string}, fn outcome, acc ->
case outcome do
{:error, _e} -> {:halt, outcome}
_ -> {:cont, acc}
end
end)
end
def matches?(password, saved_password) do
Bcrypt.verify_pass(password, saved_password)
end
def generate(opts) do
range = Enum.concat([?0..?9, ?A..?Z, ?a..?z, [?_, ?-, ?:, ??, ?!, ?.]])
for(_i <- 1..opts[:min_password_length], do: <<Enum.random(range)>>)
|> Enum.join()
end
end