webring/apps/webring/lib/webring/user.ex

53 lines
1.2 KiB
Elixir

defmodule Webring.Password do
@salt ".!.!.!013aF"
@alg :sha256
def hash_with_salt(password) do
:crypto.hash(@alg, password <> @salt)
|> :binary.encode_hex()
|> String.downcase()
end
def hash(password) do
hash_with_salt(password)
end
def verify(password, hashed_password) do
hash_with_salt(password) == hashed_password
end
def dummy() do
hash_with_salt("lol lmao rofl lul so funny") == "not a real sha256 sum because we don't care"
end
end
defmodule Webring.User do
use Ecto.Schema
import Ecto.Changeset
alias Webring.Password
schema "users" do
field(:username, :string)
field(:password, :string, virtual: true)
field(:hashed_password, :string)
timestamps()
end
def changeset(user, params) do
end
def changeset_with_password(user, params) do
user
|> cast(params, [:password])
|> validate_required(:password)
|> validate_length(:password, 5)
|> validate_confirmation(:password, required: true)
|> hash_password
|> changeset(params)
end
def hash_password(%Ecto.Changeset{changes: %{password: pass}} = changeset) do
changeset
|> put_change(:hashed_password, Password.hash(pass))
end
end