Search code examples
elixirimporterrorpassword-hash

Something wrong with my Comeonin.bcrypt code for password hashing


So I'm using Phoenix latest version and Comeonin 5.0 and bcrypt_elixir 2.0 for hashing my password but somehow it's not able to use my current function which I'm using and it's showing that it's undefined.

Because the function is changed now before I was using hashpwsalt(pass) to pass my password to create a hash. But now I'm using hash_pwd_salt(pass). But it's still showing me the error.

defp put_password_hash(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: pass}}
        ->
          put_change(changeset, :password_hash, hash_pwd_salt(pass))
      _ ->
          changeset
    end
  end
end

This is the error I'm getting when I try to run my server :

cannot import Comeonin.Bcrypt.hash_pwd_salt/1 because it is undefined or private

Please take a look at my code and tell me what changes I can do?


Solution

  • The problem is there's no single parameter function hash_pwd_salt/1. There's a hash_pwd_salt/2, and the 2nd parameter is optional.

    Here's the source for that function in v2.0 of Bcrypt:

      def hash_pwd_salt(password, opts \\ []) do
        Base.hash_password(
          password,
          gen_salt(
            Keyword.get(opts, :log_rounds, Application.get_env(:bcrypt_elixir, :log_rounds, 12)),
            Keyword.get(opts, :legacy, false)
          )
        )
    end
    

    Note that if you alias the module instead, you don't need to the the arity of the function.

    E.g., put alias Comeonin.Bcrypt near the top of your file, and then specify the module when calling the function like this: Bcrypt.hash_pwd_salt(pass)