【发布时间】:2020-10-27 04:12:31
【问题描述】:
我怎样才能使这个蛮力算法多线程?如果我启动它,它只使用一个 cpu。我怎样才能并行化这个?这似乎是不可能的。对我来说,fork 或 pthread 都可以。这段代码对哈希进行暴力破解,生成所有可能的字符串,生成哈希并与摘要进行比较。
#include <stdio.h>
#include <string.h>
#include "attacchi.h"
#include "hash.h"
void iterazione(char *stringa, int index, int lunghezza);
char *checksum,*hashType;
void bruteforce(char digest[],char tipohash[]) {
checksum = malloc(sizeof(char)*1024);
hashType = malloc(sizeof(char)*1024);
strcpy(checksum,digest);
strcpy(hashType,tipohash);
int lunghezza, i;
printf("Inserire la lunghezza massima da testare: ");
scanf("%d", &lunghezza);
char stringa[lunghezza + 1];
memset(stringa, 0, lunghezza + 1);
for (i = 1; i <= lunghezza; i++) {
iterazione(stringa, 0, lunghezza);
}
}
void iterazione(char *stringa, int index, int lunghezza) {
char c;
if (index < (lunghezza - 1)) {
for (c = ' '; c <= '~'; ++c) {
stringa[index] = c;
iterazione(stringa, index + 1, lunghezza);
}
} else {
for (c = ' '; c <= '~'; ++c) {
stringa[index] = c;
stringa[index+1] = '\n';
if(strcmp(hash(stringa,hashType),checksum)==0) {
printf("Trovato!\nhash %s %s -> %s\n", checksum, hashType, stringa);
exit(0);
}
}
}
}
【问题讨论】:
标签: c multithreading pthreads fork brute-force