Using Elliptic Curve keys for signing in Erlang

8 Dec 2023 14:10 erlang cryptography

How do I sign (and verify) things in Erlang, using an ECDSA key?

We’ll start with a private key on the NIST P-256 (secp256r1, prime256v1) curve:

PrivateKey = public_key:generate_key({namedCurve, secp256r1}).

Signing a message

Signature = public_key:sign(<<"Hello World">>, sha256, PrivateKey).

Verifying a message

To verify a message, you need only the public key:

#'ECPrivateKey'{publicKey = Pub, parameters = Params} = PrivateKey.
PublicKey = {#'ECPoint'{point = Pub}, Params}.
true = public_key:verify(<<"Hello World">>, sha256, Signature, PublicKey).

But it also works with the private key:

true = public_key:verify(<<"Hello World">>, sha256, Signature, PrivateKey).