1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-keygen synced 2026-06-18 04:00:03 +02:00

move src dir to cli and add it's own cmake file.

This commit is contained in:
Henrik Hautakoski 2020-04-07 22:37:05 +02:00
parent 526b87670b
commit ba0efa2dd7
15 changed files with 48 additions and 43 deletions

42
cli/CMakeLists.txt Normal file
View file

@ -0,0 +1,42 @@
# Options
option(FORCE_ANSI "Force ANSI console colors even on windows" OFF)
# --------------------------------
# Program
# --------------------------------
set (PROGRAM_EXE ${CMAKE_PROJECT_NAME})
set (PROGRAM_SOURCE
isatty.cpp
cli_key_search_result.cpp
console.cpp
benchmark.cpp
main.cpp
)
if (WIN32 AND NOT FORCE_ANSI)
set (PROGRAM_SOURCE ${PROGRAM_SOURCE} console_win32.cpp)
else()
# *nix should have ansi support.
set (PROGRAM_SOURCE ${PROGRAM_SOURCE} console_ansi.cpp)
endif()
add_executable( ${PROGRAM_EXE} ${PROGRAM_SOURCE} )
target_link_libraries( ${PROGRAM_EXE} PUBLIC common )
# --------------------------------
# Install
# --------------------------------
install(TARGETS ${PROGRAM_EXE} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# Documentation
if (UNIX)
configure_file( docs/eosio-keygen.1.in ${PROJECT_BINARY_DIR}/man1/eosio-keygen.1 )
install(DIRECTORY ${PROJECT_BINARY_DIR}/man1
DESTINATION ${CMAKE_INSTALL_MANDIR})
endif (UNIX)

54
cli/benchmark.cpp Normal file
View 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 <libeosio/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 libeosio::ec_keypair k;
libeosio::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/benchmark.h Normal file
View 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 */

View file

@ -0,0 +1,74 @@
/**
* 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 <libeosio/WIF.h>
#include <eoskeygen/core/dictionary.h>
#include "console.h"
#include "cli_key_search_result.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;
}
CliKeySearchResult::CliKeySearchResult(const Dictionary& dict) :
m_dict (dict)
{
}
void CliKeySearchResult::onResult(const struct libeosio::ec_keypair* key, const struct KeySearch::result& result) {
std::string pub = libeosio::wif_pub_encode(key->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 << "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: " << libeosio::wif_priv_encode(key->secret) << std::endl;
}
} // namespace eoskeygen

View file

@ -0,0 +1,50 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef EOSIOKEYGEN_KEY_SEARCH_HELPERS_H
#define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H
#include <libeosio/types.h>
#include <eoskeygen/core/string.h>
#include <eoskeygen/key_search.h>
#include <eoskeygen/key_search_result.h>
namespace eoskeygen {
class Dictionary;
class CliKeySearchResult : public IKeySearchResult
{
public:
CliKeySearchResult(const Dictionary& dict);
virtual void onResult(const struct libeosio::ec_keypair* key, const struct KeySearch::result& result);
protected :
const Dictionary& m_dict;
};
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_KEY_SEARCH_HELPERS_H */

48
cli/console.cpp Normal file
View 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 "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/console.h Normal file
View 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/console_ansi.cpp Normal file
View 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/console_win32.cpp Normal file
View 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

153
cli/docs/eosio-keygen.1.in Normal file
View file

@ -0,0 +1,153 @@
.TH @PROJECT_NAME@ 1 "January, 2020" "@PROJECT_NAME@ @PROJECT_VERSION@"
.SH NAME
@PROJECT_NAME@ - Generate public and private keypair for
.UR https://eos.io/
EOS
.UE .
.SH SYNOPSIS
.SY @PROJECT_NAME@
.OP \-h|--help
.YS
.SY @PROJECT_NAME@
.OP \-v
.YS
.SY @PROJECT_NAME@
search
.OP -m
.OP \--l33t
.OP \--threads=<num>
.OP \--dict=<file1> ...
.OP \--lang=<value> ...
.B word_list
.OP count
.YS
.SY @PROJECT_NAME@
benchmark
.OP num_keys
.YS
.SH DESCRIPTION
.P
Output one EOSIO key pair if no arguments are given
.P
Options and subcommands are as follows:
.TP 15
.B -h, --help
Shows this help text.
.TP 15
.B -v
Shows version.
.TP 15
.B search
performs a search, finding
.I <count>
public keys containing one or more words from
.I <word_list>
(separated with ',').
Instead of a list it is possible to specify a file with words (separated with newline '\\n') using
.I file:<filename>
.RS 16
Search specific options:
.RS 2
.TP 20
.B -m
Monochrome, disables all color output.
.TP 20
.B --l33t
Takes each word in
.I <word_list>
and find all l33tspeak combinations of that word and uses the new list for the search.
.TP 20
.B --threads=<num>
Use
.I <num>
of parallel threads for searching. Default is what the operating system recomends.
.TP 20
.B --dict=<file>
Use words found in
.I <file>
(separated by newline) to highlight words in the keys found (note that the words in this
file are not used for search. only for highlight output). There can be more then one
.B --dict
flag. In that case contents of all files are merged into one dictionary.
.TP 20
.B --lang=<value>
Same as
.B --dict
but will use
.I <value>
to find a file in
.B @CMAKE_INSTALL_FULL_DATADIR@/@CMAKE_PROJECT_NAME@/dict.
There can be more then one
.B --lang
flag. In that case contents of all files are merged into one dictionary.
.TP 20
.B count
Number of keys to search for (default is 10)
.RE 1
.TP 15
.B benchmark
performs a benchmark test, generating
.I <num_keys>
keys and measuring the time.
.PP
.RS 16
Benchmark specific options:
.RS 2
.TP 15
.B num_keys
Number of keys to search for (default is 10)
.RE 1
.SH SECURITY NOTICE
Keys are generated by OpenSSL\'s
.B EC_KEY_generate_key
function. The program will
never expose your keys to anything but the computers memory and output of the\
program. You are free to inspect the source code and compile yourself to verify.
.P
However, use this at your own risk. we cannot guarantee that the keys are\
cryptographically secure as this depends on OpenSSL's implementation (alto it is\
widely used and should be safe)
.P
Please read the
.I LICENSE
file.
.P
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
.br
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
.br
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
.br
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
.br
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
.br
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.SH BUGS
Report bugs to
.UR https://github.com/eosswedenorg/eosio-keygen/issues
Github
.UE . Thank you.
.SH AUTHOR
.MT henrik@eossweden.org
Henrik Hautakoski
.ME
.UR https://eossweden.org
EOS Sw/eden
.UE

21
cli/isatty.cpp Normal file
View 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/isatty.h Normal file
View 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 */

293
cli/main.cpp Normal file
View file

@ -0,0 +1,293 @@
/**
* 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 <cstring>
#include <libeosio/base58.h>
#include <libeosio/ec.h>
#include <libeosio/WIF.h>
#include <eoskeygen/config.h>
#include <eoskeygen/core/file.h>
#include <eoskeygen/core/string.h>
#include <eoskeygen/core/dictionary.h>
#include <eoskeygen/core/leet.h>
#include <eoskeygen/key_search.h>
#include "cli_key_search_result.h"
#include "console.h"
#include "benchmark.h"
#include "config.h"
// Command line options.
bool option_l33t = false;
#ifdef EOSIOKEYGEN_HAVE_THREADS
size_t option_num_threads = eoskeygen::KeySearch::max_threads();
#endif /* EOSIOKEYGEN_HAVE_THREADS */
int cmd_search(const eoskeygen::strlist_t& words, const eoskeygen::Dictionary& dict, int count) {
eoskeygen::KeySearch ks;
eoskeygen::CliKeySearchResult rs(dict);
ks.setCallback(&rs);
for(auto it = words.begin(); it != words.end(); it++) {
size_t p = libeosio::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 EOSIOKEYGEN_HAVE_THREADS
ks.setThreadCount(option_num_threads);
#endif /* EOSIOKEYGEN_HAVE_THREADS */
std::cout << "Searching for " << count
<< " keys containing: " << eoskeygen::strlist::join(ks.getList(), ",")
#ifdef EOSIOKEYGEN_HAVE_THREADS
<< ", Using: " << option_num_threads << " threads"
#endif /* EOSIOKEYGEN_HAVE_THREADS */
<< std::endl;
ks.find(count);
return 0;
}
void usage(const char *name) {
std::cout << name
<< " [ -h | --help | -v | search [ -m | --l33t"
#ifdef EOSIOKEYGEN_HAVE_THREADS
<< " | --threads=<num>"
#endif /* EOSIOKEYGEN_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 EOSIOKEYGEN_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 /* EOSIOKEYGEN_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 libeosio::ec_keypair pair;
libeosio::ec_generate_key(&pair);
libeosio::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 EOSIOKEYGEN_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 /* EOSIOKEYGEN_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::strlist::splitw(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;
}