mirror of
https://github.com/PurpleI2P/i2pd-tools.git
synced 2025-01-22 13:27:17 +01:00
Multithread
This commit is contained in:
parent
f93bf735b0
commit
1fa3cb124f
139
vanitygen.cpp
139
vanitygen.cpp
|
@ -5,48 +5,141 @@
|
|||
#include "Crypto.h"
|
||||
#include "Identity.h"
|
||||
#include "common/key.hpp"
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
static std::mutex thread_mutex;
|
||||
static i2p::data::SigningKeyType type;
|
||||
static i2p::data::PrivateKeys keys;
|
||||
static bool finded=false;
|
||||
static size_t padding_size;
|
||||
static uint8_t * KeyBuf;
|
||||
static uint8_t * PaddingBuf;
|
||||
static unsigned long long hash;
|
||||
|
||||
#define CPU_ONLY
|
||||
|
||||
|
||||
#ifdef CPU_ONLY
|
||||
// XXX: make this faster
|
||||
static bool check_keys(const std::string & prefix, i2p::data::PrivateKeys & key)
|
||||
{
|
||||
return key.GetPublic()->GetIdentHash().ToBase32().substr(0, prefix.length()) == prefix;
|
||||
static inline bool NotThat(const char * a, const char *b){
|
||||
while(*b)
|
||||
if(*a++!=*b++) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// XXX: make this faster
|
||||
static void mutate_keys(uint8_t * buf, i2p::data::PrivateKeys & key)
|
||||
{
|
||||
uint8_t * ptr = key.GetPadding();
|
||||
// TODO: do not hard code for ed25519
|
||||
RAND_bytes(ptr, 96);
|
||||
key.RecalculateIdentHash(buf);
|
||||
inline void twist_cpu(uint8_t * buf,size_t * l0){
|
||||
//TODO: NORMAL IMPLEMENTATION
|
||||
RAND_bytes(buf,padding_size);
|
||||
}
|
||||
|
||||
|
||||
// XXX: make this faster
|
||||
static inline void mutate_keys_cpu(
|
||||
uint8_t * buf,
|
||||
uint8_t * padding,
|
||||
size_t * l0)
|
||||
{
|
||||
twist_cpu(padding,l0);
|
||||
thread_mutex.lock();
|
||||
keys.RecalculateIdentHash(buf);
|
||||
thread_mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void thread_find(const char * prefix){
|
||||
while(NotThat(keys.GetPublic()->GetIdentHash().ToBase32().c_str(),prefix) and !finded)
|
||||
{
|
||||
size_t l0 = 0;
|
||||
|
||||
mutate_keys_cpu(KeyBuf,PaddingBuf, (size_t*)&l0);
|
||||
hash++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
int main (int argc, char * argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cout << "Usage: " << argv[0] << " filename prefix" << std::endl;
|
||||
return -1;
|
||||
std::cout << "Usage: keygen filename generatestring <signature type>" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
uint8_t buf[1024] = {0};
|
||||
std::string prefix(argv[2]);
|
||||
i2p::crypto::InitCrypto (false);
|
||||
// default to ed 25519 keys
|
||||
i2p::data::SigningKeyType type = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519;
|
||||
auto keys = i2p::data::PrivateKeys::CreateRandomKeys (type);
|
||||
type = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519;
|
||||
if (argc > 3)
|
||||
type = NameToSigType(std::string(argv[3]));
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
keys = i2p::data::PrivateKeys::CreateRandomKeys (type);
|
||||
switch(type){
|
||||
case i2p::data::SIGNING_KEY_TYPE_DSA_SHA1:
|
||||
case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||||
case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
|
||||
case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512_TEST:
|
||||
std::cout << "Sorry, i don't can generate adress for this signature type" << std::endl;
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
switch(type){
|
||||
case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
|
||||
padding_size=64;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
|
||||
padding_size=32;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
|
||||
padding_size=4;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048:
|
||||
padding_size=128;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072:
|
||||
padding_size=256;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096:
|
||||
padding_size=384;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
|
||||
padding_size=96;
|
||||
break;
|
||||
case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
|
||||
case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256_TEST:
|
||||
padding_size=64;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO: multi threading
|
||||
while(!check_keys(prefix, keys))
|
||||
mutate_keys(buf, keys);
|
||||
KeyBuf = new uint8_t[keys.GetFullLen()];
|
||||
PaddingBuf = keys.GetPadding();
|
||||
unsigned int count_cpu = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
std::vector<std::thread> threads(count_cpu);
|
||||
std::cout << "Start vanity generator in " << count_cpu << " threads" << std::endl;
|
||||
for ( unsigned int j = count_cpu;j--;){
|
||||
threads[j] = std::thread(thread_find,argv[2]);
|
||||
sched_param sch;
|
||||
int policy;
|
||||
pthread_getschedparam(threads[j].native_handle(), &policy, &sch);
|
||||
sch.sched_priority = 10;
|
||||
if (pthread_setschedparam(threads[j].native_handle(), SCHED_FIFO, &sch)) {
|
||||
std::cout << "Failed to setschedparam" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for(unsigned int j = 0; j < count_cpu;j++)
|
||||
threads[j].join();
|
||||
|
||||
std::cout << "Hashes: " << hash << std::endl;
|
||||
|
||||
std::ofstream f (argv[1], std::ofstream::binary | std::ofstream::out);
|
||||
if (f)
|
||||
{
|
||||
size_t len = keys.GetFullLen ();
|
||||
uint8_t * buf = new uint8_t[len];
|
||||
len = keys.ToBuffer (buf, len);
|
||||
f.write ((char *)buf, len);
|
||||
delete[] buf;
|
||||
len = keys.ToBuffer (KeyBuf, len);
|
||||
f.write ((char *)KeyBuf, len);
|
||||
delete [] KeyBuf;
|
||||
std::cout << "Destination " << keys.GetPublic ()->GetIdentHash ().ToBase32 () << " created" << std::endl;
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue