diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a108da7..9d8b29d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,14 +26,9 @@ jobs: mkdir -p build if [ "$RUNNER_OS" == "macOS" ]; then brew install openssl - elif [ "$RUNNER_OS" == "Windows" ]; then - cd build - curl https://mirror.firedaemon.com/OpenSSL/openssl-1.1.1e-dev.zip -O - 7z x openssl-1.1.1e-dev.zip fi - - name: Configure (nix) - if: runner.os != 'Windows' + - name: Configure shell: bash run: | if [ "$RUNNER_OS" == "macOS" ]; then @@ -41,11 +36,6 @@ jobs: fi cd build && cmake ${SSL_OPTS} ${{matrix.build-opts}} .. - - name: Configure (win) - if: runner.os == 'Windows' - # cmake in windows bash can't find openssl. so we use windows cli. - run: cd build; cmake ${{matrix.build-opts}} -D OPENSSL_ROOT_DIR="$pwd\openssl-1.1\x64" .. - - name: Build shell: bash run: cmake --build build diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index fecd67b..18c0e6b 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -43,14 +43,6 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Dependancies - shell: bash - run: | - mkdir -p build - cd build - curl https://mirror.firedaemon.com/OpenSSL/openssl-1.1.1e-dev.zip -O - 7z x openssl-1.1.1e-dev.zip - - name: Configure run: | if ("${{matrix.arch}}" -eq "x86") { diff --git a/CMakeLists.txt b/CMakeLists.txt index 5192dfb..b6bc750 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ option(FORCE_ANSI "Force ANSI console colors even on windows" OFF) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules") +include(libeoskeygen) + # Use installpath from GNUInstallDirs as default. include(GNUInstallDirs) @@ -45,15 +47,9 @@ endif() set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE - src/core/file.cpp - src/core/dictionary.cpp - src/core/string.cpp - src/core/isatty.cpp - src/crypto/base58.cpp - src/crypto/WIF.cpp + src/isatty.cpp + src/cli_key_search_result.cpp src/console.cpp - src/key_search.cpp - src/key_search_helpers.cpp src/benchmark.cpp src/main.cpp ) @@ -65,36 +61,13 @@ else() set (PROGRAM_SOURCE ${PROGRAM_SOURCE} src/console_ansi.cpp) endif() -# Libraries -find_package(OpenSSL 1.1 REQUIRED) -set (PROGRAM_SOURCE ${PROGRAM_SOURCE} - src/crypto/openssl/ec.cpp - src/crypto/openssl/hash.cpp -) - -if (USE_THREADS) - find_package(Threads) - if (Threads_FOUND) - # Add preprocessor flag - add_definitions( "-DHAVE_THREADS=1" ) - set (PROGRAM_SOURCE ${PROGRAM_SOURCE} src/key_search_mt.cpp) - endif (Threads_FOUND) -endif (USE_THREADS) - # Project config file configure_file(src/config.h.in "${PROJECT_BINARY_DIR}/config.h" @ONLY) include_directories(${PROJECT_BINARY_DIR}) -# Include OpenSSL headers -include_directories( ${OPENSSL_INCLUDE_DIR} ) - add_executable( ${PROGRAM_EXE} ${PROGRAM_SOURCE} ) -target_link_libraries( ${PROGRAM_EXE} - PUBLIC - ${OPENSSL_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} -) +target_link_libraries( ${PROGRAM_EXE} PUBLIC eoskeygen ) # -------------------------------- # Install @@ -102,26 +75,6 @@ target_link_libraries( ${PROGRAM_EXE} install(TARGETS ${PROGRAM_EXE} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -# Win32 specific. -if (WIN32) - if (CMAKE_SIZEOF_VOID_P EQUAL 8) - set (OPENSSL_LIBCRYPTO_NAME libcrypto-1_1-x64) - else() - set (OPENSSL_LIBCRYPTO_NAME libcrypto-1_1) - endif() - set( OPENSSL_LIBCRYPTO_DLL ${OPENSSL_ROOT_DIR}/bin/${OPENSSL_LIBCRYPTO_NAME}.dll ) - - # Need to copy libcrypto dll to binary folder. - add_custom_command(TARGET ${PROGRAM_EXE} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${OPENSSL_LIBCRYPTO_DLL} - ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR} - ) - - # We also need to copy libcrypto dll during install - install(FILES ${OPENSSL_LIBCRYPTO_DLL} DESTINATION ${CMAKE_INSTALL_BINDIR}) -endif (WIN32) - # Readme and license install(FILES README.md LICENSE LICENSE.bitcoin DESTINATION ${CMAKE_INSTALL_SHAREDIR}) @@ -141,6 +94,6 @@ endif (UNIX) set( CPACK_DEBIAN_PACKAGE_PRIORITY "optional" ) set( CPACK_DEBIAN_PACKAGE_SECTION "misc" ) -set( CPACK_DEBIAN_PACKAGE_DEPENDS "libssl1.1, libc6" ) +set( CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON ) include( cpack_custom ) diff --git a/CMakeModules/cpack_custom.cmake b/CMakeModules/cpack_custom.cmake index 6a23457..d4fad9b 100644 --- a/CMakeModules/cpack_custom.cmake +++ b/CMakeModules/cpack_custom.cmake @@ -104,7 +104,7 @@ set( CPACK_DEBIAN_FILE_NAME # -------------------------------- -# Defult to gzip tar on unix. zip otherwise. +# Default to gzip tar on unix. zip otherwise. if (NOT CPACK_GENERATOR) if (UNIX) set( CPACK_GENERATOR "TGZ" ) diff --git a/CMakeModules/libeoskeygen.cmake b/CMakeModules/libeoskeygen.cmake new file mode 100644 index 0000000..d58803f --- /dev/null +++ b/CMakeModules/libeoskeygen.cmake @@ -0,0 +1,52 @@ + +# -------------------------------- +# Variables +# -------------------------------- +set( LIBEOSKEYGEN_GIT_URL "https://github.com/eosswedenorg/libeoskeygen.git" ) +set( LIBEOSKEYGEN_WANTED_VERSION 0.1.1 ) + +# -------------------------------- +# Macros +# -------------------------------- +macro(fromGit tag) + + message ("Using libeoskeygen from: ${LIBEOSKEYGEN_GIT_URL}@${tag}") + + include(FetchContent) + FetchContent_Declare(libeoskeygen + GIT_REPOSITORY ${LIBEOSKEYGEN_GIT_URL} + GIT_TAG ${tag} + ) + + FetchContent_GetProperties(libeoskeygen) + if (NOT libeoskeygen_POPULATED) + FetchContent_Populate(libeoskeygen) + add_subdirectory(${libeoskeygen_SOURCE_DIR} ${libeoskeygen_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() +endmacro() + +macro(buildLocal src) + message ("Using local libeoskeygen at: ${src}") + add_subdirectory(${src} ${src}/build EXCLUDE_FROM_ALL) +endmacro() + +# If we have a local libeoskeygen +if (LIBEOSKEYGEN_SOURCE_DIR) + buildLocal( ${LIBEOSKEYGEN_SOURCE_DIR} ) +else() + + # Check if version is in fact a version. + if (LIBEOSKEYGEN_WANTED_VERSION MATCHES "^[0-9]+(.[0-9]+)?(.[0-9]+)(-[a-zA-Z0-9]+)?$") + # Try finding the package on the system. + find_package(libeoskeygen ${LIBEOSKEYGEN_WANTED_VERSION} QUIET) + if (libeoskeygen_FOUND) + message ("Using libeoskeygen in: ${libeoskeygen_DIR}") + # Not found, download from git. + else() + fromGit( v${LIBEOSKEYGEN_WANTED_VERSION} ) + endif() + # Assume version contains a git branch. + else() + fromGit( ${LIBEOSKEYGEN_WANTED_VERSION} ) + endif() +endif() diff --git a/README.md b/README.md index 069af15..3b72adf 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This program generates public and private keypair for [EOS](https://eos.io/) ## Compile -You will need `openssl` development files (version 1.1 or later) to compile and `cmake 3.15` or later to compile this project. +You will need `libeoskeygen` and `cmake 3.15` or later to compile this project. ### Linux/MacOS @@ -18,11 +18,37 @@ You will need `openssl` development files (version 1.1 or later) to compile and #### Linux **Ubuntu:** + +First you need to have a compiler. this can be installed with apt. + ```sh -$ apt-get install gcc g++ libssl-dev +$ apt-get install gcc g++ ``` -**Other**: Consult the manual for you package manager. +You then need `libeoskeygen`. + +This can be installed from [EOS Sweden's APT Repository](https://eosswedenorg.github.io/apt) like this: + +```sh +$ sudo apt-get install software-properties-common +$ curl https://apt.eossweden.org/key 2> /dev/null | sudo apt-key add - +$ sudo apt-add-repository -y 'deb [arch=amd64] https://apt.eossweden.org/main `lsb_release -cs` stable' +$ sudo apt-get install libeoskeygen-dev +``` + +or manually via `.deb` file from [github](https://github.com/eosswedenorg/libeoskeygen/releases) + +```sh +$ wget +$ sudo apt install ./libeoskeygen-dev-.deb +``` + +**Other**: + +Consult the manual for you package manager. +`libeoskeygen` will be downloaded and compiled if it's not installed automatically (however this is slower). + +Consult [libeoskeygen's github](https://github.com/eosswedenorg/libeoskeygen) if you want to compile and install it manually. **CMake** @@ -48,13 +74,15 @@ Other methods is documanted at https://cmake.org/download You must have a compiler installed. This project is known to build with `Xcode 11.0` but other versions should work. -You need to have opessl and cmake installed also, this can be done with this `brew` command: +You need to have cmake installed also, this can be done with this `brew` command: ```sh -$ brew install openssl cmake +$ brew install cmake ``` If you need a newer version of cmake, you can download the official `.dmg` file: [cmake-3.15.7-Darwin-x86_64.dmg](https://github.com/Kitware/CMake/releases/download/v3.15.7/cmake-3.15.7-Darwin-x86_64.dmg). or see https://cmake.org/download for other versions. +`libeoskeygen` needs to be compiled and installed from source. [Go here](https://github.com/eosswedenorg/libeoskeygen) + #### Build Run `./build.sh` to trigger cmake. @@ -73,13 +101,12 @@ $ cmake .. && make #### Dependencies -Download and install `cmake` version `3.15` or newer from [cmake.org](https://cmake.org) and download -[openssl](https://mirror.firedaemon.com/OpenSSL/openssl-1.1.1e-dev.zip) - -unpack `openssl-1.1.1e-dev.zip` somewhere on the filesystem. +Download and install `cmake` version `3.15` or newer from [cmake.org](https://cmake.org) You will also need a compiler. [Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16) (Selecting C++ during installation) is recommended. + + #### Build. you need to set `OPENSSL_ROOT_DIR` to the directory where you unpacked @@ -103,7 +130,6 @@ These compile options are available: | Cmake | build.sh | Description | |--------------------------- | ----------------- | ------------------------------------------| | -DCMAKE_BUILD_TYPE=`value` | -t `value` | Type of build | -| -DUSE_THREADS=`OFF` | --disable-threads | Disable thread support | | -DFORCE_ANSI=`ON` | --force-ansi | Force ANSI console colors even on windows | For more details about options run `./build.sh -l` or `mkdir build && cmake build -LA` diff --git a/src/benchmark.cpp b/src/benchmark.cpp index bc8e69f..43d8b35 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "crypto/ec.h" +#include #include "benchmark.h" using std::chrono::steady_clock; diff --git a/src/key_search_helpers.cpp b/src/cli_key_search_result.cpp similarity index 68% rename from src/key_search_helpers.cpp rename to src/cli_key_search_result.cpp index 661b6e0..b61ea3a 100644 --- a/src/key_search_helpers.cpp +++ b/src/cli_key_search_result.cpp @@ -22,10 +22,10 @@ * SOFTWARE. */ #include -#include "core/dictionary.h" -#include "crypto/WIF.h" +#include +#include #include "console.h" -#include "key_search_helpers.h" +#include "cli_key_search_result.h" namespace eoskeygen { @@ -37,19 +37,24 @@ 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) { +CliKeySearchResult::CliKeySearchResult(const Dictionary& dict) : +m_dict (dict) +{ +} + +void CliKeySearchResult::onResult(const struct ec_keypair* key, const struct KeySearch::result& result) { std::string pub = wif_pub_encode(key->pub); - Dictionary::search_result_t dict_res = dict.search(pub); + Dictionary::search_result_t dict_res = m_dict.search(pub); std::cout << "----" << std::endl; - std::cout << "Found: " << pub.substr(result->pos, result->len) << std::endl; + std::cout << "Found: " << pub.substr(result.pos, result.len) << std::endl; std::cout << "Public: EOS"; for(size_t i = 3; i < pub.length(); ) { - if (i == result->pos) { - i += highlight(console::red, pub, result->pos, result->len); + if (i == result.pos) { + i += highlight(console::red, pub, result.pos, result.len); continue; } @@ -66,21 +71,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/cli_key_search_result.h similarity index 71% rename from src/key_search_helpers.h rename to src/cli_key_search_result.h index 9b2862c..2645aeb 100644 --- a/src/key_search_helpers.h +++ b/src/cli_key_search_result.h @@ -24,24 +24,27 @@ #ifndef EOSIOKEYGEN_KEY_SEARCH_HELPERS_H #define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H -#include "core/string.h" -#include "crypto/types.h" +#include +#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. +class CliKeySearchResult : public IKeySearchResult +{ +public: + CliKeySearchResult(const Dictionary& dict); + + virtual void onResult(const struct ec_keypair* key, const struct KeySearch::result& result); + +protected : + + const Dictionary& m_dict; }; -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); - } // namespace eoskeygen #endif /* EOSIOKEYGEN_KEY_SEARCH_HELPERS_H */ diff --git a/src/console.cpp b/src/console.cpp index 99472f3..2208856 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "core/isatty.h" +#include "isatty.h" #include "console.h" namespace eoskeygen { namespace console { diff --git a/src/core/dictionary.cpp b/src/core/dictionary.cpp deleted file mode 100644 index ffa8b89..0000000 --- a/src/core/dictionary.cpp +++ /dev/null @@ -1,102 +0,0 @@ - -#include -#include -#include -#include -#include "file.h" -#include "string.h" -#include "dictionary.h" - -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/src/core/dictionary.h b/src/core/dictionary.h deleted file mode 100644 index 903b3a5..0000000 --- a/src/core/dictionary.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * 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_CORE_DICTIONARY_H -#define EOSIOKEYGEN_CORE_DICTIONARY_H - -#include -#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 - void add(const std::string& word); - - // Add words from another dictionary. - void add(const Dictionary& 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_CORE_DICTIONARY_H */ diff --git a/src/core/file.cpp b/src/core/file.cpp deleted file mode 100644 index 72c01b8..0000000 --- a/src/core/file.cpp +++ /dev/null @@ -1,26 +0,0 @@ - -#include -#include "file.h" - -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/src/core/file.h b/src/core/file.h deleted file mode 100644 index 72cb5ab..0000000 --- a/src/core/file.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * 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_CORE_FILE_H -#define EOSIOKEYGEN_CORE_FILE_H - -#include "string.h" -#include - -namespace eoskeygen { - -bool readLines(const std::string& filename, strlist_t& lines); - -} // namespace - -#endif /* EOSIOKEYGEN_CORE_FILE_H */ diff --git a/src/core/string.cpp b/src/core/string.cpp deleted file mode 100644 index f9f0da6..0000000 --- a/src/core/string.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/** - * 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 "../crypto/base58.h" -#include "string.h" - -namespace eoskeygen { - -strlist_t strsplitwords(const std::string& str, const std::string& delim) { - - strlist_t words = strsplit(str, delim); - std::for_each(words.begin(), words.end(), trim); - return words; -} - -strlist_t strsplit(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 strjoin(const 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; -} - -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)); -} - -strlist_t& base58_strip(strlist_t& list) { - - std::transform(list.begin(), list.end(), list.begin(), [](std::string& str){ return base58_strip(str); }); - return list; -} - -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/src/core/string.h b/src/core/string.h deleted file mode 100644 index 1662c7b..0000000 --- a/src/core/string.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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_CORE_STRING_H -#define EOSIOKEYGEN_CORE_STRING_H - -#include -#include - -namespace eoskeygen { - -typedef std::vector strlist_t; - -strlist_t strsplitwords(const std::string& str, const std::string& delim = ","); - -strlist_t strsplit(const std::string& str, const std::string& delim); - -std::string strjoin(const strlist_t& list, const std::string& delim); - -std::string& strtolower(std::string& str); - -std::string& rtrim(std::string& str); -std::string& ltrim(std::string& str); -std::string& trim(std::string& str); - -strlist_t& base58_strip(strlist_t& list); - -strlist_t l33twords(std::string str); - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CORE_STRING_H */ diff --git a/src/crypto/WIF.cpp b/src/crypto/WIF.cpp deleted file mode 100644 index d31aff4..0000000 --- a/src/crypto/WIF.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/** - * 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 "base58.h" -#include "checksum.h" -#include "WIF.h" - -namespace eoskeygen { - -#define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ - -std::string wif_priv_encode(ec_privkey_t priv) { - - checksum_t check; - // 1 byte extra for prefix. - unsigned char buf[1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE] = { PRIV_KEY_PREFIX }; - - memcpy(buf + 1, priv.data(), priv.size()); - - // Checksum - check = checksum_sha256d(buf, 1 + EC_PRIVKEY_SIZE); - memcpy(buf + 1 + EC_PRIVKEY_SIZE, check.data(), check.size()); - - return base58_encode(buf, buf + sizeof(buf)); -} - -std::string wif_pub_encode(ec_pubkey_t pub) { - - checksum_t check = checksum_ripemd160(pub.data(), pub.size()); - unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE]; - - memcpy(buf, pub.data(), pub.size()); - memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); - - return "EOS" + base58_encode(buf, buf + sizeof(buf)); -} - -void wif_print_key(const struct ec_keypair *key) { - - std::cout << "Public: " << wif_pub_encode(key->pub) << std::endl; - std::cout << "Private: " << wif_priv_encode(key->secret) << std::endl; -} - -} // namespace eoskeygen diff --git a/src/crypto/WIF.h b/src/crypto/WIF.h deleted file mode 100644 index 14a1855..0000000 --- a/src/crypto/WIF.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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_CRYPTO_WIF_H -#define EOSIOKEYGEN_CRYPTO_WIF_H - -#include -#include "types.h" - -namespace eoskeygen { - -std::string wif_priv_encode(ec_privkey_t priv); - -std::string wif_pub_encode(ec_pubkey_t pub); - -void wif_print_key(const struct ec_keypair *key); - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CRYPTO_WIF_H */ diff --git a/src/crypto/base58.cpp b/src/crypto/base58.cpp deleted file mode 100644 index a1d112f..0000000 --- a/src/crypto/base58.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2009-2019 The Bitcoin Core developers - * Copyright (c) 2009-2019 Bitcoin Developers - * - * 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. - * - * Based on code from https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp - */ -#include -#include -#include -#include "base58.h" - -namespace eoskeygen { - -static const char charmap[59] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - -std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend) { - - // Skip & count leading zeroes. - int zeroes = 0; - int length = 0; - while (pbegin != pend && *pbegin == 0) { - pbegin++; - zeroes++; - } - // Allocate enough space in big-endian base58 representation. - std::size_t size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up. - std::vector b58(size); - // Process the bytes. - while (pbegin != pend) { - int carry = *pbegin; - int i = 0; - // Apply "b58 = b58 * 256 + ch". - for (std::vector::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { - carry += 256 * (*it); - *it = static_cast(carry % 58); - carry /= 58; - } - - assert(carry == 0); - length = i; - pbegin++; - } - // Skip leading zeroes in base58 result. - std::vector::iterator it = b58.begin() + (size - length); - while (it != b58.end() && *it == 0) - it++; - // Translate the result into a string. - std::string str; - str.reserve(zeroes + (b58.end() - it)); - str.assign(zeroes, '1'); - while (it != b58.end()) - str += charmap[*(it++)]; - return str; -} - -std::string base58_encode(const std::string& str) { - - const unsigned char *ptr = (const unsigned char *) str.c_str(); - return base58_encode(ptr, ptr + str.length()); -} - -std::string base58_encode(const std::vector& vch) { - - return base58_encode(vch.data(), vch.data() + vch.size()); -} - -bool is_base58(char ch) { - for(unsigned int i=0; i < sizeof(charmap); i++) { - if (ch == charmap[i]) { - return true; - } - } - return false; -} - -size_t is_base58(const std::string& str) { - - auto p = std::find_if_not(str.begin(), str.end(), static_cast(is_base58)); - - if (p == str.end()) { - return std::string::npos; - } - return p - str.begin(); -} - -std::string& base58_strip(std::string &str) { - str.erase(std::remove_if(str.begin(), str.end(), [] (std::string::value_type ch) - { return is_base58(ch) == false; } - ), str.end()); - return str; -} - -} // namespace eoskeygen diff --git a/src/crypto/base58.h b/src/crypto/base58.h deleted file mode 100644 index 4d52f31..0000000 --- a/src/crypto/base58.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * 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_CRYPTO_BASE58_H -#define EOSIOKEYGEN_CRYPTO_BASE58_H - -#include -#include - -namespace eoskeygen { - -std::string base58_encode(const std::string& str); -std::string base58_encode(const std::vector& vch); -std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend); - -bool is_base58(char ch); - -// Returns std::string::npos if the string contains only base58 characters -// Otherwise the position of the first non base58 character is returned. -size_t is_base58(const std::string& str); - -std::string& base58_strip(std::string& str); - -} //namespace eoskeygen - -#endif /* EOSIOKEYGEN_CRYPTO_BASE58_H */ diff --git a/src/crypto/checksum.h b/src/crypto/checksum.h deleted file mode 100644 index a5f48f8..0000000 --- a/src/crypto/checksum.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 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_CRYPTO_EOS_CHECKSUM_H -#define EOSIOKEYGEN_CRYPTO_EOS_CHECKSUM_H - -#include -#include -#include -#include "hash.h" - -namespace eoskeygen { - -#define CHECKSUM_SIZE 4 - -typedef std::array checksum_t; - -template -inline checksum_t checksum(const unsigned char* data, std::size_t len) { - checksum_t crc; - T hash; - - F(data, len, &hash); - std::memcpy(crc.data(), &hash, crc.size()); - return crc; -} - -#define checksum_sha256 checksum -#define checksum_sha256d checksum -#define checksum_ripemd160 checksum - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CHECKSUM_H */ diff --git a/src/crypto/ec.h b/src/crypto/ec.h deleted file mode 100644 index e8558dd..0000000 --- a/src/crypto/ec.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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_CRYPTO_EC_H -#define EOSIOKEYGEN_CRYPTO_EC_H - -#include "types.h" - -namespace eoskeygen { - -/** - * Generates a keypair using the secp256k1 curve. - * public key is in compressed format. - */ -int ec_generate_key(struct ec_keypair *pair); - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CRYPTO_EC_H */ diff --git a/src/crypto/hash.h b/src/crypto/hash.h deleted file mode 100644 index 783ba57..0000000 --- a/src/crypto/hash.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * 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_CRYPTO_HASH_H -#define EOSIOKEYGEN_CRYPTO_HASH_H - -#include -#include "types.h" - -namespace eoskeygen { - -sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); - -// sha256 double. -sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); - -ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CRYPTO_HASH_H */ diff --git a/src/crypto/openssl/ec.cpp b/src/crypto/openssl/ec.cpp deleted file mode 100644 index 4b43a70..0000000 --- a/src/crypto/openssl/ec.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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 "../ec.h" - -namespace eoskeygen { - -int ec_generate_key(struct ec_keypair *pair) { - - int ret = -1; - EC_KEY *k; - BN_CTX *ctx; - - // Create BIGNUM context. - ctx = BN_CTX_new(); - if (ctx == NULL) { - return -1; - } - - // Construct curve. - k = EC_KEY_new_by_curve_name(NID_secp256k1); - if (k == NULL) { - goto fail1; - } - - // Generate new key pair. - if (EC_KEY_generate_key(k) != 1) { - goto fail2; - } - - // Copy private key to binary format. - EC_KEY_priv2oct(k, pair->secret.data(), EC_PRIVKEY_SIZE); - - // Copy public key - EC_POINT_point2oct(EC_KEY_get0_group(k), - EC_KEY_get0_public_key(k), POINT_CONVERSION_COMPRESSED, - pair->pub.data(), EC_PUBKEY_SIZE, ctx); - - ret = 0; -fail2: - EC_KEY_free(k); -fail1: - BN_CTX_free(ctx); - return ret; -} - -} // namespace eoskeygen diff --git a/src/crypto/openssl/hash.cpp b/src/crypto/openssl/hash.cpp deleted file mode 100644 index 424c46a..0000000 --- a/src/crypto/openssl/hash.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/** - * 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 "../hash.h" - -namespace eoskeygen { - -sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { - return (sha256_t *) SHA256(data, len, out->data); -} - -sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out) { - SHA256(data, len, out->data); - return (sha256_t *) SHA256(out->data, 32, out->data); -} - -ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out) { - return (ripemd160_t *) RIPEMD160(data, len, out->data); -} - -} // namespace eoskeygen diff --git a/src/crypto/types.h b/src/crypto/types.h deleted file mode 100644 index 23e2585..0000000 --- a/src/crypto/types.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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_CRYPTO_TYPES_H -#define EOSIOKEYGEN_CRYPTO_TYPES_H - -#include - -namespace eoskeygen { - -#define EC_PRIVKEY_SIZE 32 - -/* - * Compressed format! - * z||x, where byte z specifies which (of the 2) solutions of the quadratic equation y is. - * Each cordinate is 32 bytes. - */ -#define EC_PUBKEY_SIZE (32 + 1) - -typedef std::array ec_privkey_t; -typedef std::array ec_pubkey_t; - -struct ec_keypair { - ec_privkey_t secret; - ec_pubkey_t pub; -}; - -// Hashes. - -typedef struct { unsigned char data[20]; } ripemd160_t; -typedef struct { unsigned char data[32]; } sha256_t; - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_CRYPTO_TYPES_H */ diff --git a/src/core/isatty.cpp b/src/isatty.cpp similarity index 100% rename from src/core/isatty.cpp rename to src/isatty.cpp diff --git a/src/core/isatty.h b/src/isatty.h similarity index 100% rename from src/core/isatty.h rename to src/isatty.h diff --git a/src/key_search.cpp b/src/key_search.cpp deleted file mode 100644 index 7c99a94..0000000 --- a/src/key_search.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/** - * 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 "crypto/ec.h" -#include "key_search_helpers.h" -#include "key_search.h" - -namespace eoskeygen { - -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); - } -} - -void KeySearch::addDictionary(const Dictionary& dictionary) -{ - m_dict = dictionary; -} - -const strlist_t& KeySearch::getList() -{ - return m_words; -} - -void KeySearch::clear() -{ - m_words.clear(); -} - -void KeySearch::_search_linear(size_t n) { - - size_t count = 0; - struct ec_keypair pair; - - while (count < n) { - struct key_result res; - ec_generate_key(&pair); - if (key_contains_word(&pair, m_words, &res)) { - key_search_result(&pair, &res, m_dict); - count++; - } - } -} - -void KeySearch::find(size_t num_results) { - -#ifdef 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); -} - -} // namespace eoskeygen diff --git a/src/key_search.h b/src/key_search.h deleted file mode 100644 index da26772..0000000 --- a/src/key_search.h +++ /dev/null @@ -1,82 +0,0 @@ -/** - * 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_KEY_SEARCH_H -#define EOSIOKEYGEN_KEY_SEARCH_H - -#include "core/dictionary.h" -#include "string.h" - -namespace eoskeygen { - -class KeySearch -{ -public : - // 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); - - void addDictionary(const Dictionary& dictionary); - - // get the list of words to search for. - const strlist_t& getList(); - - // Clears the search list. - void clear(); - -#ifdef HAVE_THREADS - // Set the number of threads to use while searching. - void setThreadCount(size_t num); -#endif /* HAVE_THREADS */ - - // Perform a search. - void find(size_t num_results); - -protected : - -#ifdef HAVE_THREADS - void _thr_proc(); - - void _search_mt(size_t n); -#endif /* HAVE_THREADS */ - - void _search_linear(size_t n); - -protected : - // List of words to search for. - strlist_t m_words; - - // Dictionary to use when we find a search result. - Dictionary m_dict; - -#ifdef HAVE_THREADS - // Number of threads to use. - size_t m_threads; -#endif /* HAVE_THREADS */ -}; - -} // namespace eoskeygen - -#endif /* EOSIOKEYGEN_KEY_SEARCH_H */ diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp deleted file mode 100644 index 35fcbfe..0000000 --- a/src/key_search_mt.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/** - * 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 "crypto/ec.h" -#include "key_search_helpers.h" -#include "key_search.h" - -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 ec_keypair pair; - - while (g_count < g_max) { - struct key_result res; - - ec_generate_key(&pair); - if (key_contains_word(&pair, m_words, &res)) { - - // Guard output with mutex, so we don't get - // interrupted mid write and can write to g_count 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 print result. - g_count++; - key_search_result(&pair, &res, m_dict); - } - } -} - -void KeySearch::setThreadCount(size_t num) -{ - m_threads = num; -} - -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 diff --git a/src/main.cpp b/src/main.cpp index 7fb38ee..384d2fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,16 +26,17 @@ #endif /* HAVE_THREADS */ #include #include -#include "config.h" -#include "core/file.h" -#include "core/dictionary.h" -#include "core/string.h" -#include "crypto/base58.h" -#include "crypto/ec.h" -#include "crypto/WIF.h" +#include +#include +#include +#include +#include +#include +#include +#include "cli_key_search_result.h" #include "console.h" -#include "key_search.h" #include "benchmark.h" +#include "config.h" // Command line options. bool option_l33t = false; @@ -47,8 +48,9 @@ int option_num_threads = std::thread::hardware_concurrency(); int cmd_search(const eoskeygen::strlist_t& words, const eoskeygen::Dictionary& dict, int count) { eoskeygen::KeySearch ks; + eoskeygen::CliKeySearchResult rs(dict); - ks.addDictionary(dict); + ks.setCallback(&rs); for(auto it = words.begin(); it != words.end(); it++) { size_t p = eoskeygen::is_base58(*it);