-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsha256_check.c
115 lines (101 loc) · 3.16 KB
/
sha256_check.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <openssl/sha.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define THREADS 128L
#define BATCH 78125000L // 1 0000 000 000 divided by 128
#define TOTALNUM 10000000000
struct hashPair {
long long int number;
unsigned char hash[32];
};
struct hashPair hashes[TOTALNUM];
static void printHash(unsigned char * hash) {
for (int i = 0; i < 32; ++i)
printf("%02X", *(hash + i));
putchar(' ');
}
static int compareHash(const void * x, const void * y) {
uint64_t * a, * b;
a = ((struct hashPair *) x)->hash;
b = ((struct hashPair *) y)->hash;
for (int i = 0; i < 4; ++i) {
if(a[i] < b[i]) return -1;
else if(a[i] > b[i]) return 1;
}
return 0;
}
static void * fillHash(void * threadId) {
unsigned char * number;
number = malloc(11);
for (long long int i = BATCH * (long long int) threadId; i < (BATCH * (1 + (long long int)threadId)); ++i) {
hashes[i].number = i;
snprintf(number, 11, "%010lld", i);
SHA256(number, 10, hashes[i].hash);
}
free(number);
qsort(&hashes[BATCH * (long long int) threadId], BATCH, sizeof(struct hashPair), compareHash);
return (void *) (intptr_t) 0;
}
static void * checkEquals(void * threadId) {
for (long long int i = BATCH * (long long int) threadId + 1; i < BATCH * (1 + (long long int) threadId); ++i) {
if(!compareHash((void *) &hashes[i], (void *) &hashes[i - 1])) {
flockfile(stdout);
printf("number %lld and %lld give the same hash:\n", hashes[i].number, hashes[i-1].number);
printHash(hashes[i].hash);
printHash(hashes[i-1].hash);
putchar('\n');
fflush(stdout);
funlockfile(stdout);
}
}
return (void *) (intptr_t) 0;
}
int main() {
time_t t0 = time(0);
pthread_t * myThreads;
myThreads = malloc(THREADS * sizeof(pthread_t));
for (int i = 0; i < THREADS; ++i)
pthread_create(&myThreads[i], NULL, fillHash, (void *) (intptr_t) i);
for (int i = 0; i < THREADS; ++i)
pthread_join(myThreads[i], NULL);
time_t t1 = time(0);
int diff = t1 - t0;
printf("\nI started sorting at: %02d:%02d\n\n", diff / 60, diff % 60);
fflush(stdout);
qsort(hashes, TOTALNUM, sizeof(struct hashPair), compareHash);
t1 = time(0);
diff = t1 - t0;
printf("\nI finished sorting at: %02d:%02d\n\n", diff / 60, diff % 60);
fflush(stdout);
for (int i = 0; i < THREADS; ++i)
pthread_create(&myThreads[i], NULL, checkEquals, (void *) (intptr_t) i);
for (int i = 0; i < THREADS; ++i)
pthread_join(myThreads[i], NULL);
for (long long int i = 1; i < THREADS; ++i) {
if(!compareHash((void *) &hashes[i * BATCH], (void *) &hashes[i * BATCH - 1])) {
printf("number %lld and %lld give the same hash:\n", hashes[i].number, hashes[i-1].number);
printHash(hashes[i].hash);
printHash(hashes[i-1].hash);
putchar('\n');
fflush(stdout);
}
}
free(myThreads);
t1 = time(0);
diff = t1 - t0;
printf("\nIt took: %02d:%02d\n\n", diff / 60, diff % 60);
// If somebody needs to print or save certain hashes
/* unsigned char * number;
number = malloc(11);
for (long long int i = 0; i < TOTALNUM; ++i) {
snprintf(number, 11, "%010lld", hashes[i].number);
fprintf(stdout, "Number %s:\n", number);
printHash(hashes[i].hash);
putc('\n', stdout);
}
free(number); */
return 0;
}