Prevent unauthorized access by securely hashing and storing passwords.
def register_user(username, password) do
hashed_password = :crypto.hash(:sha256, password)
User.changeset(%User{}, %{username: username, password: hashed_password})
|> Repo.insert()
end
This Elixir function hashes passwords with the SHA-256 function before storing them. While SHA-256 is not inherently insecure, it is not suitable for password hashing due to its speed, which makes it susceptible to brute-force attacks.
def register_user(username, password) do
hashed_password = Comeonin.Bcrypt.hashpwsalt(password)
User.changeset(%User{}, %{username: username, password: hashed_password})
|> Repo.insert()
end
This Elixir function hashes passwords with bcrypt before storing them. bcrypt is a secure hashing function that is resistant to brute-force attacks due to its configurable computational cost.