mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-06-18 04:00:03 +02:00
cmake: split CMakeLists into multiple directories.
This commit is contained in:
parent
7c2629427d
commit
b52f61d319
35 changed files with 98 additions and 99 deletions
54
cli/src/benchmark.cpp
Normal file
54
cli/src/benchmark.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* 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 <chrono>
|
||||
#include "crypto/ec.h"
|
||||
#include "benchmark.h"
|
||||
|
||||
using std::chrono::steady_clock;
|
||||
using std::chrono::duration;
|
||||
using std::chrono::time_point;
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
void benchmark(size_t num_keys, struct benchmark_result* res) {
|
||||
|
||||
time_point<steady_clock> start;
|
||||
|
||||
if (num_keys < 1) {
|
||||
res->sec = res->kps = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
start = steady_clock::now();
|
||||
|
||||
for(size_t i = 0; i < num_keys; i++) {
|
||||
struct ec_keypair k;
|
||||
ec_generate_key(&k);
|
||||
}
|
||||
|
||||
res->sec = duration<float>(steady_clock::now() - start).count();
|
||||
res->kps = static_cast<float>(num_keys) / res->sec;
|
||||
}
|
||||
|
||||
} // namespace eoskeygen
|
||||
40
cli/src/benchmark.h
Normal file
40
cli/src/benchmark.h
Normal file
|
|
@ -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_BENCHMARK_H
|
||||
#define EOSIOKEYGEN_BENCHMARK_H
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
struct benchmark_result {
|
||||
float sec; // elapsed seconds.
|
||||
float kps; // keys per second.
|
||||
};
|
||||
|
||||
void benchmark(size_t num_keys, struct benchmark_result* res);
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_BENCHMARK_H */
|
||||
34
cli/src/config.h.in
Normal file
34
cli/src/config.h.in
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* 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_CONFIG_H
|
||||
#define EOSIOKEYGEN_CONFIG_H
|
||||
|
||||
#define PROGRAM_NAME "@PROJECT_NAME@"
|
||||
#define PROGRAM_VERSION "@PROJECT_VERSION@"
|
||||
|
||||
// Paths
|
||||
#define CONFIG_SHARE_PATH "@CMAKE_INSTALL_DATADIR@/@CMAKE_PROJECT_NAME@"
|
||||
#define CONFIG_SHARE_FULL_PATH "@CMAKE_INSTALL_FULL_DATADIR@/@CMAKE_PROJECT_NAME@"
|
||||
|
||||
#endif /* EOSIOKEYGEN_CONFIG_H */
|
||||
48
cli/src/console.cpp
Normal file
48
cli/src/console.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* 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 <iostream>
|
||||
#include "core/isatty.h"
|
||||
#include "console.h"
|
||||
|
||||
namespace eoskeygen { namespace console {
|
||||
|
||||
bool disable_color = false;
|
||||
|
||||
FILE* _getFileFromStream(const std::ostream& os) {
|
||||
|
||||
if (&os == &std::cout) {
|
||||
return stdout;
|
||||
} else if (&os == &std::cerr) {
|
||||
return stderr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool isColorsSupported(const std::ostream& os) {
|
||||
|
||||
FILE* fd = _getFileFromStream(os);
|
||||
return disable_color == false && isatty(fd);
|
||||
}
|
||||
|
||||
} } // namespace eoskeygen::console
|
||||
91
cli/src/console.h
Normal file
91
cli/src/console.h
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* 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_CONSOLE_H
|
||||
#define EOSIOKEYGEN_CONSOLE_H
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
namespace console {
|
||||
|
||||
extern bool disable_color;
|
||||
|
||||
// enum for all supported colors.
|
||||
enum Color {
|
||||
default_fg,
|
||||
black,
|
||||
white,
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
yellow,
|
||||
magenta,
|
||||
cyan,
|
||||
|
||||
// Light colors.
|
||||
light_grey,
|
||||
light_red,
|
||||
light_green,
|
||||
light_blue,
|
||||
light_yellow,
|
||||
light_magenta,
|
||||
light_cyan,
|
||||
|
||||
// Dark colors
|
||||
dark_grey
|
||||
};
|
||||
|
||||
enum Attribute {
|
||||
normal,
|
||||
bold,
|
||||
italic
|
||||
};
|
||||
|
||||
bool isColorsSupported(const std::ostream& os);
|
||||
|
||||
// Resets all colors/attributes
|
||||
std::ostream& reset(std::ostream& os);
|
||||
|
||||
// Foreground color
|
||||
// Defined as a class with overloaded "<<" operator so you can write:
|
||||
// std::cout << fg(red) << "Text";
|
||||
class fg
|
||||
{
|
||||
public:
|
||||
fg(Color color, Attribute attribute = normal)
|
||||
: _color(color), _attr(attribute) {}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const fg& obj);
|
||||
|
||||
protected :
|
||||
Color _color;
|
||||
Attribute _attr;
|
||||
};
|
||||
|
||||
} // namespace console
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_CONSOLE_H */
|
||||
83
cli/src/console_ansi.cpp
Normal file
83
cli/src/console_ansi.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/**
|
||||
* 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 <iostream>
|
||||
#include "console.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
namespace console {
|
||||
|
||||
std::ostream& reset(std::ostream& os) {
|
||||
|
||||
if (!isColorsSupported(os)) {
|
||||
return os;
|
||||
}
|
||||
return os << "\033[0m";
|
||||
}
|
||||
|
||||
// Foreground
|
||||
std::ostream& operator<<(std::ostream& os, const fg& obj) {
|
||||
|
||||
int attr;
|
||||
int code;
|
||||
|
||||
if (!isColorsSupported(os)) {
|
||||
return os;
|
||||
}
|
||||
|
||||
switch(obj._color) {
|
||||
case black : code = 30; break;
|
||||
case red : code = 31; break;
|
||||
case green : code = 32; break;
|
||||
case yellow : code = 33; break;
|
||||
case blue : code = 34; break;
|
||||
case magenta : code = 35; break;
|
||||
case cyan : code = 36; break;
|
||||
case light_grey : code = 37; break;
|
||||
case dark_grey : code = 90; break;
|
||||
case light_red : code = 91; break;
|
||||
case light_green : code = 92; break;
|
||||
case light_yellow : code = 93; break;
|
||||
case light_blue : code = 94; break;
|
||||
case light_magenta : code = 95; break;
|
||||
case light_cyan : code = 96; break;
|
||||
case white : code = 97; break;
|
||||
case default_fg : default :
|
||||
code = 39;
|
||||
}
|
||||
|
||||
switch(obj._attr) {
|
||||
|
||||
case bold : attr = 1; break;
|
||||
case italic : attr = 2; break;
|
||||
case normal : default :
|
||||
attr = 0;
|
||||
}
|
||||
|
||||
return os << "\033[" << attr << ";" << code << "m";
|
||||
}
|
||||
|
||||
} // namespace console
|
||||
|
||||
} // namespace eoskeygen
|
||||
108
cli/src/console_win32.cpp
Normal file
108
cli/src/console_win32.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/**
|
||||
* 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 <windows.h>
|
||||
#include <iostream>
|
||||
#include "console.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
// WinAPI colors
|
||||
#define FG_BLACK 0
|
||||
#define FG_BLUE FOREGROUND_BLUE
|
||||
#define FG_GREEN FOREGROUND_GREEN
|
||||
#define FG_RED FOREGROUND_RED
|
||||
#define FG_CYAN (FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
#define FG_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
|
||||
#define FG_MAGENTA (FOREGROUND_RED | FOREGROUND_BLUE)
|
||||
#define FG_DARKGREY FOREGROUND_INTENSITY
|
||||
#define FG_GREY (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
#define FG_LIGHTBLUE (FOREGROUND_INTENSITY | FOREGROUND_BLUE)
|
||||
#define FG_LIGHTGREEN (FOREGROUND_INTENSITY | FOREGROUND_GREEN)
|
||||
#define FG_LIGHTCYAN (FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
#define FG_LIGHTRED (FOREGROUND_INTENSITY | FOREGROUND_RED)
|
||||
#define FG_LIGHTMAGENTA (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE)
|
||||
#define FG_LIGHTYELLOW (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN)
|
||||
#define FG_WHITE (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
#define FG_DEFAULT FG_GREY
|
||||
|
||||
namespace console {
|
||||
|
||||
HANDLE _getNativeHandle(const std::ostream& os) {
|
||||
|
||||
if (&os == &std::cout) {
|
||||
return ::GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
} else if (&os == &std::cerr) {
|
||||
return ::GetStdHandle(STD_ERROR_HANDLE);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::ostream& reset(std::ostream& os) {
|
||||
|
||||
if (isColorsSupported(os)) {
|
||||
::SetConsoleTextAttribute(_getNativeHandle(os), FG_DEFAULT);
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
// Foreground
|
||||
std::ostream& operator<<(std::ostream& os, const fg& obj) {
|
||||
|
||||
int code;
|
||||
|
||||
if (isColorsSupported(os)) {
|
||||
|
||||
switch(obj._color) {
|
||||
case black : code = FG_BLACK; break;
|
||||
case red : code = FG_RED; break;
|
||||
case green : code = FG_GREEN; break;
|
||||
case blue : code = FG_BLUE; break;
|
||||
case yellow : code = FG_YELLOW; break;
|
||||
case magenta : code = FG_MAGENTA; break;
|
||||
case cyan : code = FG_CYAN; break;
|
||||
case light_grey : code = FG_GREY; break;
|
||||
case light_red : code = FG_LIGHTRED; break;
|
||||
case light_green : code = FG_LIGHTGREEN; break;
|
||||
case light_yellow : code = FG_LIGHTYELLOW; break;
|
||||
case light_blue : code = FG_LIGHTBLUE; break;
|
||||
case light_magenta : code = FG_LIGHTMAGENTA; break;
|
||||
case light_cyan : code = FG_LIGHTCYAN; break;
|
||||
case dark_grey : code = FG_DARKGREY; break;
|
||||
case white : code = FG_WHITE; break;
|
||||
case default_fg : default :
|
||||
code = FG_DEFAULT;
|
||||
}
|
||||
|
||||
::SetConsoleTextAttribute(_getNativeHandle(os), code);
|
||||
}
|
||||
|
||||
// WinAPI does not support text attributes in the console.
|
||||
// so we ignore those.
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace console
|
||||
|
||||
} // namespace eoskeygen
|
||||
102
cli/src/core/dictionary.cpp
Normal file
102
cli/src/core/dictionary.cpp
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include "file.h"
|
||||
#include "string.h"
|
||||
#include "dictionary.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
struct StringContains {
|
||||
StringContains(const std::string& str, std::vector<size_t>& 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<size_t>& 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<size_t> 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
|
||||
72
cli/src/core/dictionary.h
Normal file
72
cli/src/core/dictionary.h
Normal file
|
|
@ -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_CORE_DICTIONARY_H
|
||||
#define EOSIOKEYGEN_CORE_DICTIONARY_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
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<std::string> m_words;
|
||||
};
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_CORE_DICTIONARY_H */
|
||||
26
cli/src/core/file.cpp
Normal file
26
cli/src/core/file.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
#include <cstdio>
|
||||
#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
|
||||
36
cli/src/core/file.h
Normal file
36
cli/src/core/file.h
Normal file
|
|
@ -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_CORE_FILE_H
|
||||
#define EOSIOKEYGEN_CORE_FILE_H
|
||||
|
||||
#include "string.h"
|
||||
#include <vector>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
bool readLines(const std::string& filename, strlist_t& lines);
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif /* EOSIOKEYGEN_CORE_FILE_H */
|
||||
21
cli/src/core/isatty.cpp
Normal file
21
cli/src/core/isatty.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#if _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#define _isatty isatty
|
||||
#define _fileno fileno
|
||||
#endif
|
||||
#include "isatty.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
bool isatty(int fd) {
|
||||
return ::_isatty(fd);
|
||||
}
|
||||
|
||||
bool isatty(FILE* fd) {
|
||||
// fileno() segfaults if fd is null.
|
||||
return fd ? isatty(_fileno(fd)) : false;
|
||||
}
|
||||
|
||||
} // namespace eoskeygen
|
||||
37
cli/src/core/isatty.h
Normal file
37
cli/src/core/isatty.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* 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_ISATTY_H
|
||||
#define EOSIOKEYGEN_CORE_ISATTY_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
bool isatty(int fd);
|
||||
|
||||
bool isatty(FILE* fd);
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_CORE_ISATTY_H */
|
||||
151
cli/src/core/string.cpp
Normal file
151
cli/src/core/string.cpp
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/**
|
||||
* 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 <cstddef>
|
||||
#include <cctype>
|
||||
#include <algorithm>
|
||||
#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<char>('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
|
||||
52
cli/src/core/string.h
Normal file
52
cli/src/core/string.h
Normal file
|
|
@ -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.
|
||||
*/
|
||||
#ifndef EOSIOKEYGEN_CORE_STRING_H
|
||||
#define EOSIOKEYGEN_CORE_STRING_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
typedef std::vector<std::string> 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 */
|
||||
66
cli/src/crypto/WIF.cpp
Normal file
66
cli/src/crypto/WIF.cpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/**
|
||||
* 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 <iostream>
|
||||
#include <string.h>
|
||||
#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
|
||||
40
cli/src/crypto/WIF.h
Normal file
40
cli/src/crypto/WIF.h
Normal file
|
|
@ -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_CRYPTO_WIF_H
|
||||
#define EOSIOKEYGEN_CRYPTO_WIF_H
|
||||
|
||||
#include <string>
|
||||
#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 */
|
||||
113
cli/src/crypto/base58.cpp
Normal file
113
cli/src/crypto/base58.cpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/**
|
||||
* 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 <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
#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<unsigned char> b58(size);
|
||||
// Process the bytes.
|
||||
while (pbegin != pend) {
|
||||
int carry = *pbegin;
|
||||
int i = 0;
|
||||
// Apply "b58 = b58 * 256 + ch".
|
||||
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
|
||||
carry += 256 * (*it);
|
||||
*it = static_cast<unsigned char>(carry % 58);
|
||||
carry /= 58;
|
||||
}
|
||||
|
||||
assert(carry == 0);
|
||||
length = i;
|
||||
pbegin++;
|
||||
}
|
||||
// Skip leading zeroes in base58 result.
|
||||
std::vector<unsigned char>::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<unsigned char>& 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<bool (*)(char)>(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
|
||||
46
cli/src/crypto/base58.h
Normal file
46
cli/src/crypto/base58.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* 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 <string>
|
||||
#include <vector>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
std::string base58_encode(const std::string& str);
|
||||
std::string base58_encode(const std::vector<unsigned char>& 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 */
|
||||
54
cli/src/crypto/checksum.h
Normal file
54
cli/src/crypto/checksum.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* 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 <cstddef>
|
||||
#include <cstring>
|
||||
#include <array>
|
||||
#include "hash.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
#define CHECKSUM_SIZE 4
|
||||
|
||||
typedef std::array<unsigned char, CHECKSUM_SIZE> checksum_t;
|
||||
|
||||
template <typename T, T* (*F)(const unsigned char *, std::size_t, T*)>
|
||||
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<sha256_t, sha256>
|
||||
#define checksum_sha256d checksum<sha256_t, sha256d>
|
||||
#define checksum_ripemd160 checksum<ripemd160_t, ripemd160>
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_CHECKSUM_H */
|
||||
39
cli/src/crypto/ec.h
Normal file
39
cli/src/crypto/ec.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* 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 */
|
||||
41
cli/src/crypto/hash.h
Normal file
41
cli/src/crypto/hash.h
Normal file
|
|
@ -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_CRYPTO_HASH_H
|
||||
#define EOSIOKEYGEN_CRYPTO_HASH_H
|
||||
|
||||
#include <cstddef>
|
||||
#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 */
|
||||
70
cli/src/crypto/openssl/ec.cpp
Normal file
70
cli/src/crypto/openssl/ec.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/**
|
||||
* 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 <openssl/ec.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/hmac.h>
|
||||
#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
|
||||
43
cli/src/crypto/openssl/hash.cpp
Normal file
43
cli/src/crypto/openssl/hash.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* 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 <openssl/sha.h>
|
||||
#include <openssl/ripemd.h>
|
||||
#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(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
|
||||
55
cli/src/crypto/types.h
Normal file
55
cli/src/crypto/types.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* 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 <array>
|
||||
|
||||
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<unsigned char, EC_PRIVKEY_SIZE> ec_privkey_t;
|
||||
typedef std::array<unsigned char, EC_PUBKEY_SIZE> 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 */
|
||||
88
cli/src/key_search.cpp
Normal file
88
cli/src/key_search.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* 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 <string>
|
||||
#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
|
||||
82
cli/src/key_search.h
Normal file
82
cli/src/key_search.h
Normal file
|
|
@ -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.
|
||||
*/
|
||||
#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 */
|
||||
86
cli/src/key_search_helpers.cpp
Normal file
86
cli/src/key_search_helpers.cpp
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/**
|
||||
* 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 <iostream>
|
||||
#include "core/dictionary.h"
|
||||
#include "crypto/WIF.h"
|
||||
#include "console.h"
|
||||
#include "key_search_helpers.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
static size_t highlight(console::Color color, const std::string& str, size_t pos, size_t len) {
|
||||
|
||||
std::cout << console::fg(color, console::bold)
|
||||
<< str.substr(pos, len)
|
||||
<< console::reset;
|
||||
return len;
|
||||
}
|
||||
|
||||
void key_search_result(const struct ec_keypair* key, const struct key_result* result, const Dictionary& dict) {
|
||||
|
||||
std::string pub = wif_pub_encode(key->pub);
|
||||
Dictionary::search_result_t dict_res = dict.search(pub);
|
||||
|
||||
std::cout << "----" << 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);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto p = dict_res.find(i);
|
||||
if (p != dict_res.end()) {
|
||||
i += highlight(console::blue, pub, p->first, p->second);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::cout << pub[i++];
|
||||
}
|
||||
|
||||
std::cout << std::endl
|
||||
<< "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
|
||||
47
cli/src/key_search_helpers.h
Normal file
47
cli/src/key_search_helpers.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* 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_HELPERS_H
|
||||
#define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H
|
||||
|
||||
#include "core/string.h"
|
||||
#include "crypto/types.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
class Dictionary;
|
||||
|
||||
struct key_result {
|
||||
size_t pos; // position where the word was found.
|
||||
size_t len; // the length of the word.
|
||||
};
|
||||
|
||||
void key_search_result(const struct ec_keypair* key, const struct key_result* result, const Dictionary& dict);
|
||||
|
||||
// Check if any word in <word_list> appears in <key>'s public key.
|
||||
// returns true if a word was found (stored in <result>), 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 */
|
||||
101
cli/src/key_search_mt.cpp
Normal file
101
cli/src/key_search_mt.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* 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 <cstddef>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#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<std::mutex> 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<std::thread> 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
|
||||
292
cli/src/main.cpp
Normal file
292
cli/src/main.cpp
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
#ifdef HAVE_THREADS
|
||||
#include <thread>
|
||||
#endif /* HAVE_THREADS */
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#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 "console.h"
|
||||
#include "key_search.h"
|
||||
#include "benchmark.h"
|
||||
|
||||
// Command line options.
|
||||
bool option_l33t = false;
|
||||
|
||||
#ifdef HAVE_THREADS
|
||||
int option_num_threads = std::thread::hardware_concurrency();
|
||||
#endif /* HAVE_THREADS */
|
||||
|
||||
int cmd_search(const eoskeygen::strlist_t& words, const eoskeygen::Dictionary& dict, int count) {
|
||||
|
||||
eoskeygen::KeySearch ks;
|
||||
|
||||
ks.addDictionary(dict);
|
||||
|
||||
for(auto it = words.begin(); it != words.end(); it++) {
|
||||
size_t p = eoskeygen::is_base58(*it);
|
||||
if (p != std::string::npos) {
|
||||
std::cerr << "The word '"
|
||||
<< *it << "' contains an invalid non-base58 character '"
|
||||
<< (*it)[p] << "'" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (option_l33t) {
|
||||
for(std::size_t i = 0; i < words.size(); i++) {
|
||||
ks.addList(eoskeygen::l33twords(words[i]));
|
||||
}
|
||||
} else {
|
||||
ks.addList(words);
|
||||
}
|
||||
|
||||
#ifdef HAVE_THREADS
|
||||
ks.setThreadCount(option_num_threads);
|
||||
#endif /* HAVE_THREADS */
|
||||
|
||||
std::cout << "Searching for " << count
|
||||
<< " keys containing: " << eoskeygen::strjoin(ks.getList(), ",")
|
||||
#ifdef HAVE_THREADS
|
||||
<< ", Using: " << option_num_threads << " threads"
|
||||
#endif /* HAVE_THREADS */
|
||||
<< std::endl;
|
||||
|
||||
ks.find(count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usage(const char *name) {
|
||||
|
||||
std::cout << name
|
||||
<< " [ -h | --help | -v | search [ -m | --l33t"
|
||||
#ifdef HAVE_THREADS
|
||||
<< " | --threads=<num>"
|
||||
#endif /* HAVE_THREADS */
|
||||
<< " | --dict=<file> ... "
|
||||
<< " | --lang=<value> ... ] <word_list>|file:<filename> [ <count:10> ]"
|
||||
<< " | benchmark [ <num:1000> ]"
|
||||
<< " ]"
|
||||
<< std::endl << std::endl;
|
||||
|
||||
std::cout << " - Output one EOSIO key pair if no arguments are given" << std::endl << std::endl;
|
||||
|
||||
// Options
|
||||
std::cout
|
||||
<< " Options:" << std::endl
|
||||
<< " -h, --help Shows this help text."
|
||||
<< std::endl << std::endl
|
||||
<< " -v Shows version."
|
||||
<< std::endl << std::endl;
|
||||
|
||||
std::cout << " search: " << std::endl
|
||||
<< " performs a search, finding <count> public keys containing" << std::endl
|
||||
<< " one or more words from <word_list> (separated with ',')." << std::endl
|
||||
<< std::endl
|
||||
<< " Instead of a list it is possible to specify a file with words" << std::endl
|
||||
<< " (separated with newline '\\n') using file:<filename>"
|
||||
<< std::endl << std::endl
|
||||
<< " -m: Monochrome, disables all color output."
|
||||
<< std::endl << std::endl
|
||||
<< " --l33t: Takes each word in <word_list> and find all l33tspeak" << std::endl
|
||||
<< " combinations of that word and uses the new list for the search."
|
||||
#ifdef HAVE_THREADS
|
||||
<< std::endl << std::endl
|
||||
<< " --threads=<num>: Use <num> of parallel threads for searching." << std::endl
|
||||
<< " Default is what the operating system recomend."
|
||||
#endif /* HAVE_THREADS */
|
||||
<< std::endl << std::endl
|
||||
<< " --dict=<file>: Use words found in <file> (separated by newline) to" << std::endl
|
||||
<< " highlight words in the keys found (note that the words in this" << std::endl
|
||||
<< " file are not used for search. only for highlight output)." << std::endl
|
||||
<< " There can be more then one --dict flag. In that case contents" << std::endl
|
||||
<< " of all files are merged into one dictionary." << std::endl
|
||||
<< std::endl << std::endl
|
||||
<< " --lang=<value>: Same as --dict but will use <value>" << std::endl
|
||||
<< " to find a file in " << CONFIG_SHARE_FULL_PATH << "/dict." << std::endl
|
||||
<< " There can be more then one --lang flag. In that case contents" << std::endl
|
||||
<< " of all files are merged into one dictionary." << std::endl
|
||||
<< std::endl;
|
||||
|
||||
std::cout << " Benchmark: " << std::endl
|
||||
<< " performs a benchmark test, generating <num> keys and measuring the time." << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void cmd_benchmark(size_t num_keys) {
|
||||
|
||||
struct eoskeygen::benchmark_result res;
|
||||
|
||||
std::cout << "Benchmark: Generating "
|
||||
<< num_keys << " keys" << std::endl;
|
||||
|
||||
eoskeygen::benchmark(num_keys, &res);
|
||||
|
||||
std::cout << "Result: Took " << res.sec << " seconds, "
|
||||
<< res.kps << " keys per second." << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
// current position in argv
|
||||
// when parsing command line.
|
||||
int p = 1;
|
||||
|
||||
// No args, just print a key.
|
||||
if (argc <= 1) {
|
||||
struct eoskeygen::ec_keypair pair;
|
||||
eoskeygen::ec_generate_key(&pair);
|
||||
eoskeygen::wif_print_key(&pair);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[p], "-h") || !strcmp(argv[p], "--help")) {
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[p], "-v")) {
|
||||
std::cout << PROGRAM_NAME << " v" << PROGRAM_VERSION << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[p], "search")) {
|
||||
|
||||
int count = 10;
|
||||
eoskeygen::strlist_t words;
|
||||
eoskeygen::Dictionary dict;
|
||||
|
||||
while(p++ < argc - 1) {
|
||||
if (!strcmp(argv[p], "-m")) {
|
||||
eoskeygen::console::disable_color = true;
|
||||
} else if (!strcmp(argv[p], "--l33t")) {
|
||||
option_l33t = true;
|
||||
} else if (!memcmp(argv[p], "--threads=", 10)) {
|
||||
#ifdef HAVE_THREADS
|
||||
option_num_threads = atoi(argv[p] + 10);
|
||||
if (option_num_threads < 2) {
|
||||
std::cerr << "NOTICE: Number of threads less than 2 does not make sense."
|
||||
<< " So eosio-keygen will use 2." << std::endl;
|
||||
option_num_threads = 2;
|
||||
}
|
||||
#else
|
||||
// Even if we dont have threads. we consume the flag.
|
||||
// otherwise we might break scripts. Print a nice message instead.
|
||||
std::cerr << "NOTICE: eosio-keygen is not compiled with"
|
||||
<< " thread support. this option is ignored." << std::endl;
|
||||
#endif /* HAVE_THREADS */
|
||||
}
|
||||
// Dictionary.
|
||||
else if (!memcmp(argv[p], "--dict=", 7)) {
|
||||
eoskeygen::Dictionary d;
|
||||
std::string filename(argv[p] + 7);
|
||||
|
||||
if (d.loadFromFile(filename)) {
|
||||
dict.add(d);
|
||||
} else {
|
||||
std::cerr << "Could not load dictionary from file: " << filename << std::endl;
|
||||
}
|
||||
}
|
||||
// Language (dictionary, but we find the file in <CONFIG_SHARE_FULL_PATH>/dict/<lang>)
|
||||
else if (!memcmp(argv[p], "--lang=", 7)) {
|
||||
eoskeygen::Dictionary d;
|
||||
std::string lang(argv[p] + 7);
|
||||
std::string filename(std::string(CONFIG_SHARE_FULL_PATH) + "/dict/" + lang);
|
||||
|
||||
if (d.loadFromFile(filename)) {
|
||||
dict.add(d);
|
||||
} else {
|
||||
std::cerr << "Could not load language " << lang << " (" << filename << ")" << std::endl;
|
||||
}
|
||||
}
|
||||
// Error out on any flag we don't support.
|
||||
else if (argv[p][0] == '-') {
|
||||
std::cerr << "Unrecognized flag: " << argv[p] << std::endl;
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
// wordlist and count
|
||||
else if (words.size() < 1) {
|
||||
std::string arg = std::string(argv[p]);
|
||||
if (arg.rfind("file:", 0) == 0) {
|
||||
std::string filename = arg.substr(5);
|
||||
if (!eoskeygen::readLines(filename, words)) {
|
||||
std::cerr << "Could not read file: " << filename << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (words.size() < 1) {
|
||||
std::cerr << filename << " did not contain any words" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// List
|
||||
else {
|
||||
words = eoskeygen::strsplitwords(arg);
|
||||
}
|
||||
|
||||
if (p + 1 < argc) {
|
||||
count = atoi(argv[++p]);
|
||||
if (count < 1) {
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (words.size() < 1) {
|
||||
std::cerr << "You must specify a word list." << std::endl;
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return cmd_search(words, dict, count);
|
||||
}
|
||||
// Benchmark
|
||||
else if (!strcmp(argv[p], "benchmark")) {
|
||||
int num_keys = 1000;
|
||||
|
||||
if (++p < argc) {
|
||||
num_keys = atoi(argv[p]);
|
||||
if (num_keys < 1) {
|
||||
num_keys = 1;
|
||||
}
|
||||
}
|
||||
|
||||
cmd_benchmark(num_keys);
|
||||
} else {
|
||||
std::cerr << "Unrecogniced command: " << argv[1] << std::endl;
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue