Created
January 23, 2017 23:48
-
-
Save scrogson/a2029410cf88df513166a45fd9d523a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp gen_jwk_set do | |
{:ok, pem} = genrsa(2048) | |
pem | |
|> JOSE.JWK.from_pem() | |
|> JOSE.JWK.to_public() | |
|> JOSE.JWK.to_record() | |
|> List.wrap() | |
|> :jose_jwk_set.to_map(%{}) | |
|> Poison.encode() | |
end | |
defp genrsa(bits \\ 2048) when is_integer(bits) and bits > 0 do | |
with {data, 0} <- System.cmd("openssl", ["genrsa", to_string(bits)], [stderr_to_stdout: true]), | |
{start, _} <- :binary.match(data, "-----BEGIN RSA PRIVATE KEY-----") do | |
{:ok, :binary.part(data, start, String.length(data) - start)} | |
else error -> | |
{:error, error} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment