DEV Community

voluntas
voluntas

Posted on

2 1

Erlang/OTP 18.0 で AES-GCM を使う

Erlang/OTP 18.0 から AEAD な AES-GCM と ChaCha20-Poly1305 に対応しています。

AES-GCM は 128/256 です。とりあえず AES-GCM の 256 を試してみます。

crypto:supports/0 で ciphers の中に aes_gcm がいることを確認して下しださい。

chacha20_poly1305 は boringssl でしかまだ対応していないらしく、OpenSSL では非対応です。

AES-GCM

crypto:block_encrypt(aes_gcm, Key, IV, {Nonce, Plain}) で使えます。戻りが {CipherText, CipherTag} なので気をつけてください。

Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.0  (abort with ^G)
> crypto:supports().
[{hashs,[md4,md5,sha,ripemd160,sha224,sha256,sha384,sha512]},
 {ciphers,[des_cbc,des_cfb,des3_cbc,des_ede3,blowfish_cbc,
           blowfish_cfb64,blowfish_ofb64,blowfish_ecb,aes_cbc128,
           aes_cfb8,aes_cfb128,aes_cbc256,rc2_cbc,aes_ctr,rc4,aes_ecb,
           des3_cbf,aes_ige256,aes_gcm]},
 {public_keys,[rsa,dss,dh,srp,ec_gf2m,ecdsa,ecdh]}]
> crypto:block_encrypt(aes_gcm, binary:copy(<<0>>, 32), binary:copy(<<0>>, 24), {<<"">>, <<"1234567890">>}).
{<<41,96,181,164,113,168,138,253,250,8>>,
 <<205,178,48,121,244,0,208,72,72,4,142,93,114,149,83,137>>}
> crypto:block_decrypt(aes_gcm, binary:copy(<<0>>, 32), binary:copy(<<0>>, 24), {<<"">>,  <<41,96,181,164,113,168,138,253,250,8>>, <<205,178,48,121,244,0,208,72,72,4,142,93,114,149,83,137>>}).
<<"1234567890">>
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay