defmodule Erlef.Crypto do
  @moduledoc false

  def compare(str1, str2), do: Plug.Crypto.secure_compare(str1, str2)

  def hmac_encode(key, data) do
    :hmac
    |> :crypto.mac(:sha3_256, key, data)
    |> Base.encode16(case: :lower)
  end
end

defmodule Erlef.Crypto do
  @moduledoc """
  Erlef.Crypto

  Provides wrappers around common cryptographic operations and dependencies
  """

  @doc """
  Compares two strings using `Plug.Crypto.secure_compare/2` to mitigate timing attacks.
  """
  @spec compare(String.t(), String.t()) :: boolean()
  def compare(str1, str2), do: Plug.Crypto.secure_compare(str1, str2)

  @doc """
  Computes Message Authencation Code (MAC) via `:erlang.mac/4`. 
  The computed MAC is base 16 encoded and lower cased. 
  """
  @spec hmac_encode(binary(), binary()) :: String.t()
  def hmac_encode(key, data) do
    :hmac
    |> :crypto.mac(:sha3_256, key, data)
    |> Base.encode16(case: :lower)
  end
end

Add simple tests and clean up Erlef.Crypto
Add simple tests and clean up Erlef.Crypto

simplabs/website,simplabs/website