Erlang version: 9.2
I am trying to sign one datablock with generated keys on ecdh-base.
Here is my workflow:
86> {PublicKey, PrivKeyOut} = crypto:generate_key(ecdh, crypto:ec_curve(secp521r1)).
{<<4,0,196,6,85,178,189,234,231,13,82,152,96,162,92,163,
133,81,42,147,168,146,138,226,15,80,127,228,...>>,
<<1,33,215,135,89,40,35,40,104,14,217,153,78,62,53,83,
198,165,84,30,135,159,218,82,47,102,204,...>>}
87> Mesage = "testmessage".
"testmessage"
88> Hash = crypto:hash(sha512, Mesage).
<<1,216,98,78,245,111,176,233,114,224,249,27,118,114,49,
189,40,144,90,249,175,108,79,235,186,247,247,40,131,...>>
89> Signature = crypto:sign(ecdsa, sha512, Hash, PrivKeyOut).
** exception error: bad argument
in function crypto:pkey_sign_nif/5
called as crypto:pkey_sign_nif(ecdsa,sha512,
<<1,216,98,78,245,111,176,233,114,224,
249,27,118,114,49,189,40,144,90,249,
175,108,79,235,186,247,...>>,
<<1,33,215,135,89,40,35,40,104,14,217,
153,78,62,53,83,198,165,84,30,135,159,
218,82,47,...>>,
[])
in call from crypto:sign/5 (crypto.erl, line 433)
What am I doing wrong?
There are two problems with your code:
crypto:sign/4
is calledKey
, but if you check the type spec, it in fact takes an[ecdh_private(), ecdh_params()]
list (in case of using theecdsa
algorithm at least).Hash
yourself, the third argument shall be{digest, Hash}
. Otherwise you will sign the hash of the message hash. You can also pass the plain message to the function, but in that case it has to be a binary, not a string.This is how to fix these problems:
Or, in case you need the
Hash
later and/or you getMessage
as a string, this would also work: