-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaes_test.exs
54 lines (40 loc) · 1.46 KB
/
aes_test.exs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule Encryption.AESTest do
use ExUnit.Case
alias Encryption.AES
doctest Encryption.AES
test ".encrypt can encrypt a value" do
assert AES.encrypt("hello") != "hello"
end
test ".encrypt can encrypt a number" do
assert is_binary(AES.encrypt(123))
end
test ".encrypt includes the random IV in the value" do
<<iv::binary-16, ciphertext::binary>> = AES.encrypt("hello")
assert String.length(iv) != 0
assert String.length(ciphertext) != 0
assert is_binary(ciphertext)
end
test ".encrypt includes the key_id in the value" do
<<_iv::binary-16, _tag::binary-16, key_id::unsigned-big-integer-32, _ciphertext::binary>> =
AES.encrypt("hello")
assert key_id == 0
end
test ".encrypt does not produce the same ciphertext twice" do
assert AES.encrypt("hello") != AES.encrypt("hello")
end
test "can decrypt a value" do
plaintext = "hello" |> AES.encrypt() |> AES.decrypt()
assert plaintext == "hello"
end
test "can still decrypt the value after adding a new encryption key" do
encrypted_value = "hello" |> AES.encrypt()
original_keys = Application.get_env(:encryption, Encryption.AES)[:keys]
# add a new key
Application.put_env(:encryption, Encryption.AES,
keys: original_keys ++ [:crypto.strong_rand_bytes(32)]
)
assert "hello" == encrypted_value |> AES.decrypt()
# rollback to the original keys
Application.put_env(:encryption, Encryption.AES, keys: original_keys)
end
end