-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencrypt.erl
36 lines (28 loc) · 1.1 KB
/
encrypt.erl
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
-module(encrypt).
-export([encrypt/1, decrypt/1]).
-define(SECRET_KEY_FILE, "secret.key").
-define(MODE, aes_gcm).
-define(AAD, <<"AES256GCM">>).
-define(IV, <<"ab">>).
encrypt(Msg) when is_binary(Msg) ->
{CipherText, CipherTag} = crypto:block_encrypt(?MODE, get_secret_key(), ?IV, {?AAD, Msg, 16}),
list_to_binary([CipherTag, CipherText]).
decrypt(<<Tag:16/binary, CipherText/binary>>) ->
crypto:block_decrypt(?MODE, get_secret_key(), ?IV, {?AAD, CipherText, Tag}).
get_secret_key() ->
% Check if file exists
EncodedKey = case filelib:is_regular(?SECRET_KEY_FILE) of
true ->
{ok, Contents} = file:read_file(?SECRET_KEY_FILE),
Contents;
_ ->
% If not generate key
Contents = generate_secret_key(),
file:write_file(?SECRET_KEY_FILE, Contents),
Contents
end,
% decode
base64:decode(EncodedKey).
generate_secret_key() ->
Key = crypto:strong_rand_bytes(16),
base64:encode(Key).