2015-11-05 20:47:30 +01:00
|
|
|
// bitmessage cracker, build with g++ or MSVS to a shared library, use included python code for usage under bitmessage
|
|
|
|
#ifdef _WIN32
|
2019-06-28 17:05:40 +02:00
|
|
|
#include "winsock.h"
|
|
|
|
#include "windows.h"
|
2015-11-05 20:47:30 +01:00
|
|
|
#define uint64_t unsigned __int64
|
|
|
|
#else
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-05-24 09:42:49 +02:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__) || defined (__NetBSD__)
|
2015-11-08 11:26:17 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2015-11-05 20:47:30 +01:00
|
|
|
|
|
|
|
#include "openssl/sha.h"
|
|
|
|
|
|
|
|
#define HASH_SIZE 64
|
|
|
|
#define BUFLEN 16384
|
|
|
|
|
2015-11-05 22:18:53 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define EXPORT __attribute__ ((__visibility__("default")))
|
2015-11-07 18:39:55 +01:00
|
|
|
#elif defined(_WIN32)
|
2015-11-05 22:18:53 +01:00
|
|
|
#define EXPORT __declspec(dllexport)
|
|
|
|
#endif
|
|
|
|
|
2015-11-08 11:26:17 +01:00
|
|
|
#ifndef __APPLE__
|
2015-11-05 20:47:30 +01:00
|
|
|
#define ntohll(x) ( ( (uint64_t)(ntohl( (unsigned int)((x << 32) >> 32) )) << 32) | ntohl( ((unsigned int)(x >> 32)) ) )
|
2015-11-08 11:26:17 +01:00
|
|
|
#endif
|
2015-11-05 20:47:30 +01:00
|
|
|
|
|
|
|
unsigned long long max_val;
|
|
|
|
unsigned char *initialHash;
|
|
|
|
unsigned long long successval = 0;
|
2015-11-07 18:39:55 +01:00
|
|
|
unsigned int numthreads = 0;
|
|
|
|
|
2015-11-05 20:47:30 +01:00
|
|
|
#ifdef _WIN32
|
2015-11-07 19:03:38 +01:00
|
|
|
DWORD WINAPI threadfunc(LPVOID param) {
|
2015-11-05 20:47:30 +01:00
|
|
|
#else
|
|
|
|
void * threadfunc(void* param) {
|
|
|
|
#endif
|
2015-11-07 19:03:38 +01:00
|
|
|
unsigned int incamt = *((unsigned int*)param);
|
2015-11-05 20:47:30 +01:00
|
|
|
SHA512_CTX sha;
|
|
|
|
unsigned char buf[HASH_SIZE + sizeof(uint64_t)] = { 0 };
|
|
|
|
unsigned char output[HASH_SIZE] = { 0 };
|
|
|
|
|
|
|
|
memcpy(buf + sizeof(uint64_t), initialHash, HASH_SIZE);
|
|
|
|
|
|
|
|
unsigned long long tmpnonce = incamt;
|
|
|
|
unsigned long long * nonce = (unsigned long long *)buf;
|
|
|
|
unsigned long long * hash = (unsigned long long *)output;
|
|
|
|
while (successval == 0) {
|
|
|
|
tmpnonce += numthreads;
|
|
|
|
|
|
|
|
(*nonce) = ntohll(tmpnonce); /* increment nonce */
|
|
|
|
SHA512_Init(&sha);
|
|
|
|
SHA512_Update(&sha, buf, HASH_SIZE + sizeof(uint64_t));
|
|
|
|
SHA512_Final(output, &sha);
|
|
|
|
SHA512_Init(&sha);
|
|
|
|
SHA512_Update(&sha, output, HASH_SIZE);
|
|
|
|
SHA512_Final(output, &sha);
|
|
|
|
|
|
|
|
if (ntohll(*hash) < max_val) {
|
|
|
|
successval = tmpnonce;
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 14:48:53 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
return 0;
|
|
|
|
#else
|
2015-11-05 20:47:30 +01:00
|
|
|
return NULL;
|
2017-02-19 14:48:53 +01:00
|
|
|
#endif
|
2015-11-05 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 18:39:55 +01:00
|
|
|
void getnumthreads()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD_PTR dwProcessAffinity, dwSystemAffinity;
|
|
|
|
#elif __linux__
|
2019-09-18 11:09:29 +02:00
|
|
|
cpu_set_t dwProcessAffinity;
|
2017-02-18 17:20:09 +01:00
|
|
|
#elif __OpenBSD__
|
|
|
|
int mib[2], core_count = 0;
|
|
|
|
int dwProcessAffinity = 0;
|
|
|
|
size_t len2;
|
2015-11-07 18:39:55 +01:00
|
|
|
#else
|
|
|
|
int dwProcessAffinity = 0;
|
|
|
|
int32_t core_count = 0;
|
|
|
|
#endif
|
2019-09-18 11:09:29 +02:00
|
|
|
size_t len = sizeof(dwProcessAffinity);
|
|
|
|
if (numthreads > 0)
|
|
|
|
return;
|
2015-11-07 18:39:55 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinity, &dwSystemAffinity);
|
|
|
|
#elif __linux__
|
2019-09-18 11:09:29 +02:00
|
|
|
sched_getaffinity(0, len, &dwProcessAffinity);
|
2017-02-18 17:20:09 +01:00
|
|
|
#elif __OpenBSD__
|
|
|
|
len2 = sizeof(core_count);
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
if (sysctl(mib, 2, &core_count, &len2, 0, 0) == 0)
|
|
|
|
numthreads = core_count;
|
2015-11-07 18:39:55 +01:00
|
|
|
#else
|
2015-11-08 11:26:17 +01:00
|
|
|
if (sysctlbyname("hw.logicalcpu", &core_count, &len, 0, 0) == 0)
|
2015-11-07 18:39:55 +01:00
|
|
|
numthreads = core_count;
|
2016-05-24 09:42:49 +02:00
|
|
|
else if (sysctlbyname("hw.ncpu", &core_count, &len, 0, 0) == 0)
|
|
|
|
numthreads = core_count;
|
2015-11-07 18:39:55 +01:00
|
|
|
#endif
|
2019-09-18 11:09:29 +02:00
|
|
|
for (unsigned int i = 0; i < len * 8; i++)
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
if (dwProcessAffinity & (1i64 << i))
|
|
|
|
#else // CYGWIN/MINGW
|
|
|
|
if (dwProcessAffinity & (1ULL << i))
|
|
|
|
#endif
|
|
|
|
#elif defined __linux__
|
|
|
|
if (CPU_ISSET(i, &dwProcessAffinity))
|
|
|
|
#else
|
|
|
|
if (dwProcessAffinity & (1 << i))
|
|
|
|
#endif
|
|
|
|
numthreads++;
|
|
|
|
if (numthreads == 0) // something failed
|
|
|
|
numthreads = 1;
|
|
|
|
printf("Number of threads: %i\n", (int)numthreads);
|
2015-11-07 18:39:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-05 22:18:53 +01:00
|
|
|
extern "C" EXPORT unsigned long long BitmessagePOW(unsigned char * starthash, unsigned long long target)
|
2015-11-05 20:47:30 +01:00
|
|
|
{
|
|
|
|
successval = 0;
|
|
|
|
max_val = target;
|
2015-11-07 18:39:55 +01:00
|
|
|
getnumthreads();
|
2015-11-05 20:47:30 +01:00
|
|
|
initialHash = (unsigned char *)starthash;
|
|
|
|
# ifdef _WIN32
|
|
|
|
HANDLE* threads = (HANDLE*)calloc(sizeof(HANDLE), numthreads);
|
|
|
|
# else
|
2015-11-05 22:18:53 +01:00
|
|
|
pthread_t* threads = (pthread_t*)calloc(sizeof(pthread_t), numthreads);
|
|
|
|
struct sched_param schparam;
|
|
|
|
schparam.sched_priority = 0;
|
2015-11-05 20:47:30 +01:00
|
|
|
# endif
|
2015-11-07 18:39:55 +01:00
|
|
|
unsigned int *threaddata = (unsigned int *)calloc(sizeof(unsigned int), numthreads);
|
2015-11-07 19:03:38 +01:00
|
|
|
for (unsigned int i = 0; i < numthreads; i++) {
|
2015-11-07 18:39:55 +01:00
|
|
|
threaddata[i] = i;
|
2015-11-05 20:47:30 +01:00
|
|
|
# ifdef _WIN32
|
2015-11-07 18:39:55 +01:00
|
|
|
threads[i] = CreateThread(NULL, 0, threadfunc, (LPVOID)&threaddata[i], 0, NULL);
|
2015-11-05 20:47:30 +01:00
|
|
|
SetThreadPriority(threads[i], THREAD_PRIORITY_IDLE);
|
|
|
|
# else
|
2015-11-07 18:39:55 +01:00
|
|
|
pthread_create(&threads[i], NULL, threadfunc, (void*)&threaddata[i]);
|
|
|
|
# ifdef __linux__
|
2019-09-18 11:09:29 +02:00
|
|
|
pthread_setschedparam(threads[i], SCHED_IDLE, &schparam);
|
2015-11-07 18:39:55 +01:00
|
|
|
# else
|
2015-11-08 11:26:17 +01:00
|
|
|
pthread_setschedparam(threads[i], SCHED_RR, &schparam);
|
2015-11-07 18:39:55 +01:00
|
|
|
# endif
|
2015-11-05 20:47:30 +01:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
# ifdef _WIN32
|
|
|
|
WaitForMultipleObjects(numthreads, threads, TRUE, INFINITE);
|
|
|
|
# else
|
2015-11-07 19:03:38 +01:00
|
|
|
for (unsigned int i = 0; i < numthreads; i++) {
|
2015-11-05 20:47:30 +01:00
|
|
|
pthread_join(threads[i], NULL);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
free(threads);
|
2015-11-07 18:39:55 +01:00
|
|
|
free(threaddata);
|
2015-11-05 20:47:30 +01:00
|
|
|
return successval;
|
2019-09-18 11:09:29 +02:00
|
|
|
}
|