-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.c
96 lines (86 loc) · 2.5 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "sha1.h"
#include "lstack.h"
struct job {
lstack_t *results;
size_t min_results, nzeros;
};
const char EMPTY[SHA1_DIGEST_SIZE] = {0};
void print_hash(unsigned char hash[SHA1_DIGEST_SIZE])
{
for (int i = 0; i < SHA1_DIGEST_SIZE; i++)
printf("%02x", hash[i]);
putchar('\n');
}
void fillrandom(unsigned char *buffer, size_t n)
{
FILE *urandom = fopen("/dev/urandom", "r");
if (urandom == NULL || !fread(buffer, n, 1, urandom))
abort();
fclose(urandom);
}
void hash(uint8_t in[SHA1_DIGEST_SIZE], uint8_t out[SHA1_DIGEST_SIZE])
{
SHA1_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, in, SHA1_DIGEST_SIZE);
SHA1_Final(&ctx, out);
}
void *worker(void *arg)
{
struct job *job = arg;
uint8_t buffer[SHA1_DIGEST_SIZE], output[SHA1_DIGEST_SIZE];
fillrandom(buffer, sizeof(buffer)); // seed
while (lstack_size(job->results) < job->min_results) {
hash(buffer, output);
if (memcmp(output, EMPTY, job->nzeros) == 0) {
unsigned char *copy = malloc(SHA1_DIGEST_SIZE);
if (copy == NULL)
abort();
memcpy(copy, buffer, SHA1_DIGEST_SIZE);
if (lstack_push(job->results, copy) != 0)
abort();
}
memcpy(buffer, output, SHA1_DIGEST_SIZE);
}
return NULL;
}
int main()
{
int nthreads = sysconf(_SC_NPROCESSORS_ONLN); // Linux
lstack_t results;
struct job job = {
.results = &results,
.min_results = 4096,
.nzeros = 2
};
lstack_init(&results, job.min_results + nthreads);
/* Spawn threads. */
pthread_t threads[nthreads];
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
for (int i = 0; i < nthreads; i++)
pthread_create(threads + i, NULL, worker, &job);
/* Monitor progress */
size_t nresults, last = -1;
while ((nresults = lstack_size(&results)) < job.min_results) {
if (nresults != last)
fprintf(stderr, "\033[0GFound %zd hashes ...", nresults);
last = nresults;
sleep(1);
}
for (int i = 0; i < nthreads; i++)
pthread_join(threads[i], NULL);
nresults = lstack_size(&results);
fprintf(stderr, "\033[0G\033[KFound %zd hashes.\n", nresults);
/* Print results */
unsigned char *content;
while ((content = lstack_pop(&results)) != NULL) {
print_hash(content);
free(content);
}
return 0;
}