forked from AkkomaGang/akkoma
Salmon: generate private key with native crypto if available.
This commit is contained in:
parent
44f7154fb9
commit
4524721fba
1 changed files with 22 additions and 9 deletions
|
@ -73,17 +73,30 @@ def encode_key({:RSAPublicKey, modulus, exponent}) do
|
||||||
"RSA.#{modulus_enc}.#{exponent_enc}"
|
"RSA.#{modulus_enc}.#{exponent_enc}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_rsa_pem do
|
# Native generation of RSA keys is only available since OTP 20+ and in default build conditions
|
||||||
port = Port.open({:spawn, "openssl genrsa"}, [:binary])
|
# We try at compile time to generate natively an RSA key otherwise we fallback on the old way.
|
||||||
{:ok, pem} = receive do
|
try do
|
||||||
{^port, {:data, pem}} -> {:ok, pem}
|
_ = :public_key.generate_key({:rsa, 2048, 65537})
|
||||||
end
|
def generate_rsa_pem do
|
||||||
Port.close(port)
|
key = :public_key.generate_key({:rsa, 2048, 65537})
|
||||||
if Regex.match?(~r/RSA PRIVATE KEY/, pem) do
|
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
|
||||||
|
pem = :public_key.pem_encode([entry]) |> String.trim_trailing
|
||||||
{:ok, pem}
|
{:ok, pem}
|
||||||
else
|
|
||||||
:error
|
|
||||||
end
|
end
|
||||||
|
rescue
|
||||||
|
_ ->
|
||||||
|
def generate_rsa_pem do
|
||||||
|
port = Port.open({:spawn, "openssl genrsa"}, [:binary])
|
||||||
|
{:ok, pem} = receive do
|
||||||
|
{^port, {:data, pem}} -> {:ok, pem}
|
||||||
|
end
|
||||||
|
Port.close(port)
|
||||||
|
if Regex.match?(~r/RSA PRIVATE KEY/, pem) do
|
||||||
|
{:ok, pem}
|
||||||
|
else
|
||||||
|
:error
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def keys_from_pem(pem) do
|
def keys_from_pem(pem) do
|
||||||
|
|
Loading…
Reference in a new issue