Skip to content

Instantly share code, notes, and snippets.

@foeken
Created September 13, 2018 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foeken/1469e36dd42a989acc482c719416d1ac to your computer and use it in GitHub Desktop.
Save foeken/1469e36dd42a989acc482c719416d1ac to your computer and use it in GitHub Desktop.
ActiveSupport MessageEncryptor Elixir HMAC ExCrypto
defmodule ActiveSupportMessageEncryptor do
def encrypt(map, key) do
clear_text = Poison.encode!(map)
{:ok, aes_256_key} = Base.decode64(key)
{:ok, {iv, cipher_text}} = ExCrypto.encrypt(aes_256_key, clear_text)
payload = [cipher_text, iv] |> Enum.map(fn x -> Base.encode64(x) end) |> Enum.join("--") |> Base.encode64()
digest = :crypto.hmac(:sha, aes_256_key, payload) |> Base.encode16(case: :lower)
[payload, digest] |> Enum.join("--")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment