Skip to content
Snippets Groups Projects
Commit 169ef6ff authored by Mathieu Valois's avatar Mathieu Valois
Browse files

Use more oriented-objet code

parent 21487cbd
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ set(CMAKE_LD_FLAGS "-lpthread") ...@@ -16,6 +16,7 @@ set(CMAKE_LD_FLAGS "-lpthread")
add_executable(cppack add_executable(cppack
src/core/Statsgen.cpp src/core/Statsgen.cpp
src/core/Policy.cpp src/core/Policy.cpp
src/core/ThreadData.cpp
src/cli/Main.cpp) src/cli/Main.cpp)
target_link_libraries(cppack ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(cppack ${CMAKE_THREAD_LIBS_INIT})
...@@ -42,6 +43,7 @@ if(Qt5Core_FOUND) ...@@ -42,6 +43,7 @@ if(Qt5Core_FOUND)
src/gui/MainGui.cpp src/gui/MainGui.cpp
src/core/Statsgen.cpp src/core/Statsgen.cpp
src/core/Policy.cpp src/core/Policy.cpp
src/core/ThreadData.cpp
ressources/logos.qrc ressources/logos.qrc
) )
......
...@@ -16,21 +16,15 @@ ...@@ -16,21 +16,15 @@
#include <string> #include <string>
#include <unordered_map>
#include <iostream> #include <iostream>
#include <regex>
#include <queue>
#include <thread> #include <thread>
#include <climits>
#include "Policy.h" #include "Policy.h"
#include "SecurityRules.h" #include "SecurityRules.h"
#include "ThreadData.h"
#define MAX_THREADS 32 #define MAX_THREADS 32
typedef std::unordered_map<std::string, uint64_t> StringOccurrence;
typedef std::unordered_map<int, uint64_t> IntOccurrence;
struct PasswordStats { struct PasswordStats {
int pass_length = 0; int pass_length = 0;
...@@ -39,56 +33,6 @@ struct PasswordStats { ...@@ -39,56 +33,6 @@ struct PasswordStats {
Policy pol; Policy pol;
}; };
/**
* @brief Simplify number of arguments for functions
* minimal number of digits of a password, etc.
*/
struct minMax {
uint mindigit = UINT_MAX;
uint maxdigit = 0;
uint minlower = UINT_MAX;
uint maxlower = 0;
uint minupper = UINT_MAX;
uint maxupper = 0;
uint minspecial = UINT_MAX;
uint maxspecial = 0;
};
/**
* @brief All needed variables to give to each thread
*/
struct thread_data {
int thread_id;
std::string filename;
uint64_t lineBegin = 0;
uint64_t lineEnd = 0;
uint64_t total_counter = 0;
uint64_t total_filter = 0;
IntOccurrence length;
StringOccurrence simplemasks;
StringOccurrence advancedmasks;
StringOccurrence charactersets;
std::queue<std::string> password_queue;
minMax minMaxValue;
std::regex current_regex;
bool use_regex = false;
bool withcount = false;
uint limitSimplemask;
uint limitAdvancedmask;
SecurityRules sr;
bool finished = false;
};
/** /**
* @brief Calcul and save all analysed statistics * @brief Calcul and save all analysed statistics
*/ */
...@@ -158,8 +102,7 @@ public: ...@@ -158,8 +102,7 @@ public:
} }
} }
void configureThread(thread_data& td) const; void configureThread(ThreadData& td) const;
void mergeThread(const thread_data& td);
/** /**
...@@ -200,11 +143,11 @@ public: ...@@ -200,11 +143,11 @@ public:
inline const StringOccurrence& getStatsCharsets() const { return stats_charactersets; } inline const StringOccurrence& getStatsCharsets() const { return stats_charactersets; }
inline const StringOccurrence& getStatsSimple() const { return stats_simplemasks; } inline const StringOccurrence& getStatsSimple() const { return stats_simplemasks; }
inline const StringOccurrence& getStatsAdvanced() const { return stats_advancedmasks; } inline const StringOccurrence& getStatsAdvanced() const { return stats_advancedmasks; }
inline const thread_data* getThreadsData() const { return td; } inline const ThreadData* getThreadsData() const { return td; }
private: private:
std::string filename; std::string filename;
struct thread_data td[MAX_THREADS]; ThreadData td[MAX_THREADS];
// Filters // Filters
......
...@@ -52,7 +52,7 @@ void Statsgen::setSecurityRules(const uint& length, const uint& special, const u ...@@ -52,7 +52,7 @@ void Statsgen::setSecurityRules(const uint& length, const uint& special, const u
_sr = { _sr.nbSecurePassword, length, special, digit, upper, lower }; _sr = { _sr.nbSecurePassword, length, special, digit, upper, lower };
} }
void Statsgen::configureThread(thread_data& td) const { void Statsgen::configureThread(ThreadData& td) const {
td.filename = filename; td.filename = filename;
td.current_regex = current_regex; td.current_regex = current_regex;
td.use_regex = use_regex; td.use_regex = use_regex;
...@@ -62,44 +62,6 @@ void Statsgen::configureThread(thread_data& td) const { ...@@ -62,44 +62,6 @@ void Statsgen::configureThread(thread_data& td) const {
td.sr = { 0, _sr.minLength, _sr.minSpecial, _sr.minDigit, _sr.minLower, _sr.minUpper }; td.sr = { 0, _sr.minLength, _sr.minSpecial, _sr.minDigit, _sr.minLower, _sr.minUpper };
} }
void Statsgen::mergeThread(const thread_data& td){
total_counter += td.total_counter;
total_filter += td.total_filter;
Policy min, max;
min.digit = td.minMaxValue.mindigit;
min.lower = td.minMaxValue.minlower;
min.upper = td.minMaxValue.minupper;
min.special = td.minMaxValue.minspecial;
max.digit = td.minMaxValue.maxdigit;
max.lower = td.minMaxValue.maxlower;
max.upper = td.minMaxValue.maxupper;
max.special = td.minMaxValue.maxspecial;
_sr.nbSecurePassword += td.sr.nbSecurePassword;
updateMinMax(minMaxValue, min);
updateMinMax(minMaxValue, max);
for(pair<int, uint64_t> occ: td.length){
stats_length[occ.first] += occ.second;
}
for(pair<string, int> occ : td.charactersets){
stats_charactersets[occ.first] += occ.second;
}
for(pair<string, int> occ: td.simplemasks){
stats_simplemasks[occ.first] += occ.second;
}
for(pair<string, int> occ: td.advancedmasks){
stats_advancedmasks[occ.first] += occ.second;
}
}
int Statsgen::generate_stats() { int Statsgen::generate_stats() {
uint64_t nbline = 0; uint64_t nbline = 0;
if (!is_stdin){ if (!is_stdin){
...@@ -162,10 +124,12 @@ int Statsgen::generate_stats() { ...@@ -162,10 +124,12 @@ int Statsgen::generate_stats() {
exit(-1); exit(-1);
} }
td[i].finished = true; td[i].finished = true;
mergeThread(td[i]); if(i >= 1){
td[0] += td[i];
}
} }
if (!total_counter) { if (!td[0].total_counter) {
cerr << "[ERROR] Empty file or not existing file" << endl; cerr << "[ERROR] Empty file or not existing file" << endl;
return 0; return 0;
} }
...@@ -175,59 +139,60 @@ int Statsgen::generate_stats() { ...@@ -175,59 +139,60 @@ int Statsgen::generate_stats() {
void Statsgen::print_stats() { void Statsgen::print_stats() {
ThreadData& data = td[0];
int count; int count;
float perc = percentage(total_filter, total_counter); float perc = percentage(data.total_filter, data.total_counter);
cout << "\n\tSelected " << total_filter << " on " << total_counter << " passwords\t(" cout << "\n\tSelected " << data.total_filter << " on " << data.total_counter << " passwords\t("
<< perc << " %)" << endl; << perc << " %)" << endl;
cout << "\nSecurity rules : " << endl; cout << "\nSecurity rules : " << endl;
cout << "\tMinimal length of a password: " << _sr.minLength << endl; cout << "\tMinimal length of a password: " << data.sr.minLength << endl;
cout << "\tMinimum of special characters in a password: " << _sr.minSpecial << endl; cout << "\tMinimum of special characters in a password: " << data.sr.minSpecial << endl;
cout << "\tMinimum of digits in a password: " << _sr.minDigit << endl; cout << "\tMinimum of digits in a password: " << data.sr.minDigit << endl;
cout << "\tMinimum of lower characters in a password: " << _sr.minLower << endl; cout << "\tMinimum of lower characters in a password: " << data.sr.minLower << endl;
cout << "\tMinimum of upper characters in a password: " << _sr.minUpper << endl; cout << "\tMinimum of upper characters in a password: " << data.sr.minUpper << endl;
float perce = percentage(_sr.nbSecurePassword, total_counter); float perce = percentage(data.sr.nbSecurePassword, data.total_counter);
cout << "\n\t\t--> " << _sr.nbSecurePassword << " passwords\t(" << perce << " %) respect the security rules\n" << endl; cout << "\n\t\t--> " << data.sr.nbSecurePassword << " passwords\t(" << perce << " %) respect the security rules\n" << endl;
cout << "\nmin - max\n" << endl; cout << "\nmin - max\n" << endl;
cout << setw(43) << right << "digit: " cout << setw(43) << right << "digit: "
<< setw(2) << right << minMaxValue.mindigit << " - " << minMaxValue.maxdigit << endl; << setw(2) << right << data.minMaxValue.mindigit << " - " << data.minMaxValue.maxdigit << endl;
cout << setw(43) << right << "lower: " cout << setw(43) << right << "lower: "
<< setw(2) << right << minMaxValue.minlower << " - " << minMaxValue.maxlower << endl; << setw(2) << right << data.minMaxValue.minlower << " - " << data.minMaxValue.maxlower << endl;
cout << setw(43) << right << "upper: " cout << setw(43) << right << "upper: "
<< setw(2) << right << minMaxValue.minupper << " - " << minMaxValue.maxupper << endl; << setw(2) << right << data.minMaxValue.minupper << " - " << data.minMaxValue.maxupper << endl;
cout << setw(43) << right << "special: " cout << setw(43) << right << "special: "
<< setw(2) << right << minMaxValue.minspecial << " - " << minMaxValue.maxspecial << endl; << setw(2) << right << data.minMaxValue.minspecial << " - " << data.minMaxValue.maxspecial << endl;
cout << "\nStatistics relative to length: \n" << endl; cout << "\nStatistics relative to length: \n" << endl;
showMap(stats_length, top, total_counter, hiderare, count); showMap(data.length, top, data.total_counter, hiderare, count);
cout << "\nStatistics relative to charsets: \n" << endl; cout << "\nStatistics relative to charsets: \n" << endl;
showMap(stats_charactersets, -1, total_counter, hiderare, count); showMap(data.charactersets, -1, data.total_counter, hiderare, count);
cout << "\nStatistics relative to simplemasks: \n" << endl; cout << "\nStatistics relative to simplemasks: \n" << endl;
showMap(stats_simplemasks, top, total_counter, hiderare, count); showMap(data.simplemasks, top, data.total_counter, hiderare, count);
if (limitSimplemask > 0) { if (limitSimplemask > 0) {
cout << endl; cout << endl;
readResult(stats_simplemasks["othermasks"], "othermasks", count, total_counter, hiderare); readResult(data.simplemasks["othermasks"], "othermasks", count, data.total_counter, hiderare);
} }
cout << "\nStatistics relative to advancedmask: \n" << endl; cout << "\nStatistics relative to advancedmask: \n" << endl;
showMap(stats_advancedmasks, top, total_counter, hiderare, count); showMap(data.advancedmasks, top, data.total_counter, hiderare, count);
if (! outfile_name.empty()){ if (! outfile_name.empty()){
locale::global(locale("C")); locale::global(locale("C"));
ofstream outfile_stream(outfile_name); ofstream outfile_stream(outfile_name);
map<uint64_t, string, greater<uint64_t>> reverse = flip_map(stats_advancedmasks); map<uint64_t, string, greater<uint64_t>> reverse = flip_map(data.advancedmasks);
for(pair<uint64_t, string> it : reverse){ for(pair<uint64_t, string> it : reverse){
if(it.second == "othermasks") continue; if(it.second == "othermasks") continue;
outfile_stream << it.second << "," << it.first << endl; outfile_stream << it.second << "," << it.first << endl;
...@@ -237,7 +202,7 @@ void Statsgen::print_stats() { ...@@ -237,7 +202,7 @@ void Statsgen::print_stats() {
if (limitAdvancedmask > 0) { if (limitAdvancedmask > 0) {
cout << endl; cout << endl;
readResult(stats_advancedmasks["othermasks"], "othermasks", count, total_counter, hiderare); readResult(data.advancedmasks["othermasks"], "othermasks", count, data.total_counter, hiderare);
} }
} }
...@@ -290,18 +255,7 @@ pair<uint, uint> get_masks(const string& password, PasswordStats& c){ ...@@ -290,18 +255,7 @@ pair<uint, uint> get_masks(const string& password, PasswordStats& c){
return make_pair(sizeSimpleMask, sizeAdvancedMask); return make_pair(sizeSimpleMask, sizeAdvancedMask);
} }
void updateMinMax(minMax& m, const Policy& pol) { void handle_password(const string& password, const uint64_t& nbPasswords, ThreadData* my_data){
m.mindigit = min(m.mindigit, pol.digit);
m.maxdigit = max(m.maxdigit, pol.digit);
m.minlower = min(m.minlower, pol.lower);
m.maxlower = max(m.maxlower, pol.lower);
m.minupper = min(m.minupper, pol.upper);
m.maxupper = max(m.maxupper, pol.upper);
m.minspecial = min(m.minspecial, pol.special);
m.maxspecial = max(m.maxspecial, pol.special);
}
void handle_password(const string& password, const uint64_t& nbPasswords, thread_data* my_data){
my_data->total_counter += nbPasswords; my_data->total_counter += nbPasswords;
if(my_data->use_regex && !regex_match(password,my_data->current_regex)){ if(my_data->use_regex && !regex_match(password,my_data->current_regex)){
return; return;
...@@ -326,11 +280,11 @@ void handle_password(const string& password, const uint64_t& nbPasswords, thread ...@@ -326,11 +280,11 @@ void handle_password(const string& password, const uint64_t& nbPasswords, thread
my_data->charactersets[ c.pol ] += nbPasswords; my_data->charactersets[ c.pol ] += nbPasswords;
my_data->simplemasks[ c.simplemask_string ] += nbPasswords; my_data->simplemasks[ c.simplemask_string ] += nbPasswords;
my_data->advancedmasks[ c.advancedmask_string ] += nbPasswords; my_data->advancedmasks[ c.advancedmask_string ] += nbPasswords;
updateMinMax(my_data->minMaxValue, c.pol); my_data->minMaxValue.updateMinMax(c.pol);
} }
void* generate_stats_thread_queue(void* threadarg) { void* generate_stats_thread_queue(void* threadarg) {
struct thread_data *my_data = (struct thread_data *) threadarg; ThreadData *my_data = (ThreadData *) threadarg;
string line; string line;
uint64_t nbline = 0; uint64_t nbline = 0;
...@@ -346,7 +300,7 @@ void* generate_stats_thread_queue(void* threadarg) { ...@@ -346,7 +300,7 @@ void* generate_stats_thread_queue(void* threadarg) {
} }
void* generate_stats_thread(void* threadarg) { void* generate_stats_thread(void* threadarg) {
struct thread_data* my_data = (struct thread_data *) threadarg; ThreadData* my_data = (ThreadData *) threadarg;
ifstream readfile(my_data->filename); ifstream readfile(my_data->filename);
uint64_t nbline = 0; uint64_t nbline = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment