I tried to use crypto:sign/4 to sign a message but failed. Could anyone show me how to sign a message using ECDSA in Erlang? Thanks. (I'm using Erlang version R16B01.)
The module code:
-module(message).
-compile(export_all).
go() ->
{_PubKey, PriKey} = crypto:generate_key(ecdh, secp256k1),
SigBin = sign_message(PriKey, "Hello"),
SigBin.
sign_message(PriKey, Msg) ->
Algorithm = ecdsa,
DigestType = sha256,
MsgBin = list_to_binary(Msg),
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, PriKey),
SigBin.
But it failed on a test run:
1> message:go().
** exception error: no function clause matching crypto:sign(ecdsa,sha256,
{digest,
<<24,95,141,179,34,113,254,37,245,97,166,252,147,
139,46,38,67,6,236,48,78,218,81,128,...>>},
<<189,38,200,204,95,248,54,69,42,65,216,165,242,228,100,
54,158,5,61,174,58,198,191,161,9,...>>) (crypto.erl, line 462)
Thanks to Paul, this error can be fixed by making the following change.
change:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, PriKey),
to:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, [PriKey, secp256k1]),