|
16 | 16 |
|
17 | 17 | -module(rabbit_control_pbe). |
18 | 18 |
|
19 | | --export([encode/7]). |
| 19 | +-export([decode/4, encode/4, list_ciphers/0, list_hashes/0]). |
20 | 20 |
|
21 | 21 | % for testing purposes |
22 | 22 | -export([evaluate_input_as_term/1]). |
23 | 23 |
|
24 | | -encode(ListCiphers, _ListHashes, _Decode, _Cipher, _Hash, _Iterations, _Args) when ListCiphers -> |
25 | | - {ok, io_lib:format("~p", [rabbit_pbe:supported_ciphers()])}; |
| 24 | +list_ciphers() -> |
| 25 | + {ok, io_lib:format("~p", [rabbit_pbe:supported_ciphers()])}. |
26 | 26 |
|
27 | | -encode(_ListCiphers, ListHashes, _Decode, _Cipher, _Hash, _Iterations, _Args) when ListHashes -> |
28 | | - {ok, io_lib:format("~p", [rabbit_pbe:supported_hashes()])}; |
| 27 | +list_hashes() -> |
| 28 | + {ok, io_lib:format("~p", [rabbit_pbe:supported_hashes()])}. |
29 | 29 |
|
30 | | -encode(_ListCiphers, _ListHashes, Decode, Cipher, Hash, Iterations, Args) -> |
31 | | - CipherExists = lists:member(Cipher, rabbit_pbe:supported_ciphers()), |
32 | | - HashExists = lists:member(Hash, rabbit_pbe:supported_hashes()), |
33 | | - encode_encrypt_decrypt(CipherExists, HashExists, Decode, Cipher, Hash, Iterations, Args). |
34 | | - |
35 | | -encode_encrypt_decrypt(CipherExists, _HashExists, _Decode, _Cipher, _Hash, _Iterations, _Args) when CipherExists =:= false -> |
36 | | - {error, io_lib:format("The requested cipher is not supported", [])}; |
37 | | - |
38 | | -encode_encrypt_decrypt(_CipherExists, HashExists, _Decode, _Cipher, _Hash, _Iterations, _Args) when HashExists =:= false -> |
39 | | - {error, io_lib:format("The requested hash is not supported", [])}; |
40 | | - |
41 | | -encode_encrypt_decrypt(_CipherExists, _HashExists, _Decode, _Cipher, _Hash, Iterations, _Args) when Iterations =< 0 -> |
| 30 | +validate(_Cipher, _Hash, Iterations, _Args) when Iterations =< 0 -> |
42 | 31 | {error, io_lib:format("The requested number of iterations is incorrect", [])}; |
| 32 | +validate(_Cipher, _Hash, _Iterations, Args) when length(Args) < 2 -> |
| 33 | + {error, io_lib:format("Please provide a value to encode/decode and a passphrase", [])}; |
| 34 | +validate(_Cipher, _Hash, _Iterations, Args) when length(Args) > 2 -> |
| 35 | + {error, io_lib:format("Too many arguments. Please provide a value to encode/decode and a passphrase", [])}; |
| 36 | +validate(Cipher, Hash, _Iterations, _Args) -> |
| 37 | + case lists:member(Cipher, rabbit_pbe:supported_ciphers()) of |
| 38 | + false -> |
| 39 | + {error, io_lib:format("The requested cipher is not supported", [])}; |
| 40 | + true -> |
| 41 | + case lists:member(Hash, rabbit_pbe:supported_hashes()) of |
| 42 | + false -> |
| 43 | + {error, io_lib:format("The requested hash is not supported", [])}; |
| 44 | + true -> ok |
| 45 | + end |
| 46 | + end. |
43 | 47 |
|
44 | | -encode_encrypt_decrypt(_CipherExists, _HashExists, Decode, Cipher, Hash, Iterations, Args) when length(Args) == 2, Decode =:= false -> |
45 | | - [Value, PassPhrase] = Args, |
46 | | - try begin |
47 | | - TermValue = evaluate_input_as_term(Value), |
48 | | - Result = rabbit_pbe:encrypt_term(Cipher, Hash, Iterations, list_to_binary(PassPhrase), TermValue), |
49 | | - {ok, io_lib:format("~p", [{encrypted, Result}])} |
50 | | - end |
51 | | - catch |
52 | | - _:Msg -> {error, io_lib:format("Error during cipher operation: ~p", [Msg])} |
53 | | - end; |
54 | | - |
55 | | -encode_encrypt_decrypt(_CipherExists, _HashExists, Decode, Cipher, Hash, Iterations, Args) when length(Args) == 2, Decode -> |
56 | | - [Value, PassPhrase] = Args, |
57 | | - try begin |
58 | | - TermValue = evaluate_input_as_term(Value), |
59 | | - TermToDecrypt = case TermValue of |
60 | | - {encrypted, EncryptedTerm} -> |
61 | | - EncryptedTerm; |
62 | | - _ -> |
63 | | - TermValue |
64 | | - end, |
65 | | - Result = rabbit_pbe:decrypt_term(Cipher, Hash, Iterations, list_to_binary(PassPhrase), TermToDecrypt), |
66 | | - {ok, io_lib:format("~p", [Result])} |
67 | | - end |
68 | | - catch |
69 | | - _:Msg -> {error, io_lib:format("Error during cipher operation: ~p", [Msg])} |
70 | | - end; |
| 48 | +encode(Cipher, Hash, Iterations, Args) -> |
| 49 | + case validate(Cipher, Hash, Iterations, Args) of |
| 50 | + {error, Err} -> {error, Err}; |
| 51 | + ok -> |
| 52 | + [Value, PassPhrase] = Args, |
| 53 | + try begin |
| 54 | + TermValue = evaluate_input_as_term(Value), |
| 55 | + Result = rabbit_pbe:encrypt_term(Cipher, Hash, Iterations, |
| 56 | + list_to_binary(PassPhrase), |
| 57 | + TermValue), |
| 58 | + {ok, io_lib:format("~p", [{encrypted, Result}])} |
| 59 | + end |
| 60 | + catch |
| 61 | + _:Msg -> {error, io_lib:format("Error during cipher operation: ~p", [Msg])} |
| 62 | + end |
| 63 | + end. |
71 | 64 |
|
72 | | -encode_encrypt_decrypt(_CipherExists, _HashExists, _Decode, _Cipher, _Hash, _Iterations, _Args) -> |
73 | | - {error, io_lib:format("Please provide a value to encode/decode and a passphrase", [])}. |
| 65 | +decode(Cipher, Hash, Iterations, Args) -> |
| 66 | + case validate(Cipher, Hash, Iterations, Args) of |
| 67 | + {error, Err} -> {error, Err}; |
| 68 | + ok -> |
| 69 | + [Value, PassPhrase] = Args, |
| 70 | + try begin |
| 71 | + TermValue = evaluate_input_as_term(Value), |
| 72 | + TermToDecrypt = case TermValue of |
| 73 | + {encrypted, EncryptedTerm} -> |
| 74 | + EncryptedTerm; |
| 75 | + _ -> |
| 76 | + TermValue |
| 77 | + end, |
| 78 | + Result = rabbit_pbe:decrypt_term(Cipher, Hash, Iterations, |
| 79 | + list_to_binary(PassPhrase), |
| 80 | + TermToDecrypt), |
| 81 | + {ok, io_lib:format("~p", [Result])} |
| 82 | + end |
| 83 | + catch |
| 84 | + _:Msg -> {error, io_lib:format("Error during cipher operation: ~p", [Msg])} |
| 85 | + end |
| 86 | + end. |
74 | 87 |
|
75 | 88 | evaluate_input_as_term(Input) -> |
76 | 89 | {ok,Tokens,_EndLine} = erl_scan:string(Input ++ "."), |
|
0 commit comments