Created
June 15, 2020 14:36
-
-
Save selfup/73f15fdb5b5609c20745f4ca29d580a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Encrypto do | |
@encrypto_secret_key System.get_env("ENCRYPTO_SECRET_KEY") | |
@encrypto_aad_context System.get_env("ENCRYPTO_AAD_CONTEXT") | |
def encrypt(msg) do | |
decoded_key = Base.decode16!(@encrypto_secret_key) | |
iv = :crypto.strong_rand_bytes(32) | |
{ct, tag} = :crypto.block_encrypt(:aes_gcm, decoded_key, iv, {@encrypto_aad_context, msg}) | |
Base.encode16(iv <> tag <> ct) | |
end | |
def decrypt(msg) do | |
decoded_key = Base.decode16!(@encrypto_secret_key) | |
<<iv::binary-32, tag::binary-16, ct::binary>> = Base.decode16!(msg) | |
:crypto.block_decrypt(:aes_gcm, decoded_key, iv, {@encrypto_aad_context, ct, tag}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment