-
-
Save voltone/d3c0bb3ee821703f52d439e00262cb88 to your computer and use it in GitHub Desktop.
ECDSASignature module for Elixir
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
defmodule ECDSASignature do | |
require Record | |
Record.defrecord( | |
:ecdsa_signature, | |
:"ECDSA-Sig-Value", | |
Record.extract(:"ECDSA-Sig-Value", from_lib: "public_key/include/OTP-PUB-KEY.hrl") | |
) | |
def new(r, s) when is_integer(r) and is_integer(s) do | |
ecdsa_signature(r: r, s: s) | |
end | |
def new(raw) when is_binary(raw) do | |
size = raw |> byte_size() |> div(2) | |
<<r::size(size)-unit(8), s::size(size)-unit(8)>> = raw | |
new(r, s) | |
end | |
# Export to DER binary format, for use with :public_key.verify/4 | |
def to_der(ecdsa_signature() = signature) do | |
:public_key.der_encode(:"ECDSA-Sig-Value", signature) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment