From 188460a91bd37ca6f18fb89e448819b642953be7 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 May 2017 04:08:15 +0300 Subject: code style & refactoring --- utils/block_cmd_parser.hpp | 90 +++++++++++++++++++++++----------------------- utils/block_dumper.hpp | 29 +++++++-------- utils/block_input.hpp | 36 ++++++------------- utils/data_parsers.hpp | 6 ++-- utils/decrypt_block.cpp | 22 ++++++------ utils/encrypt_block.cpp | 14 ++++---- utils/file_cmd_parser.hpp | 14 ++++---- 7 files changed, 96 insertions(+), 115 deletions(-) (limited to 'utils') diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 70452e3..9c9d61c 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -13,8 +13,8 @@ #include #include -#include #include +#include #include #include #include @@ -86,9 +86,9 @@ namespace po::notify(vm); - parse_inputs(settings, inputs, std::deque( + inputs = parse_inputs(settings, std::deque{ std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end()))); + std::make_move_iterator(args.end())}); return settings; } @@ -96,57 +96,55 @@ namespace bool exit_with_usage() const { return help_flag; } private: - static void parse_inputs( + static std::vector parse_inputs( const Settings& settings, - std::vector& inputs, std::deque&& args) { + std::vector inputs; while (!args.empty()) + inputs.emplace_back(parse_input(settings, args)); + return inputs; + } + + static Input parse_input( + const Settings& settings, + std::deque& args) + { + std::string key{std::move(args.front())}; + args.pop_front(); + + std::string iv; + + if (aes::mode_requires_init_vector(settings.mode)) { - auto key_string = std::move(args.front()); + if (args.empty()) + throw boost::program_options::error{"an initialization vector is required for the selected mode of operation"}; + iv = std::move(args.front()); args.pop_front(); + } - std::string iv_string; - - if (aes::mode_requires_init_vector(settings.mode)) - { - if (args.empty()) - { - throw boost::program_options::error( - "an initialization vector is required for the selected mode of operation"); - } - iv_string = std::move(args.front()); - args.pop_front(); - } - - std::vector input_block_strings; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - input_block_strings.emplace_back(std::move(args.front())); - args.pop_front(); - } - - if (aes::mode_requires_init_vector(settings.mode)) - { - inputs.emplace_back( - std::move(key_string), - std::move(iv_string), - std::move(input_block_strings)); - } - else - { - inputs.emplace_back( - std::move(key_string), - std::move(input_block_strings)); - } + auto blocks = parse_blocks(args); + + if (aes::mode_requires_init_vector(settings.mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; + } + + static std::vector parse_blocks(std::deque& args) + { + std::vector blocks; + + while (!args.empty()) + { + std::string block{std::move(args.front())}; + args.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); } + + return blocks; } const std::string prog_name; diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index df5b818..ff28c75 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -15,9 +15,12 @@ namespace { template - void dump_block(const char* name, const typename aes::Types::Block& block) + void dump_block( + const char* header, + const typename aes::Types::Block& block) { - std::cout << name << ": " << aes::to_string(block) << "\n" << aes::to_matrix_string(block) << "\n"; + std::cout << header << ": " << aes::to_string(block) << "\n"; + std::cout << aes::to_matrix_string(block) << "\n"; } template @@ -45,9 +48,9 @@ namespace } template - void dump_round_keys(const char* name, const typename aes::Types::RoundKeys& round_keys) + void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) { - std::cout << name << ":\n"; + std::cout << header << ":\n"; for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; std::cout << "\n"; @@ -66,40 +69,34 @@ namespace } template - void dump_wrapper( - const aes::EncryptWrapper& wrapper) + void dump_wrapper(const aes::EncryptWrapper& wrapper) { dump_encryption_keys(wrapper.encryption_keys); } template - void dump_wrapper( - const aes::DecryptWrapper& wrapper) + void dump_wrapper(const aes::DecryptWrapper& wrapper) { dump_decryption_keys(wrapper.decryption_keys); } template ::value>::type* = nullptr> - void dump_next_iv( - const aes::EncryptWrapper& wrapper) + void dump_next_iv(const aes::EncryptWrapper& wrapper) { dump_block("Next initialization vector", wrapper.iv); } template ::value>::type* = nullptr> - void dump_next_iv( - const aes::EncryptWrapper&) + void dump_next_iv(const aes::EncryptWrapper&) { } template ::value>::type* = nullptr> - void dump_next_iv( - const aes::DecryptWrapper& wrapper) + void dump_next_iv(const aes::DecryptWrapper& wrapper) { dump_block("Next initialization vector", wrapper.iv); } template ::value>::type* = nullptr> - void dump_next_iv( - const aes::DecryptWrapper&) + void dump_next_iv(const aes::DecryptWrapper&) { } } diff --git a/utils/block_input.hpp b/utils/block_input.hpp index 0bcabbb..549e3f2 100644 --- a/utils/block_input.hpp +++ b/utils/block_input.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include namespace @@ -13,34 +14,19 @@ namespace class Input { public: - Input( - const std::string& key_string, - const std::string& iv_string, - const std::vector& input_block_strings) - : key_string(key_string) - , iv_string(iv_string) - , input_block_strings(input_block_strings) + Input(const std::string& key, const std::string& iv, std::vector&& blocks) + : key{key} + , iv{iv} + , blocks{std::move(blocks)} { } - Input( - const std::string& key_string, - const std::vector& input_block_strings) - : key_string(key_string) - , input_block_strings(input_block_strings) + Input(const std::string& key, std::vector&& blocks) + : key{key} + , blocks{std::move(blocks)} { } - const std::string& get_key_string() const { return key_string; } - - const std::string& get_iv_string() const { return iv_string; } - - const std::vector& get_input_block_strings() const - { - return input_block_strings; - } - - private: - const std::string key_string; - const std::string iv_string; - const std::vector input_block_strings; + const std::string key; + const std::string iv; + const std::vector blocks; }; } diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp index 33c766a..e4f0ae2 100644 --- a/utils/data_parsers.hpp +++ b/utils/data_parsers.hpp @@ -11,15 +11,15 @@ #include #include -#include #include +#include static std::istream& operator>>(std::istream& is, aes::Mode& dest) { std::string src; is >> src; - static std::map lookup_table = + static const std::unordered_map lookup_table = { {"ecb", AES_ECB}, {"cbc", AES_CBC}, @@ -42,7 +42,7 @@ static std::istream& operator>>(std::istream& is, aes::Algorithm& dest) std::string src; is >> src; - static std::map lookup_table = + static const std::unordered_map lookup_table = { {"aes128", AES_AES128}, {"aes192", AES_AES192}, diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index 4744d49..34f8abe 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -27,13 +27,13 @@ namespace if (aes::ModeRequiresInitVector()) { - aes::from_string(iv, input.get_iv_string()); + aes::from_string(iv, input.iv); if (verbose) dump_iv(iv); } typename aes::Types::Key key; - aes::from_string(key, input.get_key_string()); + aes::from_string(key, input.key); if (verbose) dump_key(key); @@ -41,10 +41,10 @@ namespace if (verbose) dump_wrapper(decrypt); - for (const auto& input_block_string : input.get_input_block_strings()) + for (const auto& block : input.blocks) { typename aes::Types::Block ciphertext, plaintext; - aes::from_string(ciphertext, input_block_string); + aes::from_string(ciphertext, block); decrypt.decrypt_block(ciphertext, plaintext); @@ -123,12 +123,12 @@ namespace void decrypt_using_particular_box( aes::Box& box, - const std::vector& input_block_strings) + const std::vector& blocks) { - for (const auto& input_block_string : input_block_strings) + for (const auto& block : blocks) { aes::Box::Block ciphertext; - box.parse_block(ciphertext, input_block_string); + box.parse_block(ciphertext, block); aes::Box::Block plaintext; box.decrypt_block(ciphertext, plaintext); @@ -142,20 +142,20 @@ namespace const Input& input) { aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.get_key_string()); + aes::Box::parse_key(key, algorithm, input.key); if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, input.get_iv_string()); + aes::Box::parse_block(iv, algorithm, input.iv); aes::Box box{algorithm, key, mode, iv}; - decrypt_using_particular_box(box, input.get_input_block_strings()); + decrypt_using_particular_box(box, input.blocks); } else { aes::Box box{algorithm, key}; - decrypt_using_particular_box(box, input.get_input_block_strings()); + decrypt_using_particular_box(box, input.blocks); } } } diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp index 340e18b..5c02d92 100644 --- a/utils/encrypt_block.cpp +++ b/utils/encrypt_block.cpp @@ -27,13 +27,13 @@ namespace if (aes::ModeRequiresInitVector::value) { - aes::from_string(iv, input.get_iv_string()); + aes::from_string(iv, input.iv); if (verbose) dump_iv(iv); } typename aes::Types::Key key; - aes::from_string(key, input.get_key_string()); + aes::from_string(key, input.key); if (verbose) dump_key(key); @@ -41,7 +41,7 @@ namespace if (verbose) dump_wrapper(encrypt); - for (const auto& input_block_string : input.get_input_block_strings()) + for (const auto& input_block_string : input.blocks) { typename aes::Types::Block plaintext, ciphertext; aes::from_string(plaintext, input_block_string); @@ -142,20 +142,20 @@ namespace const Input& input) { aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.get_key_string()); + aes::Box::parse_key(key, algorithm, input.key); if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, input.get_iv_string()); + aes::Box::parse_block(iv, algorithm, input.iv); aes::Box box{algorithm, key, mode, iv}; - encrypt_using_particular_box(box, input.get_input_block_strings()); + encrypt_using_particular_box(box, input.blocks); } else { aes::Box box{algorithm, key}; - encrypt_using_particular_box(box, input.get_input_block_strings()); + encrypt_using_particular_box(box, input.blocks); } } } diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index 0a6423f..e9cdaae 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -52,13 +52,13 @@ namespace namespace po = boost::program_options; options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value(&settings.mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&settings.algorithm)->required(), "set algorithm") - ("input-path,i", po::value(&settings.input_path)->required(), "set input file path") - ("output-path,o", po::value(&settings.output_path)->required(), "set output file path") - ("key,k", po::value(&settings.key)->required(), "set encryption key") - ("iv,v", po::value(&settings.iv), "set initialization vector"); + ("help,h", "show this message and exit") + ("mode,m", po::value(&settings.mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&settings.algorithm)->required(), "set algorithm") + ("input,i", po::value(&settings.input_path)->required(), "set input file path") + ("output,o", po::value(&settings.output_path)->required(), "set output file path") + ("key,k", po::value(&settings.key)->required(), "set encryption key") + ("iv,v", po::value(&settings.iv), "set initialization vector"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, options), vm); -- cgit v1.2.3