Skip to content

Instantly share code, notes, and snippets.

@seymores
Last active March 8, 2017 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seymores/1699b3f13084f88932383566bb9a6235 to your computer and use it in GitHub Desktop.
Save seymores/1699b3f13084f88932383566bb9a6235 to your computer and use it in GitHub Desktop.
defmodule MyApp.EncryptedField do
@behaviour Ecto.Type
def type, do: :binary
def cast(value) do
{:ok, to_string(value)}
end
# This is called when the field value is about to be written to the database
def dump(value) do
ciphertext = value |> to_string |> MyApp.EncryptionTool.encrypt
{:ok, ciphertext}
end
# This is called when the field is loaded from the database
def load(value) do
{:ok, MyApp.EncryptionTool.decrypt(value)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment