Current section

23 Versions

Jump to

Compare versions

4 files changed
+45 additions
-7 deletions
  @@ -1,9 +1,10 @@
1 1 {<<"app">>,<<"ed25519">>}.
2 2 {<<"build_tools">>,[<<"mix">>]}.
3 3 {<<"description">>,<<"Ed25519 signature functions">>}.
4 - {<<"elixir">>,<<"~> 1.4">>}.
4 + {<<"elixir">>,<<"~> 1.5">>}.
5 5 {<<"files">>,
6 - [<<"lib/ed25519.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}.
6 + [<<"lib/ed25519.ex">>,<<"lib/hash.ex">>,<<"mix.exs">>,<<"README.md">>,
7 + <<"LICENSE">>]}.
7 8 {<<"licenses">>,[<<"MIT">>]}.
8 9 {<<"links">>,
9 10 [{<<"GitHub">>,<<"https://github.com/mwmiller/ed25519_ex">>},
  @@ -11,4 +12,4 @@
11 12 {<<"maintainers">>,[<<"Matt Miller">>]}.
12 13 {<<"name">>,<<"ed25519">>}.
13 14 {<<"requirements">>,[]}.
14 - {<<"version">>,<<"1.0.2">>}.
15 + {<<"version">>,<<"1.1.0">>}.
  @@ -4,6 +4,24 @@ defmodule Ed25519 do
4 4 Ed25519 signature functions
5 5
6 6 This is mostly suitable as part of a pure Elixir solution.
7 +
8 + ## Configuration
9 +
10 + *No configuration is needed* in most cases. However, if needed, a custom hash
11 + function can be configured. As per the specification - `sha512` is the default.
12 +
13 + `config/config.exs`
14 +
15 + use Mix.Config
16 +
17 + # The hash function will be invoked as 'Blake2.hash2b(payload, 16)'
18 + config :ed25519,
19 + hash_fn: {Blake2, :hash2b, [], [16]}
20 +
21 + # The hash function will be invoked as ':crypto.hash(:sha256, payload)'
22 + config :ed25519,
23 + hash_fn: {:crypto, :hash, [:sha256], []}
24 +
7 25 """
8 26 @typedoc """
9 27 public or secret key
  @@ -40,7 +58,9 @@ defmodule Ed25519 do
40 58 defp mod(x, y) when x > 0, do: rem(x, y)
41 59 defp mod(x, y) when x < 0, do: rem(y + rem(x, y), y)
42 60
43 - defp hash(m), do: :crypto.hash(:sha512, m)
61 + # __using__ Macro generates the hash function at compile time, which allows the
62 + # hashing function to be configurable without runtime overhead
63 + use Ed25519.Hash
44 64 defp hashint(m), do: m |> hash |> decodeint
45 65
46 66 # :crypto.mod_pow chokes on negative inputs, so we feed it positive values
  @@ -0,0 +1,17 @@
1 + defmodule Ed25519.Hash do
2 + @moduledoc false
3 + defmacro __using__(_) do
4 + {mod, fun, pre_args, post_args} = Application.get_env(:ed25519, :hash_fn,
5 + {:crypto, :hash, [:sha512], []})
6 +
7 + quote do
8 + defp hash (unquote(Macro.var(:m, __MODULE__))) do
9 + unquote(mod).unquote(fun)(
10 + unquote_splicing(pre_args),
11 + unquote(Macro.var(:m, __MODULE__)),
12 + unquote_splicing(post_args)
13 + )
14 + end
15 + end
16 + end
17 + end
  @@ -3,8 +3,8 @@ defmodule Ed25519.Mixfile do
3 3
4 4 def project do
5 5 [app: :ed25519,
6 - version: "1.0.2",
7 - elixir: "~> 1.4",
6 + version: "1.1.0",
7 + elixir: "~> 1.5",
8 8 name: "Ed25519",
9 9 source_url: "https://github.com/mwmiller/ed25519_ex",
10 10 build_embedded: Mix.env == :prod,
  @@ -21,7 +21,7 @@ defmodule Ed25519.Mixfile do
21 21 defp deps do
22 22 [
23 23 {:earmark, "~> 1.0", only: :dev},
24 - {:ex_doc, "~> 0.14", only: :dev},
24 + {:ex_doc, "~> 0.18", only: :dev},
25 25 {:credo, "~> 0.8", only: [:dev, :test]},
26 26 ]
27 27 end