-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword8ParallelRandomPasswordNOWAIT.cpp
More file actions
168 lines (154 loc) · 6.74 KB
/
Copy pathPassword8ParallelRandomPasswordNOWAIT.cpp
File metadata and controls
168 lines (154 loc) · 6.74 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
#include <atomic>
#include <chrono>
#include <crypt.h>
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <omp.h>
#include <random>
///////////////////////
/// MAIN FUNCTION ///
/////////////////////
int main(int argc, char* argv[]) {
//// THREAD SETUP ////
int num_threads;
int max_threads = 999;
if (argc > 1) {
num_threads = std::atoi(argv[1]);
if (num_threads <= 0 || num_threads > max_threads) {
std::cerr << "ERRORE: Numero di thread non valido!\n";
std::cerr << "Richiesto: " << num_threads << "\n";
std::cerr << "Massimo disponibile sul sistema: " << max_threads << "\n";
std::cerr << "Uso: " << argv[0] << " [num_threads]\n";
return 1;
}
} else {
std::cout << "=================================================\n";
std::cout << " Password Decryption - Brute Force Parallel\n";
std::cout << "=================================================\n";
std::cout << "Massimo numero di thread disponibili: " << max_threads << "\n";
std::cout << "Inserisci il numero di thread da utilizzare (1-" << max_threads << "): ";
std::cin >> num_threads;
std::cout << "\n";
}
if (num_threads == 1) {
std::cout << "\n⚠️ ATTENZIONE: Hai selezionato 1 thread!\n";
std::cout << "Per prestazioni ottimali con esecuzione sequenziale,\n";
std::cout << "usa il programma dedicato: ./Password8Sequenziale\n";
std::cout << "(versione pura senza overhead OpenMP)\n\n";
}
//// SET NUMBER OF THREADS ////
omp_set_num_threads(num_threads);
//// HASH VARIABLES ////
const std::string salt = "AB";
const char* salt_cstr = salt.c_str();
std::string found;
std::atomic<bool> found_flag(false);
bool correct = true;
static std::random_device rd;
static std::mt19937 gen(rd());
//// TIMER START ////
const auto start = std::chrono::high_resolution_clock::now();
//// MAIN LOOP VARIABLES ////
constexpr int NUM_ITER = 500;
constexpr long long PASSWORDS_PER_ITER = 32LL * 13 * 2026;
long long total_passwords_tested = 0;
//// MAIN LOOP ////
for (int i = 0; i < NUM_ITER; i++) {
int numberIter = 0;
found = "";
found_flag.store(false, std::memory_order_release);
/// GENERATE RANDOM PASSWORD TARGET ////
std::uniform_int_distribution<int> giorno(1, 31);
std::uniform_int_distribution<int> mese(1, 12);
std::uniform_int_distribution<int> anno(0, 2025);
const int g = giorno(gen);
const int m = mese(gen);
const int y = anno(gen);
char target_password[9];
target_password[0] = '0' + (g / 10);
target_password[1] = '0' + (g % 10);
target_password[2] = '0' + (m / 10);
target_password[3] = '0' + (m % 10);
target_password[4] = '0' + ((y / 1000) % 10);
target_password[5] = '0' + ((y / 100) % 10);
target_password[6] = '0' + ((y / 10) % 10);
target_password[7] = '0' + (y % 10);
target_password[8] = '\0';
/// CRYPT TARGET PASSWORD ////
const char* target = crypt(target_password, salt_cstr);
/// BRUTE FORCE SEARCH WITH OPENMP NOWAIT ////
#pragma omp parallel default(none) shared(salt_cstr, found, target, found_flag) firstprivate(numberIter) reduction(+:total_passwords_tested) num_threads(25)
{
//// BUFFER FOR crypt_r ////
struct crypt_data data{};
data.initialized = 0;
//// PARALLEL FOR WITH NOWAIT ////
#pragma omp for collapse(3) schedule(static) nowait
for (int a = 0; a <= 31; ++a) {
for (int b = 0; b <= 12; ++b) {
for (int c = 0; c <= 2025; ++c) {
if (found_flag.load(std::memory_order_relaxed)) {
continue; //// NO CANCELLATION POINT ////
}
numberIter++;
char date[9];
date[0] = '0' + (a / 10);
date[1] = '0' + (a % 10);
date[2] = '0' + (b / 10);
date[3] = '0' + (b % 10);
date[4] = '0' + ((c / 1000) % 10);
date[5] = '0' + ((c / 100) % 10);
date[6] = '0' + ((c / 10) % 10);
date[7] = '0' + (c % 10);
date[8] = '\0';
/// CRYPT GENERATED DATE ////
const char* h = crypt_r(date, salt_cstr, &data);
/// CHECK IF HASH MATCHES TARGET ////
if (h[3] == target[3] && h[4] == target[4]) {
if (strcmp(h, target) == 0) {
#pragma omp critical //// CRITICAL SECTION ////
{
if (!found_flag.load(std::memory_order_relaxed)) {
found = date;
found_flag.store(true, std::memory_order_release);
}
}
}
}
}
}
}
total_passwords_tested += numberIter;
}
}
//// TIMER END////
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
double passwords_per_second = total_passwords_tested / elapsed.count();
//////// STATISTICS OUTPUT /////////
std::cout << "========================================\n";
std::cout << "Elaborazione completata!\n";
std::cout << "========================================\n";
std::cout << "Tempo totale impiegato: " << std::fixed << std::setprecision(2) << elapsed.count() << " secondi\n";
std::cout << "Tempo medio per iterazione: " << std::fixed << std::setprecision(3) << (elapsed.count()/NUM_ITER) << " secondi\n";
std::cout << "Iterazioni totali: " << NUM_ITER << "\n";
std::cout << "Password testate totali: " << total_passwords_tested << "\n";
std::cout << "Password testate/secondo: " << std::fixed << std::setprecision(0) << passwords_per_second << "\n";
std::cout << "Thread utilizzati: " << num_threads << "\n";
std::cout << "========================================\n";
if (!found.empty()) {
printf("✓ Password trovata: %s\n", found.c_str());
} else {
printf("✗ Password non trovata\n");
correct=false;
}
//// VERIFY CORRECTNESS////
if (correct) {
std::cout << "✓ Tutte le password generate sono corrette\n";
} else {
std::cout << "✗ Alcune password generate non sono corrette\n";
}
return 0;
}