Skip to content

Instantly share code, notes, and snippets.

@wttj-tech
Last active March 28, 2022 08:37
Show Gist options
  • Save wttj-tech/dc8548331b91f2922f751eb03f5fd108 to your computer and use it in GitHub Desktop.
Save wttj-tech/dc8548331b91f2922f751eb03f5fd108 to your computer and use it in GitHub Desktop.
Prevent timing attack
@doc """
Verifies the password.
If there is no user or the user doesn't have a password, we call
`Bcrypt.no_user_verify/0` to avoid timing attacks.
"""
def valid_password?(%TestAuth.Accounts.User{hashed_password: hashed_password}, password)
when is_binary(hashed_password) and byte_size(password) > 0 do
Bcrypt.verify_pass(password, hashed_password)
end
def valid_password?(_, _) do
Bcrypt.no_user_verify()
false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment