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

Importing libeoskeygen as "common" module in this repo (the amount of git repos is too damn high!)

This commit is contained in:
Henrik Hautakoski 2020-04-03 16:39:43 +02:00
parent f181bddc4f
commit 654a53698f
19 changed files with 1143 additions and 0 deletions

30
common/src/config.h.in Normal file
View file

@ -0,0 +1,30 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef EOSIOKEYGEN_COMMON_CONFIG_H
#define EOSIOKEYGEN_COMMON_CONFIG_H
// Defined if we have thread support.
#cmakedefine EOSIOKEYGEN_HAVE_THREADS
#endif /* EOSIOKEYGEN_COMMON_CONFIG_H */

View file

@ -0,0 +1,125 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <vector>
#include <iostream>
#include <cstdio>
#include <iterator>
#include <algorithm>
#include <eoskeygen/core/string.h>
#include <eoskeygen/core/dictionary.h>
#include <eoskeygen/core/file.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

49
common/src/core/file.cpp Normal file
View file

@ -0,0 +1,49 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdio>
#include <eoskeygen/core/string.h>
#include <eoskeygen/core/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

82
common/src/core/leet.cpp Normal file
View 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.
*/
#include <algorithm>
#include <eoskeygen/core/leet.h>
namespace eoskeygen {
static bool is_l33t(char ch, char& r) {
// '1', '2', '3', '4', '5', '6', '7', '8', '9'
static char alphabet[9] = { 'l', 'z', 'e', 'a', 's', 'G', 't', 'B', 'g' };
for(std::size_t i = 0; i < sizeof(alphabet) / sizeof(char); i++) {
if (ch == alphabet[i]) {
r = static_cast<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

View 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.
*/
#include <cstddef>
#include <cctype>
#include <algorithm>
#include <eoskeygen/core/string.h>
namespace eoskeygen {
std::string& strtolower(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); });
return str;
}
std::string& ltrim(std::string& str) {
auto it = std::find_if(str.begin(), str.end(), [](char ch){ return !std::isspace(ch); });
str.erase(str.begin(), it);
return str;
}
std::string& rtrim(std::string& str) {
auto it = std::find_if(str.rbegin(), str.rend(), [](char ch){ return !std::isspace(ch); });
str.erase(it.base(), str.end());
return str;
}
std::string& trim(std::string& str) {
return ltrim(rtrim(str));
}
} // namespace eoskeygen

View file

@ -0,0 +1,75 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <algorithm>
#include <eoskeygen/core/string.h>
#include <eoskeygen/core/strlist.h>
namespace eoskeygen {
strlist_t strlist::splitw(const std::string& str, const std::string& delim) {
strlist_t words = strlist::split(str, delim);
std::for_each(words.begin(), words.end(), trim);
return words;
}
strlist_t strlist::split(const std::string& str, const std::string& delim) {
strlist_t r;
size_t s = 0, e = 0, dlen = delim.length();
while((e = str.find(delim, s)) != std::string::npos) {
r.push_back(str.substr(s, e - s));
s = e + dlen;
}
r.push_back(str.substr(s));
return r;
}
std::string strlist::join(const eoskeygen::strlist_t& list, const std::string& delim) {
std::string out;
for(const std::string& item : list) {
if (item.length() < 1) {
continue;
}
out += item + delim;
}
if (out.length() > 0) {
out.erase(out.end() - delim.length());
}
return out;
}
strlist_t& strlist::strip(strlist_t& list, strlist_stripfunc_t fn) {
std::transform(list.begin(), list.end(), list.begin(), fn);
return list;
}
} // namespace eoskeygen

115
common/src/key_search.cpp Normal file
View file

@ -0,0 +1,115 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <libeosio/ec.h>
#include <libeosio/WIF.h>
#include <eoskeygen/config.h>
#include <eoskeygen/core/string.h>
#include <eoskeygen/key_search_result.h>
#include <eoskeygen/key_search.h>
namespace eoskeygen {
KeySearch::KeySearch() :
#ifdef EOSIOKEYGEN_HAVE_THREADS
m_threads(0),
#endif
m_callback(NULL)
{
}
void KeySearch::addWord(const std::string& str)
{
std::string tmp = str;
strtolower(tmp);
m_words.push_back(tmp);
}
void KeySearch::addList(const strlist_t& list)
{
for(const std::string& item : list) {
addWord(item);
}
}
const strlist_t& KeySearch::getList()
{
return m_words;
}
void KeySearch::clear()
{
m_words.clear();
}
void KeySearch::setCallback(IKeySearchResult* callback)
{
m_callback = callback;
}
void KeySearch::_search_linear(size_t n) {
size_t count = 0;
struct libeosio::ec_keypair pair;
while (count < n) {
struct result res;
libeosio::ec_generate_key(&pair);
if (_contains_word(&pair, res)) {
m_callback->onResult(&pair, res);
count++;
}
}
}
void KeySearch::find(size_t num_results) {
#ifdef EOSIOKEYGEN_HAVE_THREADS
// Only do multithread if number of threads makes sense.
if (m_threads >= 2) {
_search_mt(num_results);
return;
}
#endif /* HAVE_THREADS */
_search_linear(num_results);
}
bool KeySearch::_contains_word(const struct libeosio::ec_keypair* key, struct result& result) {
// skip first 3 chars, as those are always "EOS"
std::string pubstr = libeosio::wif_pub_encode(key->pub).substr(3);
strtolower(pubstr);
for(auto const& w: m_words) {
size_t p = pubstr.find(w);
if (p != std::string::npos) {
result.pos = p + 3;
result.len = w.length();
return true;
}
}
return false;
}
} // namespace eoskeygen

View file

@ -0,0 +1,106 @@
/**
* MIT License
*
* Copyright (c) 2019-2020 EOS Sw/eden
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstddef>
#include <thread>
#include <mutex>
#include <vector>
#include <libeosio/ec.h>
#include <eoskeygen/key_search_result.h>
#include <eoskeygen/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 libeosio::ec_keypair pair;
while (g_count < g_max) {
struct result res;
libeosio::ec_generate_key(&pair);
if (_contains_word(&pair, res)) {
// Guard output with mutex, so we don't get
// interrupted mid write and can write to g_count and res safely.
const std::lock_guard<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 call result function.
g_count++;
m_callback->onResult(&pair, res);
}
}
}
void KeySearch::setThreadCount(size_t num)
{
m_threads = num;
}
size_t KeySearch::max_threads()
{
return std::thread::hardware_concurrency();
}
void KeySearch::_search_mt(size_t n)
{
std::vector<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