Bcypt not functional in Windows 11

I am using Windows 11, with Elixir 1.17.3 and Erlang 27.1, tried make encryption auth using bcrypt but still not working, here the code error below when trying create new account :

[debug] HANDLE EVENT "save" in EventflowWeb.UserRegistrationLive
  Parameters: %{"_csrf_token" => "NgA1axEPDzodO1goMgsAMBohGQ4pBBsAEK_1GICryH7FpJLREraOftH9", "user" => %{"email" => "test@gmail.com", "password" => "[FILTERED]"}}
[debug] QUERY OK source="users" db=0.1ms queue=0.1ms idle=1803.9ms
SELECT 1 FROM "users" AS u0 WHERE (u0."email" = ?) LIMIT 1 ["test@gmail.com"]
↳ Ecto.Changeset.unsafe_validate_unique/4, at: lib/ecto/changeset.ex:2700
[error] Process #PID<0.828.0> raised an exception
** (RuntimeError) An error occurred when loading Bcrypt.
Make sure you have a C compiler and Erlang 20 installed.
If you are not using Erlang 20, either upgrade to Erlang 20 or
use version 0.12 of bcrypt_elixir.
See the Comeonin wiki for more information.

    (bcrypt_elixir 3.2.0) lib/bcrypt/base.ex:15: Bcrypt.Base.init/0
    (kernel 10.1) code_server.erl:1356: anonymous fn/1 in :code_server.handle_on_load/5
[warning] The on_load function for module Elixir.Bcrypt.Base returned:
{%RuntimeError{
   message: "An error occurred when loading Bcrypt.\nMake sure you have a C compiler and Erlang 20 installed.\nIf you are not using Erlang 20, either upgrade to Erlang 20 or\nuse version 0.12 of bcrypt_elixir.\nSee the Comeonin wiki for more information.\n"
 },
 [
   {Bcrypt.Base, :init, 0,
    [file: ~c"lib/bcrypt/base.ex", line: 15, error_info: %{...}]},
   {:code_server, :"-handle_on_load/5-fun-0-", 1,
    [file: ~c"code_server.erl", line: 1356]}
 ]}

[error] GenServer #PID<0.630.0> terminating
** (UndefinedFunctionError) function Bcrypt.Base.gen_salt/2 is undefined (module Bcrypt.Base is not available)
    (bcrypt_elixir 3.2.0) Bcrypt.Base.gen_salt(12, false)
    (bcrypt_elixir 3.2.0) lib/bcrypt.ex:77: Bcrypt.hash_pwd_salt/2
    (eventflow 0.1.0) lib/eventflow/users/user.ex:76: Eventflow.Users.User.maybe_hash_password/2
    (eventflow 0.1.0) lib/eventflow/users.ex:79: Eventflow.Users.register_user/1
    (eventflow 0.1.0) lib/eventflow_web/live/user_registration_live.ex:57: EventflowWeb.UserRegistrationLive.handle_event/3
    (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/channel.ex:508: anonymous fn/3 in Phoenix.LiveView.Channel.view_handle_event/3
    (telemetry 1.2.1) c:/Users/Ariandani/app/phoenix/eventflow/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
    (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/channel.ex:260: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 6.1) gen_server.erl:2345: :gen_server.try_handle_info/3
    (stdlib 6.1) gen_server.erl:2433: :gen_server.handle_msg/6
    (stdlib 6.1) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{topic: "lv:phx-F__O4w2TMQDsaRDD", event: "event", payload: %{"event" => "save", "type" => "form", "value" => "_csrf_token=NgA1axEPDzodO1goMgsAMBohGQ4pBBsAEK_1GICryH7FpJLREraOftH9&user%5Bemail%5D=test%40gmail.com&user%5Bpassword%5D=testgmail123"}, ref: "33", join_ref: "16"}
State: %{socket: #Phoenix.LiveView.Socket<id: "phx-F__O4w2TMQDsaRDD", endpoint: EventflowWeb.Endpoint, view: EventflowWeb.UserRegistrationLive, parent_pid: nil, root_pid: #PID<0.630.0>, router: EventflowWeb.Router, assigns: %{form: nil, __changed__: %{}, current_user: nil, flash: %{}, live_action: :new, trigger_submit: false, check_errors: false}, transport_pid: #PID<0.623.0>, ...>, components: {%{}, %{}, 1}, topic: "lv:phx-F__O4w2TMQDsaRDD", serializer: Phoenix.Socket.V2.JSONSerializer, join_ref: "16", upload_names: %{}, upload_pids: %{}}
[debug] MOUNT EventflowWeb.UserRegistrationLive
  Parameters: %{}
  Session: %{"_csrf_token" => "sKjZVFLHdsonBALb_SxAOpS9"}
[debug] Replied in 1ms
[debug] HANDLE EVENT "validate" in EventflowWeb.UserRegistrationLive
  Parameters: %{"_csrf_token" => "NgA1axEPDzodO1goMgsAMBohGQ4pBBsAEK_1GICryH7FpJLREraOftH9", "_target" => ["user", "email"], "user" => %{"_unused_email" => "", "_unused_password" => "[FILTERED]", "email" => "test@gmail.com", "password" => "[FILTERED]"}}
[debug] Replied in 204”s

I am also install all c++ compiler in visual studio tools completed (almost GB file)

here some source code i forked :

1 Like

The comeonin README already warns windows users that they will have problems using bcrypt and argon2 and suggest to use pbkdf2 which has no NIF dependencies.

3 Likes

If you are just developing on Windows but that is not your deployment target, you can run Ubuntu from Windows very easily using “WSL”

See

That is how I operate these days. Too many packages for Elixir won’t work on Windows.

1 Like

I’m facing the same issue on my Windows 11 environment. Before compiling Bcrypt on Windows, you need to configure your development environment.

To do this, in the same shell where you’ll be compiling, run the following command: cmd /K “C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsDevCmd.bat” -arch=x64. This command sets up your shell. The path needs to be customized to reflect your specific installation.

You should also consider removing the previous version of Bcrypt in your _build/dev/lib (if it wasn’t compiled correctly) or force the compilation with the command ‘mix deps.compile bcrypt_elixir --force’.

1 Like

I’ve compiled bcrypt on Windows before - but the process is not fun. Once bcrypt is compiled, you’ll forget all about it - until you upgrade or move to a new PC. Then you’ll have to do it all over again!

On a Windows machine, if you create a new Phoenix project and then run:

mix phx.gen.auth

It will use pbkdf2 instead of bcrypt. In the generated code the pdkdf2 calls are identical to their bcrypt counterparts (same function names/signatures).

2 Likes

Does both of them has pro n cons ?

There’s mention of the three libraries here:

https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Auth.html#module-password-hashing

Which also refers to the README on Comonin. Further digging reveals:

The long and the short of it is - consider using Argon2 (which requires more memory) and that bcrypt and pdkdf2 are comparable (but more susceptable to GPU attacks)

1 Like