forked from capythulhu/coxinhacoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
65 lines (53 loc) · 1.64 KB
/
hash.h
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
// Header guards
#ifndef HASH_H
#define HASH_H
#ifndef STRING_H
#define STRING_H
#include <string.h>
#endif
#ifndef STDBOOL_H
#define STDBOOL_H
#include <stdbool.h>
#endif
#include "coin.h"
#include "bytes.h"
buffer hash(buffer input);
static bool mine(buffer seed, buffer gold, bool echo);
// Calcular a hash de um buffer
buffer hash(buffer input) {
const static unsigned char randomBytes[] = { 0x78, 0x16, 0xc3, 0xb3, 0x52, 0xf9, 0xe4, 0xad };
const static unsigned short int randomBytesLength = sizeof(randomBytes);
buffer digest = new_buffer(HASH_LENGTH);
zero_buffer(digest);
int result = 0;
int i, j;
for(i = 0; i < input.length; i++) {
for(j = 0; j < HASH_LENGTH; j++) {
digest.bytes[i % digest.length] = input.bytes[i] + i
^ randomBytes[(input.bytes[i] + i) % randomBytesLength];
}
result ^= input.bytes[i] + i;
}
for(i = 0; i < digest.length; i++) {
digest.bytes[i] ^= result + i;
}
return digest;
}
// Verifica se um buffer "gold" é uma hash de
// mineração válida para um buffer "seed"
static bool mine(buffer seed, buffer gold, bool echo) {
buffer input = new_buffer(seed.length + gold.length);
concat_buffer(&input, seed);
concat_buffer(&input, gold);
buffer output = hash(input);
if(echo) {
print_buffer(output);
printf(" = ");
print_buffer(gold);
printf("\n");
}
int i = 0, j = 1 << (8 - 1);
while(!(output.bytes[i/8] & (j >> (i % 8))) && i <= DIFFICULTY) i++;
return i >= DIFFICULTY;
}
#endif