-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathencrypted_field.ex
38 lines (31 loc) · 1.17 KB
/
encrypted_field.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule Encryption.EncryptedField do
# alias our AES encrypt & decrypt functions (3.1 & 3.2)
alias Encryption.AES
# Check this module conforms to Ecto.type behavior.
@behaviour Ecto.Type
# :binary is the data type ecto uses internally
def type, do: :binary
# cast/1 simply calls to_string on the value and returns a "success" tuple
def cast(value) do
{:ok, to_string(value)}
end
# dump/1 is called when the field value is about to be written to the database
def dump(value) do
ciphertext = value |> to_string |> AES.encrypt()
# ciphertext is :binary type (no conversion required)
{:ok, ciphertext}
end
# load/1 is called when the field is loaded from the database
def load(value) do
{:ok, AES.decrypt(value)}
end
# load/1 is called when the field is loaded from the database
def load(value, _key_id) do
{:ok, AES.decrypt(value)}
end
# embed_as/1 dictates how the type behaves when embedded (:self or :dump)
# preserve the type's higher level representation
def embed_as(_), do: :self
# equal?/2 is called to determine if two field values are semantically equal
def equal?(value1, value2), do: value1 == value2
end