diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-27 12:56:13 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-27 12:56:13 +0300 |
commit | 1db94ae5d2201edb5f9421c2c30be049efc678f6 (patch) | |
tree | cb67b8f1e8a04dda4de297cc011dc9de57d1e824 /utils/decrypt_block.cpp | |
parent | README update (diff) | |
download | aes-tools-1db94ae5d2201edb5f9421c2c30be049efc678f6.tar.gz aes-tools-1db94ae5d2201edb5f9421c2c30be049efc678f6.zip |
utils: refactor command line parsing
Diffstat (limited to 'utils/decrypt_block.cpp')
-rw-r--r-- | utils/decrypt_block.cpp | 118 |
1 files changed, 40 insertions, 78 deletions
diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index 30409e0..a8c9625 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -13,7 +13,6 @@ #include <boost/program_options.hpp> -#include <deque> #include <exception> #include <iostream> #include <iterator> @@ -23,40 +22,31 @@ namespace { template <aesni::Algorithm algorithm, aesni::Mode mode> void decrypt_with_mode( - const std::string& key_str, - std::deque<std::string>& ciphertexts, + const Input& input, bool verbose = false) { typename aesni::Types<algorithm>::Block iv; if (aesni::ModeRequiresInitializationVector<mode>()) { - if (ciphertexts.empty()) - throw_iv_required(); - - aesni::from_string<algorithm>(iv, ciphertexts.front()); - ciphertexts.pop_front(); - + aesni::from_string<algorithm>(iv, input.get_iv_string()); if (verbose) dump_iv<algorithm>(iv); } typename aesni::Types<algorithm>::Key key; - aesni::from_string<algorithm>(key, key_str); - + aesni::from_string<algorithm>(key, input.get_key_string()); if (verbose) dump_key<algorithm>(key); aesni::DecryptWrapper<algorithm, mode> decrypt(key, iv); - if (verbose) dump_wrapper<algorithm, mode>(decrypt); - while (!ciphertexts.empty()) + for (const auto& input_block_string : input.get_input_block_strings()) { typename aesni::Types<algorithm>::Block ciphertext, plaintext; - aesni::from_string<algorithm>(ciphertext, ciphertexts.front()); - ciphertexts.pop_front(); + aesni::from_string<algorithm>(ciphertext, input_block_string); decrypt.decrypt_block(ciphertext, plaintext); @@ -68,7 +58,7 @@ namespace } else { - std::cout << aesni::to_string<algorithm>(plaintext) << "\n"; + std::cout << aesni::to_string<algorithm>(plaintext) << '\n'; } } } @@ -76,30 +66,29 @@ namespace template <aesni::Algorithm algorithm> void decrypt_with_algorithm( aesni::Mode mode, - const std::string& key_str, - std::deque<std::string>& ciphertexts, + const Input& input, bool verbose = false) { switch (mode) { case AESNI_ECB: - decrypt_with_mode<algorithm, AESNI_ECB>(key_str, ciphertexts, verbose); + decrypt_with_mode<algorithm, AESNI_ECB>(input, verbose); break; case AESNI_CBC: - decrypt_with_mode<algorithm, AESNI_CBC>(key_str, ciphertexts, verbose); + decrypt_with_mode<algorithm, AESNI_CBC>(input, verbose); break; case AESNI_CFB: - decrypt_with_mode<algorithm, AESNI_CFB>(key_str, ciphertexts, verbose); + decrypt_with_mode<algorithm, AESNI_CFB>(input, verbose); break; case AESNI_OFB: - decrypt_with_mode<algorithm, AESNI_OFB>(key_str, ciphertexts, verbose); + decrypt_with_mode<algorithm, AESNI_OFB>(input, verbose); break; case AESNI_CTR: - decrypt_with_mode<algorithm, AESNI_CTR>(key_str, ciphertexts, verbose); + decrypt_with_mode<algorithm, AESNI_CTR>(input, verbose); break; default: @@ -111,22 +100,21 @@ namespace void decrypt_using_cxx_api( aesni::Algorithm algorithm, aesni::Mode mode, - const std::string& key_str, - std::deque<std::string>& ciphertexts, + const Input& input, bool verbose = false) { switch (algorithm) { case AESNI_AES128: - decrypt_with_algorithm<AESNI_AES128>(mode, key_str, ciphertexts, verbose); + decrypt_with_algorithm<AESNI_AES128>(mode, input, verbose); break; case AESNI_AES192: - decrypt_with_algorithm<AESNI_AES192>(mode, key_str, ciphertexts, verbose); + decrypt_with_algorithm<AESNI_AES192>(mode, input, verbose); break; case AESNI_AES256: - decrypt_with_algorithm<AESNI_AES256>(mode, key_str, ciphertexts, verbose); + decrypt_with_algorithm<AESNI_AES256>(mode, input, verbose); break; default: @@ -137,46 +125,41 @@ namespace void decrypt_using_particular_box( aesni::Box& box, - std::deque<std::string>& ciphertexts) + const std::vector<std::string>& input_block_strings) { - while (!ciphertexts.empty()) + for (const auto& input_block_string : input_block_strings) { aesni::Box::Block ciphertext; - box.parse_block(ciphertext, ciphertexts.front()); - ciphertexts.pop_front(); + box.parse_block(ciphertext, input_block_string); aesni::Box::Block plaintext; box.decrypt_block(ciphertext, plaintext); - - std::cout << box.format_block(plaintext) << "\n"; + std::cout << box.format_block(plaintext) << '\n'; } } void decrypt_using_boxes( aesni::Algorithm algorithm, aesni::Mode mode, - const std::string& key_str, - std::deque<std::string>& ciphertexts) + const Input& input) { aesni::Box::Key key; - aesni::Box::parse_key(key, algorithm, key_str); + aesni::Box::parse_key(key, algorithm, input.get_key_string()); if (aesni::mode_requires_initialization_vector(mode)) { - if (ciphertexts.empty()) - throw_iv_required(); - aesni::Box::Block iv; - aesni::Box::parse_block(iv, algorithm, ciphertexts.front()); - ciphertexts.pop_front(); + aesni::Box::parse_block(iv, algorithm, input.get_iv_string()); decrypt_using_particular_box( - aesni::Box(algorithm, key, mode, iv), ciphertexts); + aesni::Box(algorithm, key, mode, iv), + input.get_input_block_strings()); } else { decrypt_using_particular_box( - aesni::Box(algorithm, key), ciphertexts); + aesni::Box(algorithm, key), + input.get_input_block_strings()); } } } @@ -188,53 +171,32 @@ int main(int argc, char** argv) CommandLineParser cmd_parser(argv[0]); try { - cmd_parser.parse(argc, argv); + Settings settings; + std::vector<Input> inputs; + cmd_parser.parse(settings, argc, argv, inputs); - if (cmd_parser.requested_help()) + if (cmd_parser.exit_with_usage()) { std::cout << cmd_parser; return 0; } - std::deque<std::string> args( - std::make_move_iterator(cmd_parser.args.begin()), - std::make_move_iterator(cmd_parser.args.end())); - - while (!args.empty()) + for (const auto& input : inputs) { - const auto key = args.front(); - args.pop_front(); - - std::deque<std::string> ciphertexts; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - ciphertexts.push_back(args.front()); - args.pop_front(); - } - - if (cmd_parser.use_boxes) + if (settings.use_boxes()) { decrypt_using_boxes( - cmd_parser.algorithm, - cmd_parser.mode, - key, - ciphertexts); + settings.get_algorithm(), + settings.get_mode(), + input); } else { decrypt_using_cxx_api( - cmd_parser.algorithm, - cmd_parser.mode, - key, - ciphertexts, - cmd_parser.verbose); + settings.get_algorithm(), + settings.get_mode(), + input, + settings.verbose()); } } |