Add Amethyst.Keys.decrypt/1
All checks were successful
Build & Test / nix-build (push) Successful in 1m7s

This commit is contained in:
Kodi Craft 2024-07-09 10:28:57 +02:00
parent 6a2701aace
commit 099d2a3a68
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -38,6 +38,10 @@ defmodule Amethyst.Keys do
GenServer.call(__MODULE__, :get_pub) GenServer.call(__MODULE__, :get_pub)
end end
def decrypt(encrypted) do
GenServer.call(__MODULE__, {:decrypt, encrypted})
end
@impl true @impl true
def init(bits) do def init(bits) do
Logger.info("Generating RSA keys with #{bits} bits") Logger.info("Generating RSA keys with #{bits} bits")
@ -46,20 +50,24 @@ defmodule Amethyst.Keys do
rsa_private_key = :public_key.generate_key({:rsa, bits, 65_537}) rsa_private_key = :public_key.generate_key({:rsa, bits, 65_537})
rsa_public_key = {:RSAPublicKey, modulus, public_exponent} rsa_public_key = {:RSAPublicKey, modulus, public_exponent}
privkey = :public_key.der_encode(:RSAPrivateKey, rsa_private_key)
pubkey = :public_key.der_encode(:RSAPublicKey, rsa_public_key)
Logger.info("Generated RSA keys") Logger.info("Generated RSA keys")
{:ok, {pubkey, privkey}} {:ok, {rsa_public_key, rsa_private_key}}
end end
@impl true @impl true
def handle_call(:get_priv, _from, {pubkey, privkey}) do def handle_call(:get_priv, _from, {pubkey, privkey}) do
{:reply, privkey, {pubkey, privkey}} {:reply, :public_key.der_encode(:RSAPrivateKey, privkey), {pubkey, privkey}}
end end
@impl true @impl true
def handle_call(:get_pub, _from, {pubkey, privkey}) do def handle_call(:get_pub, _from, {pubkey, privkey}) do
{:reply, pubkey, {pubkey, privkey}} {:reply, :public_key.der_encode(:RSAPublicKey, pubkey), {pubkey, privkey}}
end
@impl true
def handle_call({:decrypt, encrypted}, _from, {pubkey, privkey}) do
plaintext = :public_key.decrypt_private(encrypted, privkey)
{:reply, plaintext, {pubkey, privkey}}
end end
end end