-
-
Save jclem/b8e0547cfed23413856b0ce3a4f618ae 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 API.EncryptedField do | |
@moduledoc """ | |
A field that is encrypted in the database and decrypted when read out of it | |
""" | |
import API.Encryption | |
@behaviour Ecto.Type | |
@spec type() :: :string | |
def type, do: :string | |
@spec cast(any) :: {:ok, String.t} | |
def cast(nil), do: {:ok, nil} | |
def cast(value), do: {:ok, to_string(value)} | |
@spec dump(String.t) :: {:ok, String.t} | |
def dump(nil), do: {:ok, nil} | |
def dump(value), do: {:ok, value |> encrypt} | |
@spec load(String.t) :: {:ok, String.t} | |
def load(nil), do: {:ok, nil} | |
def load(value), do: {:ok, value |> decrypt} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment