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
|
|
|
|
#include "Winsock.h"
|
|
|
|
#include "Windows.h"
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:39:55 +01:00
|
|
|
void getnumthreads()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD_PTR dwProcessAffinity, dwSystemAffinity;
|
|
|
|
#elif __linux__
|
|
|
|
cpu_set_t dwProcessAffinity;
|
|
|
|
#else
|
|
|
|
int dwProcessAffinity = 0;
|
|
|
|
int32_t core_count = 0;
|
|
|
|
#endif
|
2015-11-08 11:26:17 +01:00
|
|
|
size_t len = sizeof(dwProcessAffinity);
|
2015-11-07 18:39:55 +01:00
|
|
|
if (numthreads > 0)
|
|
|
|
return;
|
|
|
|
#ifdef _WIN32
|
|
|
|
GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinity, &dwSystemAffinity);
|
|
|
|
#elif __linux__
|
|
|
|
sched_getaffinity(0, len, &dwProcessAffinity);
|
|
|
|
#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
|
|
|
|
for (unsigned int i = 0; i < len * 8; i++)
|
2015-11-08 11:26:17 +01:00
|
|
|
#if defined(_WIN32)
|
2016-05-24 09:42:49 +02:00
|
|
|
if (dwProcessAffinity & (1i64 << i))
|
2015-11-08 11:26:17 +01:00
|
|
|
#elif defined __linux__
|
2016-05-24 09:42:49 +02:00
|
|
|
if (CPU_ISSET(i, &dwProcessAffinity))
|
2015-11-08 11:26:17 +01:00
|
|
|
#else
|
2016-05-24 09:42:49 +02:00
|
|
|
if (dwProcessAffinity & (1 << i))
|
2015-11-07 19:03:38 +01:00
|
|
|
#endif
|
2015-11-07 18:39:55 +01:00
|
|
|
numthreads++;
|
2016-05-24 09:42:49 +02:00
|
|
|
if (numthreads == 0) // something failed
|
|
|
|
numthreads = 1;
|
2015-11-07 18:39:55 +01:00
|
|
|
printf("Number of threads: %i\n", (int)numthreads);
|
|
|
|
}
|
|
|
|
|
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__
|
2015-11-05 22:18:53 +01: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;
|
2015-11-05 22:18:53 +01:00
|
|
|
}
|