Created
June 11, 2020 21:09
-
-
Save sijanec/64b7d297ea96ed200aba17a98e2705ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <strlcat.c> | |
#include <strlcpy.c> | |
#include <base64.c> | |
#include <sha-256.c> | |
#include <explode.c> | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
typedef union { | |
uint64_t u64; | |
unsigned char uc[8]; | |
uint8_t u8[8]; | |
} multiint; | |
// EMSCRIPTEN_KEEPALIVE | |
char * generateHashcash(const char * hashcashString) { // input in format 1:diff:date:resource::random:: | |
// printf("generateHashcash Called with string %s\n", hashcashString); | |
unsigned int counterPos = strlen(hashcashString)+1; | |
unsigned char * hashcash = malloc(sizeof(char)*(counterPos+69)); // 69. we can't use more than 69 bytes for the counter, right? | |
strlcpy(hashcash, hashcashString, counterPos+(69-1)); | |
multiint counter; | |
counter.u64 = 0x0000000000000000ULL;; | |
int counterSize = sizeof(counter.u64); | |
char * whereToStartWriting = (hashcash+counterPos)-1; | |
uint8_t hash[32]; | |
unsigned char i = 0, j = 0; | |
unsigned short int leadingZeros = 0; | |
// // get difficulty { | |
char **list; | |
size_t len, hehe_u_thot_imma_use_i; | |
explode(hashcash, ":", &list, &len); | |
unsigned short int difficulty = atoi(list[1]); // 1:20:* where 20 is the diff | |
for(hehe_u_thot_imma_use_i = 0; hehe_u_thot_imma_use_i < len; ++hehe_u_thot_imma_use_i) | |
free(list[hehe_u_thot_imma_use_i]); | |
free(list); | |
// \\ } | |
while (1) { | |
Base64encode(whereToStartWriting, counter.u8, counterSize); | |
calc_sha_256(hash, hashcash, strlen(hashcash)); | |
i = 0; | |
leadingZeros = 0; | |
while(1) { | |
if(hash[i] != 0) { | |
break; | |
} | |
i++; | |
} | |
leadingZeros = 8*i; | |
// if(x <= 0) leadingZeros+=8; | |
if(hash[i] <= 1) leadingZeros+=7; | |
if(hash[i] <= 3) leadingZeros+=6; | |
if(hash[i] <= 7) leadingZeros+=5; | |
if(hash[i] <= 15) leadingZeros+=4; | |
if(hash[i] <= 31) leadingZeros+=3; | |
if(hash[i] <= 63) leadingZeros+=2; | |
if(hash[i] <= 127) leadingZeros+=1; | |
if(leadingZeros >= difficulty) { | |
// fprintf(stderr, "0x"); | |
// for(int zt = 0; zt < 32; zt++) { | |
// fprintf(stderr, "%02x", hash[zt]); | |
// } | |
// fprintf(stderr, "\n"); | |
break; | |
} | |
counter.u64++; | |
} | |
return hashcash; | |
} | |
#ifdef __cplusplus | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment