From a417390bb7035eec0d15a616e47302e8c46eecc4 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 19 Mar 2020 15:43:49 +0100 Subject: [PATCH] refactor struct key_search_result from src/key_search_helpers.h to include/eoskeygen/key_search.h --- include/eoskeygen/key_search.h | 11 +++++++++++ src/key_search.cpp | 22 ++++++++++++++++++++-- src/key_search_helpers.cpp | 19 +------------------ src/key_search_helpers.h | 12 ++---------- src/key_search_mt.cpp | 4 ++-- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/include/eoskeygen/key_search.h b/include/eoskeygen/key_search.h index 78c7f6d..19a385d 100644 --- a/include/eoskeygen/key_search.h +++ b/include/eoskeygen/key_search.h @@ -31,6 +31,13 @@ namespace eoskeygen { class KeySearch { +public : + + struct result { + size_t pos; // position where the word was found. + size_t len; // the length of the word. + }; + public : // Add a word to search for. void addWord(const std::string& str); @@ -56,6 +63,10 @@ public : protected : + // Check if any word in appears in 's public key. + // returns true if a word was found (stored in ), false otherwise. + bool _contains_word(const struct ec_keypair* key, struct result& result); + #ifdef HAVE_THREADS void _thr_proc(); diff --git a/src/key_search.cpp b/src/key_search.cpp index a40dbce..8adfc2d 100644 --- a/src/key_search.cpp +++ b/src/key_search.cpp @@ -23,6 +23,7 @@ */ #include #include +#include #include #include "key_search_helpers.h" @@ -63,9 +64,9 @@ void KeySearch::_search_linear(size_t n) { struct ec_keypair pair; while (count < n) { - struct key_result res; + struct result res; ec_generate_key(&pair); - if (key_contains_word(&pair, m_words, &res)) { + if (_contains_word(&pair, res)) { key_search_result(&pair, &res, m_dict); count++; } @@ -85,4 +86,21 @@ void KeySearch::find(size_t num_results) { _search_linear(num_results); } +bool KeySearch::_contains_word(const struct ec_keypair* key, struct result& result) { + + // skip first 3 chars, as those are always "EOS" + std::string pubstr = wif_pub_encode(key->pub).substr(3); + strtolower(pubstr); + + for(auto const& w: m_words) { + size_t p = pubstr.find(w); + if (p != std::string::npos) { + result.pos = p + 3; + result.len = w.length(); + return true; + } + } + return false; +} + } // namespace eoskeygen diff --git a/src/key_search_helpers.cpp b/src/key_search_helpers.cpp index 0ea5608..124d014 100644 --- a/src/key_search_helpers.cpp +++ b/src/key_search_helpers.cpp @@ -37,7 +37,7 @@ static size_t highlight(console::Color color, const std::string& str, size_t pos return len; } -void key_search_result(const struct ec_keypair* key, const struct key_result* result, const Dictionary& dict) { +void key_search_result(const struct ec_keypair* key, const struct KeySearch::result* result, const Dictionary& dict) { std::string pub = wif_pub_encode(key->pub); Dictionary::search_result_t dict_res = dict.search(pub); @@ -66,21 +66,4 @@ void key_search_result(const struct ec_keypair* key, const struct key_result* re << "Private: " << wif_priv_encode(key->secret) << std::endl; } -bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, struct key_result *result) { - - // skip first 3 chars, as those are always "EOS" - std::string pubstr = wif_pub_encode(key->pub).substr(3); - strtolower(pubstr); - - for(auto const& w: word_list) { - size_t p = pubstr.find(w); - if (p != std::string::npos) { - result->pos = p + 3; - result->len = w.length(); - return true; - } - } - return false; -} - } // namespace eoskeygen diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index 2340897..d3dcf0d 100644 --- a/src/key_search_helpers.h +++ b/src/key_search_helpers.h @@ -26,21 +26,13 @@ #include #include +#include namespace eoskeygen { class Dictionary; -struct key_result { - size_t pos; // position where the word was found. - size_t len; // the length of the word. -}; - -void key_search_result(const struct ec_keypair* key, const struct key_result* result, const Dictionary& dict); - -// Check if any word in appears in 's public key. -// returns true if a word was found (stored in ), false otherwise. -bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, struct key_result *result); +void key_search_result(const struct ec_keypair* key, const struct KeySearch::result* result, const Dictionary& dict); } // namespace eoskeygen diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp index 56c2907..1310caa 100644 --- a/src/key_search_mt.cpp +++ b/src/key_search_mt.cpp @@ -46,10 +46,10 @@ void KeySearch::_thr_proc() { struct ec_keypair pair; while (g_count < g_max) { - struct key_result res; + struct result res; ec_generate_key(&pair); - if (key_contains_word(&pair, m_words, &res)) { + if (_contains_word(&pair, res)) { // Guard output with mutex, so we don't get // interrupted mid write and can write to g_count safely.