This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypair.cpp
209 lines (160 loc) · 4.89 KB
/
keypair.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <chrono>
#include <thread>
#include <vector>
#include <atomic>
#include <fstream>
#include <iostream>
#include <sodium.h>
#include <unistd.h>
#include <algorithm>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __LINUX__
#include <sstream>
#endif
#include "lib/crc16.h"
#include "lib/basen.h"
enum VersionByte : uint8_t {
PUBLIC_KEY = (6 << 3),
SEED = (18 << 3)
};
const std::string base32Dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
std::atomic<bool> found{false};
std::atomic<int> count{0};
char checkInvalidChar(const std::string &input) {
for (const char& c : input)
if (base32Dictionary.find(c) == std::string::npos)
return c;
return -1;
}
int getProcessingUnits() {
int units = std::thread::hardware_concurrency();
if (units > 0)
return units;
#ifdef _SC_NPROCESSORS_CONF
units = sysconf(_SC_NPROCESSORS_CONF);
#endif
if (units < 1) {
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
units = info.dwNumberOfProcessors;
#endif
#ifdef __LINUX__
std::ifstream cpuinfo("/proc/cpuinfo", std::ifstream::in);
if (cpuinfo.open())
units = std::count(std::istream_iterator<std::string>(cpuinfo),
std::istream_iterator<std::string>(),
"processor");
cpuinfo.close();
#endif
}
if (units < 1) {
std::cout << "Unable to determine the number of CPUs. Running a single thread...\n";
return 1;
}
return units;
}
std::string encode(const VersionByte &versionByte, std::vector<uint8_t> &data) {
std::vector<uint8_t> bytes;
bytes.push_back(versionByte);
bytes.insert(bytes.end(), data.begin(), data.end());
uint16_t crc = crc16((char *) bytes.data(), bytes.size());
bytes.emplace_back(static_cast<uint8_t>(crc & 0xFF));
bytes.emplace_back(static_cast<uint8_t>((crc >> 8) & 0xFF));
return bn::encode_b32(bytes);
}
inline bool hasSuffix(const std::string &input, const std::string &term) {
return input.substr(input.size() - term.size()) == term;
}
inline bool hasPrefix(const std::string &input, const std::string &term) {
return input.substr(0, term.size()) == term;
}
inline bool hasSubstr(const std::string &input, const std::string &term) {
return input.find(term) != std::string::npos;
}
void usage(const char* exec) {
std::cerr << "Usage: " << exec << " [-p|-m|-s] <term> [-j <jobs>]\n\n";
}
void process(const std::string &term, bool (*matches)(const std::string &input, const std::string &suffix)) {
std::vector<uint8_t> pk(crypto_sign_PUBLICKEYBYTES);
std::vector<uint8_t> sk(crypto_sign_SECRETKEYBYTES);
for (;!found; count++) {
crypto_sign_keypair(pk.data(), sk.data());
std::string encodedPk = encode(PUBLIC_KEY, pk);
if (matches(encodedPk, term)) {
std::vector<uint8_t> seed(crypto_sign_SEEDBYTES);
crypto_sign_ed25519_sk_to_seed(seed.data(), sk.data());
if (!found) {
found = true;
std::cout << "FOUND!\n\n"
<< encodedPk << std::endl
<< encode(SEED, seed) << "\n\n\a";
}
break;
}
}
}
int main(int argc, char* argv[]) {
if (argc == 1) {
usage(argv[0]);
return 1;
}
std::string term;
int threadsCount = -1, c;
bool (*matchFunction)(const std::string &input, const std::string &suffix);
while ((c = getopt(argc, argv, "p:m:s:j:")) != -1) {
switch (c) {
case 'p':
if (optarg[0] != 'g' && optarg[0] != 'G') {
std::cout << "Prepending 'G' to the search term.\n";
term = "G";
}
term += optarg;
matchFunction = &hasPrefix;
break;
case 's':
term = optarg;
matchFunction = &hasSuffix;
break;
case 'm':
term = optarg;
matchFunction = &hasSubstr;
break;
case 'j':
threadsCount = atoi(optarg);
break;
case '?':
default:
usage(argv[0]);
return 0;
}
}
if (threadsCount < 1) {
threadsCount = getProcessingUnits();
}
std::transform(term.begin(), term.end(), term.begin(), ::toupper);
char invalidChar = checkInvalidChar(term);
if (invalidChar != -1) {
std::cerr << "\"" << invalidChar << "\" is not allowed. The search term must be scoped to:\n\t"
<< base32Dictionary << std::endl;
return 0;
}
if (sodium_init() == -1) {
std::cerr << "Unable to init libsodium.\n";
return 1;
}
std::cout << "Searching...\n";
std::vector<std::thread> threads(threadsCount);
auto start = std::chrono::system_clock::now();
for (int i = 0; i < threadsCount; i++)
threads[i] = std::thread(process, term, matchFunction);
std::for_each(threads.begin(), threads.end(), [](std::thread &t) { t.join(); });
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << count << " tries in "
<< elapsed_seconds.count() << "s using "
<< threadsCount << " threads.\n";
return 0;
}