To secure user passwords using cryptographically secure hashing algorithms.
defmodule MyApp.User do
use Ecto.Schema
schema "users" do
field :password_hash, :string
end
def hash_password(password) do
:crypto.hash(:md5, password)
end
end
This code is vulnerable because it uses the MD5 hash function which is considered insecure. It could allow an attacker to easily crack captured credentials.
defmodule MyApp.User do
use Ecto.Schema
import Comeonin.Bcrypt
schema "users" do
field :password_hash, :string
end
def hash_password(password) do
hashpwsalt(password)
end
end
In this secure code example, we've replaced the MD5 hash function with Bcrypt, which is considered secure. This will effectively mitigate the risks associated with MD5.