Trouble using github.com/smarkets/erlang-bcrypt/

567 views
Skip to first unread message

Jayson Bailey

unread,
Oct 14, 2013, 5:10:06 PM10/14/13
to elixir-l...@googlegroups.com
I'm trying to use bcrypt in my elixir app but can't seem to get anything working.  No matter which method I try to use, it bombs. It looks like it depends on NIF, but I don't know erlang at all(barely elixir) and so therefore don't understand this error, or how to fix it:

=ERROR REPORT==== 14-Oct-2013::15:03:45 ===
The on_load function for module bcrypt_nif returned {error,
                                                     {load_failed,
                                                      "Failed to load NIF library: 'dlopen(/Users/jayson/workspace/elixir/rexbro/deps/bcrypt/priv/bcrypt_nif.so, 2): image not found'"}}

Any help would be great.  It looks like this is the only option to use bcrypt for user passwords at the moment.  If there is something else, please point me in the right direction.

Thanks!

Chris Keele

unread,
Oct 14, 2013, 5:13:04 PM10/14/13
to elixir-l...@googlegroups.com
Looks like it could be your version of openSSL. What version are you running?

mrshankly

unread,
Oct 15, 2013, 3:41:31 AM10/15/13
to elixir-l...@googlegroups.com
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to elixir-lang-ta...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Use the opscode fork since it has a few fixes over
smarkets/erlang-bcrypt. Here's the link
https://github.com/opscode/erlang-bcrypt

Jayson Bailey

unread,
Oct 17, 2013, 3:37:30 PM10/17/13
to elixir-l...@googlegroups.com
I'm not running openssl 1.0.1e.  That didn't do it.  Changing to opscode version also didn't work.

iex(2)> :bcrypt.gen_salt()
** (MatchError) no match of right hand side value: :undefined
    src/bcrypt.erl:15: :bcrypt.mechanism/0
    src/bcrypt.erl:18: :bcrypt.gen_salt/0

Jayson Bailey

unread,
Oct 17, 2013, 4:12:26 PM10/17/13
to elixir-l...@googlegroups.com
Sorry, I'm NOW running openssl 1.0.1e

mrshankly

unread,
Oct 17, 2013, 4:46:58 PM10/17/13
to elixir-l...@googlegroups.com
On 17/10/13 21:12, Jayson Bailey wrote:
Sorry, I'm NOW running openssl 1.0.1e

On Thursday, October 17, 2013 1:37:30 PM UTC-6, Jayson Bailey wrote:
I'm not running openssl 1.0.1e.  That didn't do it.  Changing to opscode version also didn't work.

iex(2)> :bcrypt.gen_salt()
** (MatchError) no match of right hand side value: :undefined
    src/bcrypt.erl:15: :bcrypt.mechanism/0
    src/bcrypt.erl:18: :bcrypt.gen_salt/0

Are you starting :crypto and :bcrypt? You need to start both,

iex(1)> :application.start(:crypto)
:ok
iex(2)> :application.start(:bcrypt)
:ok
iex(3)> :bcrypt.gen_salt()        
{:ok, '$2a$12$froIOXasz2ynFiWpC5Dbd.'}

Dave Cottlehuber

unread,
Oct 18, 2013, 3:24:03 AM10/18/13
to elixir-l...@googlegroups.com
Hey Jayson,

Seems you're not making progress so I thought its worth going back to the beginning.

The error message says that the bcrypt_nif.so file cannot be found, i.e. it doesn't exist on disk. If the .so file is invalid, or otherwise faulty, the beam will return a different error. e.g. if I `echo junkyjunkyjunkyjunkyjunkyjunky > ./priv/bcrypt_nif.so` a different error is returned:

=ERROR REPORT==== 18-Oct-2013::08:21:19 ===
The on_load function for module bcrypt_nif returned {error,
  {load_failed,
  "Failed to load NIF library: 'dlopen(/zfs/shared/repos/erlang-bcrypt/ebin/../priv/bcrypt_nif.so,
2): no suitable image found.  Did find:\n\t/zfs/shared/repos/erlang-bcrypt/ebin/../priv/bcrypt_nif.so: file too short'"}}

Is your nif.so is missing? If so, this sounds like a build-time issue in either mix or bcrypt.

wrt to mix, how about sharing your config somewhere?

Here's a test run with a minimal mix, on OSX 10.8.5, Erlang R16B02, built via homebrew (IIRC with custom crypto, but I don't think this should matter for you), and elixir recent master branch. The only change to mix.exs is to add `[{ :bcrypt, github: "opscode/erlang-bcrypt" }]` in the dependencies.

dch@akai /ramdisk % mix new bc --bare
* creating README.md
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/bc.ex
* creating test
* creating test/test_helper.exs
* creating test/bc_test.exs

Your mix project was created with success.
You can use mix to compile it, test it, and more:

    cd bc
    mix compile
    mix test

Run `mix help` for more information.

dch@akai /ramdisk % cd bc

dch@akai bc % vi mix.exs
  … sprinkles pixie dust … 

dch@akai bc % cat mix.exs
defmodule Bc.Mixfile do
  use Mix.Project

  def project do
    [ app: :bc,
      version: "0.0.1",
      elixir: "~> 0.10.3-dev",
      deps: deps ]
  end

  # Configuration for the OTP application
  def application do
    []
  end

  defp deps do
    [{ :bcrypt, github: "opscode/erlang-bcrypt" }]
  end
end

dch@akai bc % mix deps.get
* Getting bcrypt [git: "git://github.com/opscode/erlang-bcrypt.git"]
Cloning into 'deps/bcrypt'...
remote: Counting objects: 423, done.
remote: Compressing objects: 100% (220/220), done.
remote: Total 423 (delta 199), reused 392 (delta 175)
Receiving objects: 100% (423/423), 576.47 KiB | 278.00 KiB/s, done.
Resolving deltas: 100% (199/199), done.
Checking connectivity... done
* Compiling bcrypt
==> bcrypt (compile)
Compiled src/bcrypt_app.erl
Compiled src/bcrypt_nif.erl
Compiled src/bcrypt.erl
Compiled src/bcrypt_nif_worker.erl
Compiled src/bcrypt_port_sup.erl
Compiled src/bcrypt_pool.erl
Compiled src/bcrypt_port.erl
Compiled src/bcrypt_sup.erl
Compiling c_src/async_queue.c
Compiling c_src/bcrypt.c
Compiling c_src/bcrypt_nif.c
Compiling c_src/bcrypt_port.c
Compiling c_src/blowfish.c
cc   -I/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/include -I/usr/local/Cellar/erlang/R16B02/lib/erlang/erts-5.10.3/include  -L/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/lib -lerl_interface -lei bcrypt_port.c bcrypt.o blowfish.o   -L/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/lib -lerl_interface -lei -lpthread -o ../priv/bcrypt

dch@akai bc % find . |grep \.so
./deps/bcrypt/priv/bcrypt_nif.so

dch@akai bc % iex  -pa deps/bcrypt/ebin
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (0.10.3-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(console@akai)1> :application.start :crypto
:ok
iex(console@akai)2> :application.start :bcrypt
:ok
iex(console@akai)3> {:ok, salt} = :bcrypt.gen_salt
{:ok, '$2a$12$4JcHRWAf1A08jlWmeegkwO'}
iex(console@akai)4> {:ok, hash} =  :bcrypt.hashpw "foo", salt
{:ok, '$2a$12$4JcHRWAf1A08jlWmeegkwO7J6K7b9X0ufvOAaIvQ2nmlt.kFRAYsu'}
iex(console@akai)5>


If that doesn't work, I'd also suggest confirming that this compiles & and runs at erlang level:

dch@akai /ramdisk % erl +V
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 5.10.3

dch@akai /ramdisk % git clone git://github.com/opscode/erlang-bcrypt.git
Cloning into 'erlang-bcrypt'...

dch@akai /ramdisk % cd erlang-bcrypt

dch@akai erlang-bcrypt % make
erlang-bcrypt (compile)
Compiled src/bcrypt_app.erl
Compiled src/bcrypt.erl
Compiled src/bcrypt_nif.erl
Compiled src/bcrypt_nif_worker.erl
Compiled src/bcrypt_port_sup.erl
Compiled src/bcrypt_sup.erl
Compiled src/bcrypt_pool.erl
Compiled src/bcrypt_port.erl
Compiling c_src/async_queue.c
Compiling c_src/bcrypt.c
Compiling c_src/bcrypt_nif.c
Compiling c_src/bcrypt_port.c
Compiling c_src/blowfish.c
cc   -I/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/include -I/usr/local/Cellar/erlang/R16B02/lib/erlang/erts-5.10.3/include    -L/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/lib -lerl_interface -lei bcrypt_port.c bcrypt.o blowfish.o   -L/usr/local/Cellar/erlang/R16B02/lib/erlang/lib/erl_interface-3.7.14/lib -lerl_interface -lei -lpthread -o ../priv/bcrypt

dch@akai erlang-bcrypt % find . |grep \.so
./priv/bcrypt_nif.so

dch@akai erlang-bcrypt % erl -pa ./ebin -s crypto -s bcrypt
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V5.10.3  (abort with ^G)
1> {ok, Salt} = bcrypt:gen_salt().
{ok,"$2a$12$2pL.yJMEKUA.7j/ZDQe5Iu"}
2> {ok, Hash} = bcrypt:hashpw("foo", Salt).
{ok,"$2a$12$2pL.yJMEKUA.7j/ZDQe5IuuE4bVyXaSh4JJDnNMeUBAEqoH3vuaWW"}
3> {ok, Hash} =:= bcrypt:hashpw("foo", Hash).
true
4> q().
ok

FWIW there's also https://github.com/tonyg/erlang-scrypt but if bcrypt doesn't even build we should sort that out first. 
-- 
Dave Cottlehuber
Sent with Airmail
Reply all
Reply to author
Forward
0 new messages