This repository was archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_omp_mpi.c
More file actions
295 lines (264 loc) · 9.83 KB
/
Copy pathmain_omp_mpi.c
File metadata and controls
295 lines (264 loc) · 9.83 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include "md5.h"
#include <mpi.h>
#define KEY_NUMBER 3
#define HASH_NUMBER 9
#define SYSTEM_NUMBER 5
#define M 40000000
#define PASS_NUMBER 10
typedef struct FileLine FileLine;
struct FileLine {
char *content;
FileLine *previous;
FileLine *nextLine;
};
typedef struct FileContent FileContent;
struct FileContent {
FileLine *firstLine;
int length;
char *name;
};
/*
Permet de chercher si dans une liste de hash, notre chaine est dedans
*/
int findCorrespondance(FileContent *fileContent, char *toCompare) {
FileLine *fl = fileContent->firstLine;
while (fl != NULL) {
char *tmp = fl->content;
//printf("%s, %s\n",tmp,toCompare);
if (strcmp(tmp, toCompare) == 0)
return 1;
fl = fl->nextLine;
}
return 0;
}
/*
Creer un FileContent a partir du nom d'un fichier
*/
FileContent *readFile(char *name) {
FILE *file;
file = fopen(name, "r");
if (file == NULL) {
printf("Can't open file : %s\n", name);
exit(2);
}
printf("Lecture de %s\n", name);
FileContent *fileContent = malloc(sizeof(FileContent));
fileContent->length = 0;
fileContent->name = malloc(sizeof(char) * strlen(name));
strcpy(fileContent->name, name);
fileContent->firstLine = malloc(sizeof(FileLine));
char *line = NULL;
size_t len = 0;
ssize_t read;
FileLine *currentFileLine = fileContent->firstLine;
currentFileLine->previous = NULL;
while ((read = getline(&line, &len, file)) != -1) {
line[strlen(line) - 1] = 0;
currentFileLine->content = malloc(sizeof(char) * strlen(line));
strcpy(currentFileLine->content, line);
currentFileLine->nextLine = malloc(sizeof(FileLine));
FileLine *tmp = currentFileLine;
currentFileLine = currentFileLine->nextLine;
currentFileLine->previous = tmp;
fileContent->length++;
}
currentFileLine = currentFileLine->previous;
free(currentFileLine->nextLine);
currentFileLine->nextLine = NULL;
fclose(file);
return fileContent;
}
/*
Affiche le contenu du fichier
*/
void displayFileContent(FileContent *fileContent) {
printf("Fichier de %d lignes\n", fileContent->length);
FileLine *fl = fileContent->firstLine;
printf("%d\n", fl == NULL);
while (fl != NULL) {
printf("%s\n", fl->content);
fl = fl->nextLine;
}
}
int analyseSystem(int sysNum, unsigned char *keyHashs[KEY_NUMBER][HASH_NUMBER], char keys[KEY_NUMBER][20]) {
char file_name[128];
char* c[2];
memset(file_name, 0, sizeof(file_name));
sprintf(c, "%d", sysNum);
strncat(file_name, "res/systeme_", (sizeof(file_name) - strlen(file_name) - 1));
strncat(file_name, c, (sizeof(file_name) - strlen(file_name) - 1));
strncat(file_name, ".phl", (sizeof(file_name) - strlen(file_name) - 1));
FileContent* fileContent = readFile(file_name);
for (int i = 0; i < KEY_NUMBER; i++) {
for (int j = 0; j < HASH_NUMBER; j++) {
if (findCorrespondance(fileContent, keyHashs[i][j])) {
printf("Correspondance dans %s en i = %d pour %s et %s\n", fileContent->name, j + 1, keys[i],
keyHashs[i][j]);
return j + 1;
}
}
}
return -1;
}
void threadPrint(int tid, char *msg) {
printf("Thread %d: %s\n", tid, (char *) msg);
}
int main() {
int i = 0, tid, chunk = 1;
int m = M;
char tmp;
FileContent *passwordFile;
FileContent *systems[SYSTEM_NUMBER];
char file_name[128];
char keys[KEY_NUMBER][20];
char pass[PASS_NUMBER-KEY_NUMBER][20];
char c[3];
FileLine *fl;
MPI_Status status;
char *listPassword[PASS_NUMBER];
unsigned char *listResultat[KEY_NUMBER][HASH_NUMBER];
int timedHashed[SYSTEM_NUMBER];
int maxI = 0;
MPI_Init(NULL, NULL);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
for (int x = 0; x < SYSTEM_NUMBER; x++) {
memset(file_name, 0, sizeof(file_name));
sprintf(c, "%d", x + 1);
strncat(file_name, "res/systeme_", (sizeof(file_name) - strlen(file_name) - 1));
strncat(file_name, c, (sizeof(file_name) - strlen(file_name) - 1));
strncat(file_name, ".phl", (sizeof(file_name) - strlen(file_name) - 1));
systems[x] = readFile(file_name);
}
if(world_rank == 0){
passwordFile = readFile("res/high_frequency_passwords_list.txt");
fl= passwordFile->firstLine;
while (fl != NULL) {
listPassword[i] = malloc(sizeof(char) * strlen(fl->content));
strcpy(listPassword[i++], fl->content);
fl = fl->nextLine;
}
for(int j = 0;j<PASS_NUMBER;j++){
for(int k = 0;k< strlen(listPassword[j]);k++){
if(j<KEY_NUMBER)
MPI_Send(&listPassword[j][k],1,MPI_CHAR,1,1,MPI_COMM_WORLD);
else
MPI_Send(&listPassword[j][k],1,MPI_CHAR,2,1,MPI_COMM_WORLD);
}
int x= 0;
if(j<KEY_NUMBER)
MPI_Send(&x,1,MPI_CHAR,1,1,MPI_COMM_WORLD);
else
MPI_Send(&x,1,MPI_CHAR,2,1,MPI_COMM_WORLD);
}
for(int i = 0;i<SYSTEM_NUMBER;i++){
MPI_Recv(&timedHashed[i],1,MPI_INT,1,2,MPI_COMM_WORLD,&status);
MPI_Send(&timedHashed[i],1,MPI_INT,2,2,MPI_COMM_WORLD);
}
}
if(world_rank == 1) {
for(int i = 0;i<KEY_NUMBER;i++){
int j = 0;
while (1){
MPI_Recv(&tmp,1,MPI_CHAR,0,1,MPI_COMM_WORLD,&status);
keys[i][j++] = tmp;
if(tmp == 0)break;
}
}
#pragma omp parallel shared(chunk) private(i, tid)
{
tid = omp_get_thread_num();
if (tid == 0) {
printf("Nombre de threads actives : %d\n", omp_get_num_threads());
}
unsigned char *mon_hash = malloc(sizeof(unsigned char) * MD5_HASHBYTES); /* le MD5 renvoie 16 octets */
#pragma omp for schedule(static, chunk)
for (i = 0; i < KEY_NUMBER; i++) {
threadPrint(tid, keys[i]);
for (int ind = 1; ind <= HASH_NUMBER * m; ind++) {
if (ind == 1) {
calcul_md5(keys[i], strlen(keys[i]), mon_hash);
} else {
calcul_md5(mon_hash, MD5_HASHBYTES, mon_hash);
}
if ((ind) % m == 0) {
int index = ind / m - 1;
listResultat[i][index] = malloc(sizeof(unsigned char) * MD5_HASHBYTES * 2);
//printf("%d\n", index);
char *temp = malloc(sizeof(char) * 2);
for (int j = 0; j < MD5_HASHBYTES; j++) {
//printf("%.2x", mon_hash[j]); // Affichage sur deux caracteres
sprintf(temp, "%.2x", mon_hash[j]);
listResultat[i][index][j * 2] = temp[0];
listResultat[i][index][j * 2 + 1] = temp[1];
}
//printf("\n" );
//threadPrint(tid, listResultat[i][index]);
}
}
}
#pragma omp for schedule(static, chunk)
for (i = 0; i < SYSTEM_NUMBER; i++) {
timedHashed[i] = analyseSystem(i+1, listResultat, keys);
}
}
for(int i = 0;i<SYSTEM_NUMBER;i++)
MPI_Send(&timedHashed[i],1,MPI_INT,0,2,MPI_COMM_WORLD);
}
if(world_rank == 2){
for(int i = 0;i<PASS_NUMBER-KEY_NUMBER;i++){
int j = 0;
while (1){
MPI_Recv(&tmp,1,MPI_CHAR,0,1,MPI_COMM_WORLD,&status);
pass[i][j++] = tmp;
if(tmp == 0)break;
}
}
for(int i = 0;i<SYSTEM_NUMBER;i++){
MPI_Recv(&timedHashed[i],1,MPI_INT,0,2,MPI_COMM_WORLD,&status);}
#pragma omp parallel shared(chunk) private(i, tid)
{
tid = omp_get_thread_num();
unsigned char mon_hash[MD5_HASHBYTES]; /* le MD5 renvoie 16 octets */
#pragma omp for schedule(static, chunk)
for (i = 0; i < PASS_NUMBER-KEY_NUMBER; i++) {
printf("pass: %s\n",pass[i]);
for (int ind = 1; ind <= HASH_NUMBER * m; ind++) {
if (ind == 1) {
calcul_md5(pass[i], strlen(pass[i]), mon_hash);
} else {
calcul_md5(mon_hash, MD5_HASHBYTES, mon_hash);
}
if ((ind) % m == 0) {
int index = ind / m;
for (int k = 0; k < SYSTEM_NUMBER; k++) {
if (timedHashed[k] == index ) {
char temp[2];
char tmp[MD5_HASHBYTES * 2+1];
for (int j = 0; j < MD5_HASHBYTES; j++) {
//printf("%.2x", mon_hash[j]); // Affichage sur deux caracteres
sprintf(temp, "%.2x", mon_hash[j]);
tmp[j * 2] = temp[0];
tmp[j * 2 + 1] = temp[1];
}
tmp[MD5_HASHBYTES*2] = 0;
//printf("%d, i=%d %s\n",k+1,index,tmp);
if (findCorrespondance(systems[k], tmp)) {
printf("Password admin trouvé pour le systeme %d: %s\n",k+1,pass[i]);
}
}
}
}
}
}
}
}
printf("Fin machine 2 %d\n",world_rank);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}