(UndefinedFunctionError) function :crypto.hmac/3 is undefined or private

I’m failing to render any page, the application takes giving me the following error

[error] #PID<0.1204.0> running ElixirWeb.Endpoint (connection #PID<0.1203.0>, stream id 1) terminated
Server: localhost:8080 (http)
Request: GET /
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function :crypto.hmac/3 is undefined or private
        (crypto 5.0) :crypto.hmac/3
        (plug_crypto 1.2.0) lib/plug/crypto/key_generator.ex:81: Plug.Crypto.KeyGenerator.generate/7
        (plug_crypto 1.2.0) lib/plug/crypto/key_generator.ex:67: Plug.Crypto.KeyGenerator.with_cache/3
        (plug_crypto 1.2.0) lib/plug/crypto/key_generator.ex:51: Plug.Crypto.KeyGenerator.generate/6
        (plug 1.11.0) lib/plug/session/cookie.ex:114: Plug.Session.COOKIE.put/4
        (plug 1.11.0) lib/plug/session.ex:94: anonymous fn/3 in Plug.Session.before_send/2
        (elixir 1.12.0) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
        (plug 1.11.0) lib/plug/conn.ex:1691: Plug.Conn.run_before_send/2
        (plug 1.11.0) lib/plug/conn.ex:407: Plug.Conn.send_resp/1
        (phoenix 1.5.6) lib/phoenix/endpoint/render_errors.ex:78: Phoenix.Endpoint.RenderErrors.instrument_render_and_send/5
        (phoenix 1.5.6) lib/phoenix/endpoint/render_errors.ex:64: Phoenix.Endpoint.RenderErrors.__catch__/5
        (phoenix 1.5.6) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.8.0) ~/elixir/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.8.0) ~/elixir/deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
        (cowboy 2.8.0) ~/elixir/deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
        (stdlib 3.15) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
2 Likes

Try to update plug_crypto library. It uses :crypto.hmac/3 which was removed in OTP 24.
As you can see on GitHub, newer version of plug_crypto supports OTP 24 plug_crypto/key_generator.ex at 6ba8fd42a68351459fe6c769ca04aea6022a5cd1 · elixir-plug/plug_crypto · GitHub

17 Likes

By this error I have just know now mix there are more interesting options :smiley:
Example: mix deps.update --all

8 Likes

In case you are using a pure Elixir implementation without Plug.

:crypto.hmac/{3,4} is deprecated and is removed in Erlang/OTP 24.

:crypto.mac/4 should be instead, but it is available only since Erlang/OTP 22.1.

Both functions yield the same result:

iex(6)> :crypto.mac(:hmac, :sha256, "key", "data") == :crypto.hmac(:sha256, "key", "data")
true
iex(7)> :crypto.mac(:hmac, :sha384, "key", "data") == :crypto.hmac(:sha384, "key", "data")
true
6 Likes

updated this dependency but still facing the same issue.
Pls help

1 Like

Could you show the stacktrace?

I have the same error, when i try connecto to my mongodb.

this is the error

[error] GenServer #PID<0.524.0> terminating
** (stop) exited in: GenServer.call(Mongo.PBKDF2Cache, {"7fa53ac867620679d198f32788c692ab", <<178, 132, 122, 7, 0, 224, 234, 189, 233, 38, 179, 162, 21, 21, 223, 101>>, 10000}, 5000)
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function :crypto.hmac/3 is undefined or private
            (crypto 5.0.3) :crypto.hmac(:sha, "7fa53ac867620679d198f32788c692ab", <<178, 132, 122, 7, 0, 224, 234, 189, 233, 38, 179, 162, 21, 21, 223, 101, 0, 0, 0, 1>>)
            (mongodb 0.5.1) lib/mongo/pbkdf2.ex:46: Mongo.PBKDF2.generate/7
            (mongodb 0.5.1) lib/mongo/pbkdf2_cache.ex:46: anonymous fn/4 in Mongo.PBKDF2Cache.run_task/1
            (elixir 1.12.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
            (elixir 1.12.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
            (stdlib 3.16) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
    (elixir 1.12.2) lib/gen_server.ex:1024: GenServer.call/3
    (mongodb 0.5.1) lib/mongo/auth/scram.ex:40: Mongo.Auth.SCRAM.first/5
    (mongodb 0.5.1) lib/mongo/auth/scram.ex:17: Mongo.Auth.SCRAM.auth/2
    (mongodb 0.5.1) lib/mongo/auth.ex:16: anonymous fn/3 in Mongo.Auth.run/2
    (elixir 1.12.2) lib/enum.ex:3884: Enum.find_value_list/3
    (mongodb 0.5.1) lib/mongo/auth.ex:15: Mongo.Auth.run/2
    (mongodb 0.5.1) lib/mongo/protocol.ex:57: Mongo.Protocol.connect/2
    (db_connection 2.4.1) lib/db_connection/connection.ex:82: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.16) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Mongo.Protocol

and this is my mix.exs

defmodule App.MixProject do
  use Mix.Project

  def project do
    [
      app: :app,
      version: "0.1.0",
      elixir: "~> 1.12",
      elixirc_paths: elixirc_paths(Mix.env()),
      compilers: [:gettext] ++ Mix.compilers(),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {App.Application, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:phoenix, "~> 1.6.2"},
      {:phoenix_live_dashboard, "~> 0.5"},
      {:esbuild, "~> 0.2", runtime: Mix.env() == :dev},
      {:swoosh, "~> 1.3"},
      {:telemetry_metrics, "~> 0.6"},
      {:telemetry_poller, "~> 1.0"},
      {:gettext, "~> 0.18"},
      {:jason, "~> 1.2"},
      {:plug_cowboy, "~> 2.5"},
      {:mongodb, ">= 0.0.0"},
      {:poolboy, ">= 0.0.0"}
    ]
  end

  # Aliases are shortcuts or tasks specific to the current project.
  # For example, to install project dependencies and perform other setup tasks, run:
  #
  #     $ mix setup
  #
  # See the documentation for `Mix` for more info on aliases.
  defp aliases do
    [
      setup: ["deps.get"],
      "assets.deploy": ["esbuild default --minify", "phx.digest"]
    ]
  end
end

I not understand, because I have the plug_crypto version 1.2.2,

elixir --version

Erlang/OTP 24 [erts-12.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.12.2 (compiled with Erlang/OTP 22)

and erl command

Erlang/OTP 24 [erts-12.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Eshell V12.1  (abort with ^G)

what is wrong???

:crypto.hmac/3 is not available in OTP 24 (it was deprecated in OTP 23). This is being called from mongodb - which therefore needs updating. And it looks like this commit on September 28th does exactly that.

Since that code appears unreleased you might be able to adjust your mix.exs to be:

def deps do
  ...
  {:mongodb, github: "elixir-mongo/mongodb"},
  ...
end
2 Likes

You can try out mongodb_driver | Hex which has support for OTP 23 and OTP 24.