mobilizon.chapril.org-mobil.../lib/mobilizon/crypto.ex

29 lines
704 B
Elixir
Raw Permalink Normal View History

2019-09-07 19:54:11 +02:00
defmodule Mobilizon.Crypto do
@moduledoc """
Utility module which contains cryptography related functions.
"""
@doc """
Returns random byte sequence of the length encoded to Base64.
"""
@spec random_string(integer) :: String.t()
def random_string(length) do
length
|> :crypto.strong_rand_bytes()
|> Base.url_encode64()
end
2019-09-09 00:52:49 +02:00
@doc """
Generate RSA 2048-bit private key.
"""
@spec generate_rsa_2048_private_key :: String.t()
def generate_rsa_2048_private_key do
key = :public_key.generate_key({:rsa, 2048, 65_537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
[entry]
|> :public_key.pem_encode()
|> String.trim_trailing()
end
2019-09-07 19:54:11 +02:00
end