Skip to content

Instantly share code, notes, and snippets.

@selfup
Created June 15, 2020 14:36
Show Gist options
  • Save selfup/73f15fdb5b5609c20745f4ca29d580a6 to your computer and use it in GitHub Desktop.
Save selfup/73f15fdb5b5609c20745f4ca29d580a6 to your computer and use it in GitHub Desktop.
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