我尝试使用 crypto:sign/4 对消息进行签名但失败了。谁能告诉我如何在 Erlang 中使用 ECDSA 签署消息?谢谢。(我使用的是 Erlang 版本 R16B01。)
模块代码:
-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.
但它在测试运行中失败了:
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)
感谢 Paul,可以通过进行以下更改来修复此错误。
改变:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, PriKey),
至:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, [PriKey, secp256k1]),