我試圖使用crypto:sign/4來簽署消息但失敗。任何人都可以告訴我如何在Erlang中使用ECDSA簽署消息?謝謝。 (我使用Erlang的版本R16B01。)我應該如何使用crypto:sign/4?
模塊代碼:
-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)
感謝保羅,這個錯誤通過進行以下更改來修復。
變化:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, PriKey),
到:
SigBin = crypto:sign(Algorithm, DigestType, MsgBin, [PriKey, secp256k1]),
非常感謝你,保羅:) – Ning