diff options
author | Alex Auvolat <alex@adnab.me> | 2018-09-26 15:26:06 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-09-26 15:26:06 +0200 |
commit | 7d0009d83c9b5c747d4adb535817924762429f30 (patch) | |
tree | 59b368cf189fc10976337e8caf4e6b623d69a04b /shard/lib/keys.ex | |
parent | f16973d3a492ae6d4890c40d77b0a93d3293bf3a (diff) | |
download | shard-7d0009d83c9b5c747d4adb535817924762429f30.tar.gz shard-7d0009d83c9b5c747d4adb535817924762429f30.zip |
Make it work, including Web interface
Diffstat (limited to 'shard/lib/keys.ex')
-rw-r--r-- | shard/lib/keys.ex | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/shard/lib/keys.ex b/shard/lib/keys.ex index 0dc3154..de054e1 100644 --- a/shard/lib/keys.ex +++ b/shard/lib/keys.ex @@ -48,11 +48,21 @@ defmodule Shard.Keys do Agent.get(__MODULE__, &(&1)) end + def get_any_identity() do + Agent.get(__MODULE__, fn _ -> + case list_identities() do + [x|_] -> x + [] -> new_identity() + end + end) + end + @doc""" Generate a new keypair for a user identity, and start an Identity Shard for it. """ def new_identity() do {pk, sk} = gen_keypair(Application.get_env(:shard, :identity_suffix)) + Logger.info "New identity: #{pk|>Base.encode16}" :dets.insert @key_db, {pk, sk} SApp.Identity.start_link(pk) pk @@ -86,7 +96,7 @@ defmodule Shard.Keys do If correct, returns {:ok, original_message} """ def open(pk, signed) do - if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do + if valid_identity_pk? pk do Sign.open(signed, pk) else {:error, :invalid_pk_suffix} @@ -115,10 +125,24 @@ defmodule Shard.Keys do Returns :ok if the signature was correct. """ def verify(pk, bin, sign) do - if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do + if valid_identity_pk? pk do Sign.verify_detached(sign, bin, pk) else {:error, :invalid_pk_suffix} end end + + @doc""" + Check if a public key is a valid identity pk. Requirement: have the correct suffix. + """ + def valid_identity_pk?(pk) do + check_suffix(pk, Application.get_env(:shard, :identity_suffix)) + end + + def pk_display(pk) do + pk + |> binary_part(0, 4) + |> Base.encode16 + |> String.downcase + end end |