-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
77 lines (70 loc) · 2.17 KB
/
thread.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
/*
* Authors: LOPES Marco, ISELI Cyril and RINGOT Gaëtan
* Purpose: Management of threads.
* Language: C
* Date : 2 november 2016
*/
#define _GNU_SOURCE
#include <crypt.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "string.h"
#include "threadprivate.h"
/*
* Generate a number of threads
*
* hash: hash to bruteforce
* numberThreads: number of threads to perform the bruteforce
*
*/
void createThreads(const char *hash, int numberThreads) {
pthread_t threads[numberThreads];
paramsSt paramsThread[numberThreads];
int found = 0;
for (int i = 0; i < numberThreads; i++) {
paramsThread[i].idThread = i;
paramsThread[i].numberThreads = numberThreads;
strncpy(paramsThread[i].hash, hash, LENGTH_HASH);
paramsThread[i].hash[LENGTH_HASH] = '\0';
strncpy(paramsThread[i].salt, hash, LENGTH_SALT);
paramsThread[i].salt[LENGTH_SALT] = '\0';
paramsThread[i].found = &found;
int code = pthread_create(&threads[i], NULL, thread, ¶msThread[i]);
if (code != 0) {
fprintf(stderr, "pthread_create failed!\n");
}
}
for (int i = 0; i < numberThreads; i++) {
if (pthread_join(threads[i], NULL) != 0) {
perror("pthread_join");
}
}
if (!found)
printf("No match found\n");
}
/*
* Task of a single thread
*
* paramsThread: struct which contain all information to perform the task
*
*/
void *thread(void *paramsThread) {
paramsSt *params = (paramsSt *) paramsThread;
struct crypt_data cryptData;
cryptData.initialized = 0;
unsigned long long int i = params->idThread + 1;
char *password = NULL;
do {
password = jumpToAlphabet(i, params->password);
char *hash = crypt_r(password, params->salt, &cryptData);
if (strcmp(hash, params->hash) == 0) {
printf("password = %s\n", password);
*params->found = 1;
return NULL;
}
i += params->numberThreads;
} while (!*params->found && strlen(password) <= LENGTH_MAX);
return NULL;
}