diff --git a/common/.gitignore b/common/.gitignore new file mode 100644 index 0000000..9375aee --- /dev/null +++ b/common/.gitignore @@ -0,0 +1 @@ +include/eoskeygen/config.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..fa9e2d5 --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,49 @@ +# ------------------------------------------------------------ +# Common CMake file +# +# Compiles the code that should be shared between the cli +# and gui programs into a static library. +# ------------------------------------------------------------ + +# Options +option(USE_THREADS "Compile with support for threads (if available)." ON) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + +# -------------------------------- +# Library +# -------------------------------- +set( COMMON_NAME common ) + +set( COMMON_SOURCE + src/core/file.cpp + src/core/dictionary.cpp + src/core/string.cpp + src/core/strlist.cpp + src/core/leet.cpp + src/key_search.cpp +) + +# Threads support +if (USE_THREADS) + find_package(Threads) + if (Threads_FOUND) + set( EOSIOKEYGEN_HAVE_THREADS TRUE ) + set( COMMON_SOURCE ${COMMON_SOURCE} src/key_search_mt.cpp ) + endif (Threads_FOUND) +endif (USE_THREADS) + +# Project config file +configure_file(src/config.h.in "${CMAKE_CURRENT_LIST_DIR}/include/eoskeygen/config.h" @ONLY) + +add_library( ${COMMON_NAME} STATIC ${COMMON_SOURCE} ) + +target_include_directories( ${COMMON_NAME} PUBLIC include ) + +# Link with libeosio and threads library. +include( libeosio ) +target_link_libraries( ${COMMON_NAME} + PUBLIC + libeosio + ${CMAKE_THREAD_LIBS_INIT} +) diff --git a/common/cmake/libeosio.cmake b/common/cmake/libeosio.cmake new file mode 100644 index 0000000..6427170 --- /dev/null +++ b/common/cmake/libeosio.cmake @@ -0,0 +1,51 @@ +# -------------------------------- +# Variables +# -------------------------------- +set( LIBEOSIO_GIT_URL "https://github.com/eosswedenorg/libeosio.git" ) +set( LIBEOSIO_WANTED_VERSION 0.1.0 ) + +# -------------------------------- +# Macros +# -------------------------------- +macro(fromGit tag) + + message ("Using libeosio from: ${LIBEOSIO_GIT_URL}@${tag}") + + include(FetchContent) + FetchContent_Declare(libeosio + GIT_REPOSITORY ${LIBEOSIO_GIT_URL} + GIT_TAG ${tag} + ) + + FetchContent_GetProperties(libeosio) + if (NOT libeosio_POPULATED) + FetchContent_Populate(libeosio) + add_subdirectory(${libeosio_SOURCE_DIR} ${libeosio_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() +endmacro() + +macro(buildLocal src) + message ("Using local libeosio at: ${src}") + add_subdirectory(${src} ${src}/build EXCLUDE_FROM_ALL) +endmacro() + +# If we have a locallibeosio +if (LIBEOSIO_SOURCE_DIR) + buildLocal( ${LIBEOSIO_SOURCE_DIR} ) +else() + + # Check if version is in fact a version. + if (LIBEOSIO_WANTED_VERSION MATCHES "^[0-9]+(.[0-9]+)?(.[0-9]+)(-[a-zA-Z0-9]+)?$") + # Try finding the package on the system. + find_package(libeosio ${LIBEOSIO_WANTED_VERSION} QUIET) + if (libeoskeygen_FOUND) + message ("Using libeosio in: ${libeosio_DIR}") + # Not found, download from git. + else() + fromGit( v${LIBEOSIO_WANTED_VERSION} ) + endif() + # Assume version contains a git branch. + else() + fromGit( ${LIBEOSIO_WANTED_VERSION} ) + endif() +endif() diff --git a/common/include/eoskeygen/config.h b/common/include/eoskeygen/config.h new file mode 100644 index 0000000..5728f1f --- /dev/null +++ b/common/include/eoskeygen/config.h @@ -0,0 +1,30 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CONFIG_H +#define EOSIOKEYGEN_COMMON_CONFIG_H + +// Defined if we have thread support. +#define EOSIOKEYGEN_HAVE_THREADS + +#endif /* EOSIOKEYGEN_COMMON_CONFIG_H */ diff --git a/common/include/eoskeygen/core/dictionary.h b/common/include/eoskeygen/core/dictionary.h new file mode 100644 index 0000000..3197108 --- /dev/null +++ b/common/include/eoskeygen/core/dictionary.h @@ -0,0 +1,72 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CORE_DICTIONARY_H +#define EOSIOKEYGEN_COMMON_CORE_DICTIONARY_H + +#include +#include +#include + +namespace eoskeygen { + +class Dictionary +{ +public : + // Map that contains position and length for substrings. + // + // key = position in the search string. + // value = length of the word from this position. + typedef std::map< size_t, size_t > search_result_t; + +public : + + // Load words from file. + bool loadFromFile(const std::string& filename); + + // Add a word to the dictionary. + void add(const std::string& word); + + // Add words from another dictionary. + void add(const Dictionary& dictionary); + + // Clear all words from the dictionary. + void clear(); + + // Returns true if word exists in the dictionary. + bool contains(const std::string& word) const; + + // Searches the subject for words defined in the dictionary. + // Returns a search_result_t with the words found in subject. + // See search_result_t for more details. + search_result_t search(const std::string& subject) const; + +protected : + + // Words in the dictionary. + std::set m_words; +}; + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_CORE_DICTIONARY_H */ diff --git a/common/include/eoskeygen/core/file.h b/common/include/eoskeygen/core/file.h new file mode 100644 index 0000000..fffebaa --- /dev/null +++ b/common/include/eoskeygen/core/file.h @@ -0,0 +1,35 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CORE_FILE_H +#define EOSIOKEYGEN_COMMON_CORE_FILE_H + +#include + +namespace eoskeygen { + +bool readLines(const std::string& filename, strlist_t& lines); + +} // namespace + +#endif /* EOSIOKEYGEN_COMMON_CORE_FILE_H */ diff --git a/common/include/eoskeygen/core/leet.h b/common/include/eoskeygen/core/leet.h new file mode 100644 index 0000000..01289fc --- /dev/null +++ b/common/include/eoskeygen/core/leet.h @@ -0,0 +1,36 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CORE_LEET_H +#define EOSIOKEYGEN_COMMON_CORE_LEET_H + +#include +#include + +namespace eoskeygen { + +strlist_t l33twords(std::string str); + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_CORE_LEET_H */ diff --git a/common/include/eoskeygen/core/string.h b/common/include/eoskeygen/core/string.h new file mode 100644 index 0000000..b9ae23a --- /dev/null +++ b/common/include/eoskeygen/core/string.h @@ -0,0 +1,41 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CORE_STRING_H +#define EOSIOKEYGEN_COMMON_CORE_STRING_H + +#include +#include +#include + +namespace eoskeygen { + +std::string& strtolower(std::string& str); + +std::string& rtrim(std::string& str); +std::string& ltrim(std::string& str); +std::string& trim(std::string& str); + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_CORE_STRING_H */ diff --git a/common/include/eoskeygen/core/strlist.h b/common/include/eoskeygen/core/strlist.h new file mode 100644 index 0000000..b1e8d99 --- /dev/null +++ b/common/include/eoskeygen/core/strlist.h @@ -0,0 +1,50 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CORE_STRLIST_H +#define EOSIOKEYGEN_COMMON_CORE_STRLIST_H + +#include +#include + +namespace eoskeygen { + +typedef std::vector strlist_t; + +typedef std::string& (*strlist_stripfunc_t)(std::string& str); + +namespace strlist { + +strlist_t splitw(const std::string& str, const std::string& delim = ","); + +strlist_t split(const std::string& str, const std::string& delim); + +std::string join(const strlist_t& list, const std::string& delim); + +strlist_t& strip(strlist_t& list, strlist_stripfunc_t fn); + +} // namespace strlist + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_CORE_STRLIST_H */ diff --git a/common/include/eoskeygen/key_search.h b/common/include/eoskeygen/key_search.h new file mode 100644 index 0000000..226b9cc --- /dev/null +++ b/common/include/eoskeygen/key_search.h @@ -0,0 +1,104 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_KEY_SEARCH_H +#define EOSIOKEYGEN_COMMON_KEY_SEARCH_H + +#include +#include +#include +#include +#include + +namespace eoskeygen { + +class IKeySearchResult; + +class KeySearch +{ +public : + + struct result { + size_t pos; // position where the word was found. + size_t len; // the length of the word. + }; + +public : + KeySearch(); + + // Add a word to search for. + void addWord(const std::string& str); + + // Add a list of words to search for. + void addList(const strlist_t& list); + + // get the list of words to search for. + const strlist_t& getList(); + + // Clears the search list. + void clear(); + + // Set callback for search result. + void setCallback(IKeySearchResult* callback); + +#ifdef EOSIOKEYGEN_HAVE_THREADS + // Returns the maximum number of threads + // reported by the operating system. + static size_t max_threads(); + + // Set the number of threads to use while searching. + void setThreadCount(size_t num); +#endif /* EOSIOKEYGEN_HAVE_THREADS */ + + // Perform a search. + void find(size_t num_results); + +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 libeosio::ec_keypair* key, struct result& result); + +#ifdef EOSIOKEYGEN_HAVE_THREADS + void _thr_proc(); + + void _search_mt(size_t n); +#endif /* EOSIOKEYGEN_HAVE_THREADS */ + + void _search_linear(size_t n); + +protected : + // List of words to search for. + strlist_t m_words; + +#ifdef EOSIOKEYGEN_HAVE_THREADS + // Number of threads to use. + size_t m_threads; +#endif /* EOSIOKEYGEN_HAVE_THREADS */ + + IKeySearchResult* m_callback; +}; + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_KEY_SEARCH_H */ diff --git a/common/include/eoskeygen/key_search_result.h b/common/include/eoskeygen/key_search_result.h new file mode 100644 index 0000000..943e6d9 --- /dev/null +++ b/common/include/eoskeygen/key_search_result.h @@ -0,0 +1,40 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_KEY_SEARCH_RESULT_H +#define EOSIOKEYGEN_COMMON_KEY_SEARCH_RESULT_H + +#include + +namespace eoskeygen { + +class IKeySearchResult +{ +public : + + virtual void onResult(const struct libeosio::ec_keypair* key, const struct KeySearch::result& result) = 0; +}; + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_COMMON_KEY_SEARCH_RESULT_H */ diff --git a/common/src/config.h.in b/common/src/config.h.in new file mode 100644 index 0000000..7871d9f --- /dev/null +++ b/common/src/config.h.in @@ -0,0 +1,30 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_COMMON_CONFIG_H +#define EOSIOKEYGEN_COMMON_CONFIG_H + +// Defined if we have thread support. +#cmakedefine EOSIOKEYGEN_HAVE_THREADS + +#endif /* EOSIOKEYGEN_COMMON_CONFIG_H */ diff --git a/common/src/core/dictionary.cpp b/common/src/core/dictionary.cpp new file mode 100644 index 0000000..0e74906 --- /dev/null +++ b/common/src/core/dictionary.cpp @@ -0,0 +1,125 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace eoskeygen { + +struct StringContains { + StringContains(const std::string& str, std::vector& pos) : m_str(str), m_pos(pos) {} + bool operator()(const std::string& w) { + for(size_t p = m_str.find(w); p != std::string::npos; p = m_str.find(w, p+1)) { + m_pos.push_back(p); + } + return !m_pos.empty(); + } + std::string m_str; + std::vector& m_pos; +}; + +bool Dictionary::loadFromFile(const std::string& filename) +{ + strlist_t lines; + + // Clear before adding. + clear(); + + if (readLines(filename, lines)) { + + // Read each line and add to the dictionary. + for(auto it = lines.begin(); it != lines.end(); it++) { + add(*it); + } + return true; + } + return false; +} + +void Dictionary::add(const std::string& word) +{ + // Do not insert a empty string. + if (word.length()) { + m_words.insert(word); + } +} + +void Dictionary::add(const Dictionary& dictionary) +{ + std::set_union( + m_words.begin(), m_words.end(), + dictionary.m_words.begin(), dictionary.m_words.end(), + std::inserter(m_words, m_words.begin()) + ); +} + +void Dictionary::clear() +{ + m_words.clear(); +} + +bool Dictionary::contains(const std::string& word) const +{ + return m_words.find(word) != m_words.cend(); +} + +Dictionary::search_result_t Dictionary::search(const std::string& subject) const +{ + search_result_t res; + + std::vector pos; + StringContains pred(subject, pos); + + // Find all words. + for(auto it = std::find_if(m_words.begin(), m_words.end(), pred); + it != m_words.end(); + it = std::find_if(++it, m_words.end(), pred)) { + + // Go through all found positions. + for (auto it2 = pos.begin(); it2 != pos.end(); it2++) { + + // Insert + auto rit = res.find(*it2); + if (rit == res.end()) { + res.emplace(*it2, it->length()); + } + // Update length if it's longer then the previous we found. + else if (rit->second < it->length()) { + rit->second = it->length(); + } + } + + // Clear positions + pos.clear(); + } + + return res; +} + +} // namespace eoskeygen diff --git a/common/src/core/file.cpp b/common/src/core/file.cpp new file mode 100644 index 0000000..ac73356 --- /dev/null +++ b/common/src/core/file.cpp @@ -0,0 +1,49 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include + +namespace eoskeygen { + +bool readLines(const std::string& filename, strlist_t& lines) { + + FILE *fd; + char buf[1024]; + + fd = fopen(filename.c_str(), "r"); + if (!fd) { + return false; + } + + while(fgets(buf, sizeof(buf), fd) != NULL) { + std::string line(buf); + lines.push_back(trim(line)); + } + + fclose(fd); + return true; +} + +} // namespace eoskeygen diff --git a/common/src/core/leet.cpp b/common/src/core/leet.cpp new file mode 100644 index 0000000..bf0e7f1 --- /dev/null +++ b/common/src/core/leet.cpp @@ -0,0 +1,82 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include + +namespace eoskeygen { + +static bool is_l33t(char ch, char& r) { + + // '1', '2', '3', '4', '5', '6', '7', '8', '9' + static char alphabet[9] = { 'l', 'z', 'e', 'a', 's', 'G', 't', 'B', 'g' }; + + for(std::size_t i = 0; i < sizeof(alphabet) / sizeof(char); i++) { + + if (ch == alphabet[i]) { + r = static_cast('1' + i); + return true; + } + } + return false; +} + +static void _l33t(strlist_t& list, const std::string& a, std::size_t pos) { + + // Find the next character to be replaced. + for(std::size_t i = pos; i < a.length(); i++) { + + char ch; + if (is_l33t(a[i], ch)) { + // create a new string and replace the character. + std::string b = a; + b[i] = ch; + + // Store the new string as the result. + list.push_back(b); + + // Perform the same algorithm for both strings + // at the next position. + _l33t(list, a, i + 1); + _l33t(list, b, i + 1); + break; + } + } +} + +strlist_t l33twords(std::string str) { + + strlist_t list; + + // "l" is abit special and are not included in base58 so we set it to 1. + // All other characters in "l33t" are valid. + std::transform(str.begin(), str.end(), str.begin(), [](char c){ return c == 'l' ? '1' : c; }); + + // Store the original string as the first in list. + list.push_back(str); + + _l33t(list, str, 0); + return list; +} + +} // namespace eoskeygen diff --git a/common/src/core/string.cpp b/common/src/core/string.cpp new file mode 100644 index 0000000..164e362 --- /dev/null +++ b/common/src/core/string.cpp @@ -0,0 +1,52 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include + +namespace eoskeygen { + +std::string& strtolower(std::string& str) { + std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); }); + return str; +} + +std::string& ltrim(std::string& str) { + auto it = std::find_if(str.begin(), str.end(), [](char ch){ return !std::isspace(ch); }); + str.erase(str.begin(), it); + return str; +} + +std::string& rtrim(std::string& str) { + auto it = std::find_if(str.rbegin(), str.rend(), [](char ch){ return !std::isspace(ch); }); + str.erase(it.base(), str.end()); + return str; +} + +std::string& trim(std::string& str) { + return ltrim(rtrim(str)); +} + +} // namespace eoskeygen diff --git a/common/src/core/strlist.cpp b/common/src/core/strlist.cpp new file mode 100644 index 0000000..ea12151 --- /dev/null +++ b/common/src/core/strlist.cpp @@ -0,0 +1,75 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include + +namespace eoskeygen { + +strlist_t strlist::splitw(const std::string& str, const std::string& delim) { + + strlist_t words = strlist::split(str, delim); + std::for_each(words.begin(), words.end(), trim); + return words; +} + +strlist_t strlist::split(const std::string& str, const std::string& delim) { + + strlist_t r; + size_t s = 0, e = 0, dlen = delim.length(); + + while((e = str.find(delim, s)) != std::string::npos) { + r.push_back(str.substr(s, e - s)); + s = e + dlen; + } + + r.push_back(str.substr(s)); + return r; +} + +std::string strlist::join(const eoskeygen::strlist_t& list, const std::string& delim) { + + std::string out; + + for(const std::string& item : list) { + if (item.length() < 1) { + continue; + } + out += item + delim; + } + + if (out.length() > 0) { + out.erase(out.end() - delim.length()); + } + + return out; +} + +strlist_t& strlist::strip(strlist_t& list, strlist_stripfunc_t fn) { + + std::transform(list.begin(), list.end(), list.begin(), fn); + return list; +} + +} // namespace eoskeygen diff --git a/common/src/key_search.cpp b/common/src/key_search.cpp new file mode 100644 index 0000000..e97e14a --- /dev/null +++ b/common/src/key_search.cpp @@ -0,0 +1,115 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include + +namespace eoskeygen { + +KeySearch::KeySearch() : +#ifdef EOSIOKEYGEN_HAVE_THREADS + m_threads(0), +#endif + m_callback(NULL) +{ +} + +void KeySearch::addWord(const std::string& str) +{ + std::string tmp = str; + strtolower(tmp); + m_words.push_back(tmp); +} + +void KeySearch::addList(const strlist_t& list) +{ + for(const std::string& item : list) { + addWord(item); + } +} + +const strlist_t& KeySearch::getList() +{ + return m_words; +} + +void KeySearch::clear() +{ + m_words.clear(); +} + +void KeySearch::setCallback(IKeySearchResult* callback) +{ + m_callback = callback; +} + +void KeySearch::_search_linear(size_t n) { + + size_t count = 0; + struct libeosio::ec_keypair pair; + + while (count < n) { + struct result res; + libeosio::ec_generate_key(&pair); + if (_contains_word(&pair, res)) { + m_callback->onResult(&pair, res); + count++; + } + } +} + +void KeySearch::find(size_t num_results) { + +#ifdef EOSIOKEYGEN_HAVE_THREADS + // Only do multithread if number of threads makes sense. + if (m_threads >= 2) { + _search_mt(num_results); + return; + } +#endif /* HAVE_THREADS */ + + _search_linear(num_results); +} + +bool KeySearch::_contains_word(const struct libeosio::ec_keypair* key, struct result& result) { + + // skip first 3 chars, as those are always "EOS" + std::string pubstr = libeosio::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/common/src/key_search_mt.cpp b/common/src/key_search_mt.cpp new file mode 100644 index 0000000..2e498ff --- /dev/null +++ b/common/src/key_search_mt.cpp @@ -0,0 +1,106 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include + +namespace eoskeygen { + +// Max keys to search for, +std::size_t g_max; + +// How many keys we have found so far. +std::size_t g_count; + +// Mutex guard for g_count. +std::mutex g_count_mtx; + +// Thread process. +void KeySearch::_thr_proc() { + + struct libeosio::ec_keypair pair; + + while (g_count < g_max) { + struct result res; + + libeosio::ec_generate_key(&pair); + if (_contains_word(&pair, res)) { + + // Guard output with mutex, so we don't get + // interrupted mid write and can write to g_count and res safely. + const std::lock_guard lock(g_count_mtx); + + // It is possible g_count was updated by another thread + // after we checked it in the while loop. + // So while we have the lock, we need to check it again. + if (g_count >= g_max) { + return; + } + + // Update count and call result function. + g_count++; + m_callback->onResult(&pair, res); + } + } +} + +void KeySearch::setThreadCount(size_t num) +{ + m_threads = num; +} + +size_t KeySearch::max_threads() +{ + return std::thread::hardware_concurrency(); +} + +void KeySearch::_search_mt(size_t n) +{ + std::vector t; + + t.resize(m_threads - 1); + + // Setup global variables for the threads. + g_max = n; + g_count = 0; + + // Launch them. + for(std::size_t i = 0; i < t.size(); i++) { + t[i] = std::thread(&KeySearch::_thr_proc, this); + } + + // Use main thread for 1 search + _thr_proc(); + + // Wait for all threads to compelete. + for(std::size_t i = 0; i < t.size(); i++) { + t[i].join(); + } +} + +} // namespace eoskeygen