forked from nitrogen/nitrogen_core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_compat.escript
More file actions
executable file
·57 lines (47 loc) · 1.44 KB
/
crypto_compat.escript
File metadata and controls
executable file
·57 lines (47 loc) · 1.44 KB
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
55
56
57
#!/usr/bin/env escript
%% vim: ts=4 sw=4 et ft=erlang
main([]) ->
crypto:start(),
Path = filename:dirname(escript:script_name()),
Filename = Path ++ "/include/crypto_compat.hrl",
io:format("Generating crypto compatibility...~p\n", [Path]),
Encrypt = encrypt(),
Decrypt = decrypt(),
Hash = hash(),
io:format("...Using: ~p~n",[Encrypt]),
io:format("...Using: ~p~n",[Decrypt]),
io:format("...Using: ~p~n",[Hash]),
Contents = [
"-define(WF_ENCRYPT(Key, IV, Data), ",Encrypt,").\n",
"-define(WF_DECRYPT(Key, IV, Data), ",Decrypt,").\n",
"-define(WF_HASH(Data), ",Hash,").\n"
],
ContentsBin = iolist_to_binary(Contents),
case file:read_file(Filename) of
{ok, ContentsBin} ->
io:format("...no changes needed to ~p. Skipping writing new file\n",[Filename]);
_ ->
io:format("...writing ~p\n",[Filename]),
file:write_file(Filename, Contents)
end.
encrypt() ->
case erlang:function_exported(crypto, block_encrypt, 4) of
true ->
"crypto:block_encrypt(aes_cbc128, Key, IV, Data)";
false ->
"crypto:aes_cbc_128_encrypt(Key, IV, Data)"
end.
decrypt() ->
case erlang:function_exported(crypto, block_decrypt, 4) of
true ->
"crypto:block_decrypt(aes_cbc128, Key, IV, Data)";
false ->
"crypto:aes_cbc_128_decrypt(Key, IV, Data)"
end.
hash() ->
case erlang:function_exported(crypto, hash, 2) of
true ->
"crypto:hash(sha, Data)";
false ->
"crypto:sha(Data)"
end.