Skip to content

Commit

Permalink
signing is consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Oct 23, 2024
1 parent c7969ed commit 6b715e4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/curves/ecdsa.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule CA.ECDSA do
require CA.Jacobian
require CA.ECDSA.OTP

def sign(message, privateKey, options \\ []) do
def sign(message, privateKey, options) do
%{hashfunc: hashfunc} = Enum.into(options, %{hashfunc: :sha256})
number = :crypto.hash(hashfunc, message) |> numberFromString()
curve = CA.KnownCurves.secp256k1()
curve = CA.KnownCurves.secp384r1()
randNum = CA.Integer.between(1, curve."N" - 1)
r = CA.Jacobian.multiply(curve."G", randNum, curve."N", curve."A", curve."P").x
|> CA.Integer.modulo(curve."N")
Expand All @@ -16,7 +16,7 @@ defmodule CA.ECDSA do
{r, s}
end

def private(bin), do: :erlang.element(2,X509.PrivateKey.from_pem(bin))
def private(bin), do: numberFromString(:erlang.element(3,:erlang.element(2,X509.PrivateKey.from_pem(bin))))
def public(bin), do: :public_key.pem_entry_decode(hd(:public_key.pem_decode(bin)))

def numberFromString(string) do
Expand All @@ -35,6 +35,12 @@ defmodule CA.ECDSA do
%CA.Point{ x: numberFromString(xs), y: numberFromString(ys)}
end

def sign(file, key) do
{:ok, msg} = :file.read_file file
{:ok, pem} = :file.read_file key
sign(msg, private(pem), [])
end

def verify(file, signature_file, pub) do
{:ok, msg} = :file.read_file file
{:ok, pem} = :file.read_file pub
Expand Down

0 comments on commit 6b715e4

Please sign in to comment.