diff options
Diffstat (limited to 'shard/lib/keys.ex')
-rw-r--r-- | shard/lib/keys.ex | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/shard/lib/keys.ex b/shard/lib/keys.ex index fe63148..d021242 100644 --- a/shard/lib/keys.ex +++ b/shard/lib/keys.ex @@ -4,7 +4,6 @@ defmodule Shard.Keys do """ use Agent - require Salty.Sign.Ed25519, as: Sign require Logger @key_db [Application.get_env(:shard, :data_path), "key_db"] |> Path.join |> String.to_atom @@ -20,7 +19,7 @@ defmodule Shard.Keys do end defp gen_keypair(suffix, n \\ 0) do - {:ok, pk, sk} = Sign.keypair + %{public: pk, secret: sk} = :enacl.sign_keypair if rem(n, 10000) == 0 do Logger.info "#{n}... expected #{:math.pow(256, byte_size(suffix))}" end @@ -51,7 +50,7 @@ defmodule Shard.Keys do {pk, sk} = gen_keypair(Application.get_env(:shard, :identity_suffix)) Logger.info "New identity: #{pk|>Base.encode16}" :dets.insert @key_db, {pk, sk} - Shard.Manager.Manifest.start %SApp.Identity.Manifest{pk: pk} + Shard.Manifest.start %SApp.Identity.Manifest{pk: pk} pk end @@ -72,7 +71,7 @@ defmodule Shard.Keys do def sign(pk, bin) do case :dets.lookup @key_db, pk do [{^pk, sk}] -> - Sign.sign(bin, sk) + :enacl.sign(bin, sk) _ -> {:error, :not_found} end end @@ -84,7 +83,7 @@ defmodule Shard.Keys do """ def open(pk, signed) do if valid_identity_pk? pk do - Sign.open(signed, pk) + :enacl.sign_open(signed, pk) else {:error, :invalid_pk_suffix} end @@ -97,6 +96,13 @@ defmodule Shard.Keys do end end + def get_sk(pk) do + case :dets.lookup @key_db, pk do + [{^pk, sk}] -> sk + _ -> nil + end + end + @doc""" Lookup the secret key for a pk and generate a detached signature for a message. @@ -108,7 +114,7 @@ defmodule Shard.Keys do def sign_detached(pk, bin) do case :dets.lookup @key_db, pk do [{^pk, sk}] -> - Sign.sign_detached(bin, sk) + :enacl.sign_detached(bin, sk) _ -> {:error, :not_found} end end @@ -120,7 +126,7 @@ defmodule Shard.Keys do """ def verify(pk, bin, sign) do if valid_identity_pk? pk do - Sign.verify_detached(sign, bin, pk) + :enacl.sign_verify_detached(sign, bin, pk) else {:error, :invalid_pk_suffix} end |