🤩

Erlang で ed25519 を利用して署名/検証する

2021/04/17に公開

Erlang の crypto モジュールで ed25519 の鍵ペアを生成し、そのペアを利用して署名をし、検証します。

Erlang/OTP 24 [RELEASE CANDIDATE 2] [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Eshell V12.0  (abort with ^G)
1> {PubKey, PrivKey} = crypto:generate_key(eddsa, ed25519).
{<<184,46,247,95,51,237,199,16,53,26,179,236,67,32,216,
   166,215,236,97,90,244,28,24,161,239,6,82,99,...>>,
 <<86,122,18,144,17,142,65,238,204,99,8,74,168,240,94,215,
   203,73,2,77,229,177,94,126,185,252,190,...>>}
2> {Algorithm, CryptoKey} = {eddsa, [PrivKey, ed25519]}.
{eddsa,[<<86,122,18,144,17,142,65,238,204,99,8,74,168,240,
          94,215,203,73,2,77,229,177,94,126,185,252,...>>,
        ed25519]}
3> DigestType = sha384.
sha384
4> PlainText = <<"abcd">>.
<<"abcd">>
5> Options = [].
[]
6> Signature = crypto:sign(Algorithm, DigestType, PlainText, CryptoKey, Options).
<<116,140,19,21,30,207,166,59,163,84,8,42,166,29,125,229,
  60,207,82,101,211,157,57,170,95,33,27,234,162,...>>
7> {Algorithm, CryptoKey1} = {eddsa, [PubKey, ed25519]}.
{eddsa,[<<184,46,247,95,51,237,199,16,53,26,179,236,67,32,
          216,166,215,236,97,90,244,28,24,161,239,6,...>>,
        ed25519]}
8> crypto:verify(Algorithm, DigestType, PlainText, Signature, CryptoKey1, Options).
true

Discussion