Created
September 13, 2018 08:45
-
-
Save foeken/1469e36dd42a989acc482c719416d1ac to your computer and use it in GitHub Desktop.
ActiveSupport MessageEncryptor Elixir HMAC ExCrypto
This file contains hidden or 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 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