-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainbow.c
More file actions
121 lines (88 loc) · 2.77 KB
/
Copy pathrainbow.c
File metadata and controls
121 lines (88 loc) · 2.77 KB
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
116
117
118
119
120
121
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <openssl/evp.h>
#include <string.h>
#define THREADS 4
#define LINES_SIZE 10000
#define BATCH_SIZE (LINES_SIZE / THREADS)
volatile unsigned found = 0;
typedef struct thread_conf {
char** buffer;
char* sha256_value;
} thread_conf;
void sha256(const char *str, char *output) {
EVP_MD_CTX *context = EVP_MD_CTX_new();
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int length = 0;
EVP_DigestInit_ex(context, EVP_sha256(), NULL);
EVP_DigestUpdate(context, str, strlen(str));
EVP_DigestFinal_ex(context, hash, &length);
EVP_MD_CTX_free(context);
for(int i = 0; i < length; i++) {
sprintf(output + (i * 2), "%02x", hash[i]);
}
output[64] = '\0';
}
void* thread_process_line(void* arg) {
thread_conf* conf = (thread_conf*) arg;
for (unsigned int i = 0; i < BATCH_SIZE; i++) {
if (found == 1) {
return NULL;
}
if (conf->buffer[i] != NULL) {
char sha256_result[65];
sha256(conf->buffer[i], sha256_result);
if (strcmp(sha256_result, conf->sha256_value) == 0) {
printf("We have a match ! value %s : sha256: %s\n", conf->buffer[i], sha256_result);
found = 1;
}
}
}
return NULL;
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("Usage: %s <in> <sha256>\n", argv[0]);
return 1;
}
for (unsigned short sha_count = 2; sha_count < argc; sha_count ++) {
found = 0;
FILE* in = fopen(argv[1], "r");
if (in == NULL) {
perror("fopen in");
return 2;
}
char* lines[LINES_SIZE];
size_t n = 0;
unsigned int count = 0;
for (int i = 0; i < LINES_SIZE; i++) {
lines[i] = NULL;
}
while(!feof(in)) {
count = 0;
while(count < LINES_SIZE && (getline(&lines[count], &n, in) != -1)) {
count ++;
n = 0;
}
pthread_t threads[THREADS];
for (unsigned int i = 0; i < THREADS; i ++) {
thread_conf conf = {
.buffer = &lines[BATCH_SIZE * i],
.sha256_value = argv[sha_count]
};
pthread_create(&threads[i], NULL, thread_process_line, &conf);
}
for (unsigned int i = 0; i < THREADS; i ++) {
pthread_join(threads[i], NULL);
}
for (unsigned int i = 0; i < count; i ++) {
if (lines[i] != NULL) {
free(lines[i]);
}
}
}
fclose(in);
}
return 0;
}