Skip to content

Instantly share code, notes, and snippets.

@voltone
Created March 9, 2019 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voltone/d3c0bb3ee821703f52d439e00262cb88 to your computer and use it in GitHub Desktop.
Save voltone/d3c0bb3ee821703f52d439e00262cb88 to your computer and use it in GitHub Desktop.
ECDSASignature module for Elixir
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