diff --git a/include/Statistics.h b/include/Statistics.h index 5b227e9581ce303be716b9f4285abea55e7fd3e8..eaea44f4c2bca42698532e14b44813196196e6bf 100644 --- a/include/Statistics.h +++ b/include/Statistics.h @@ -7,6 +7,7 @@ #include <regex> #include "SecurityRules.h" +#include "Utils.h" typedef std::unordered_map<std::string, uint64_t> StringOccurrence; typedef std::unordered_map<int, uint64_t> IntOccurrence; @@ -63,8 +64,58 @@ struct Statistics { StringOccurrence charactersets; minMax minMaxValue; + + void show(std::ostream& os, const Settings& settings) const; }; +/** + * @brief Print a line of statistics + * @param occ: value of this statistic + * @param mask: the statistic to display + * @param total_counter:total number of password analysed + * @param hiderare: flag to hide statistics under 1% + */ +template<typename Type> +void readResult(const uint64_t& occ, const Type& mask, const uint64_t& total_counter, const int& hiderare) { + if(occ == 0) return; + std::ostringstream ss; + float perc = percentage(occ, total_counter); + + if (perc >= hiderare) { + ss << perc; + std::string value(ss.str()); + value = value.substr(0,5); + + std::cout << std::setw(40) << std::right << mask << ": " + << std::setw(5) << std::right << value << "%" + << std::setw(5) << std::right << "(" << occ << ")" << std::endl; + } +} + +/** + * @brief Print an unordered map + * @param stats: map to show + * @param top: number of results to show + * @param total_counter: number of finded passwords + * @param hiderare: low statistics to hide + * @param count: number of shown results + */ +template<typename Type> +void showMap(const std::unordered_map<Type, uint64_t> & stats, const int & top, const uint64_t & total_counter, const int & hiderare) { + int count = 0; + std::multimap<uint64_t, Type, std::greater<uint64_t>> reverse = flip_map<Type>(stats); + std::pair<uint64_t, Type> it; + for(std::pair<uint64_t, Type> it : reverse) { + readResult<Type>(it.first, it.second, total_counter, hiderare); + count++; + if (top != -1 && count == top) break; + } + + if (count != top) { + readResult<Type>(it.first, it.second, total_counter, hiderare); + } +} + std::ostream& operator<<(std::ostream& os, const Statistics& results); #pragma omp declare reduction(dataSum: Statistics : omp_out += omp_in ) initializer(omp_priv(omp_orig)) diff --git a/include/Utils.h b/include/Utils.h index 718ffa7ec9f80e8b98e730066578b16575272fd6..ee1abf1b92c165b2f5dbfe4686c248421ddbc99f 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -41,56 +41,6 @@ std::multimap<B, A, std::greater<B>> flip_map(const std::unordered_map<A, B>& sr return dst; } -/** - * @brief Print a line of statistics - * @param res: value of this statistic - * @param crac: the statistic to display - * @param count: the number of statistics already displayed - * @param total_counter:total number of password analysed - * @param hiderare: flag to hide statistics under 1% - */ -template<typename Type> -void readResult(const uint64_t & res, const Type& carac, int & count, const uint64_t & total_counter, const int & hiderare) { - if(res == 0) return; - std::ostringstream ss; - float perc = percentage(res, total_counter); - - if (perc >= hiderare) { - ss << perc; - std::string value(ss.str()); - value = value.substr(0,5); - - std::cout << std::setw(40) << std::right << carac << ": " - << std::setw(5) << std::right << value << "%" - << std::setw(5) << std::right << "(" << res << ")" << std::endl; - - count++; - } -} - -/** - * @brief Print an unordered map - * @param stats: map to show - * @param top: number of results to show - * @param total_counter: number of finded passwords - * @param hiderare: low statistics to hide - * @param count: number of shown results - */ -template<typename Type> -void showMap(const std::unordered_map<Type, uint64_t> & stats, const int & top, const uint64_t & total_counter, const int & hiderare, int & count) { - count = 0; - std::multimap<uint64_t, Type, std::greater<uint64_t>> reverse = flip_map<Type>(stats); - std::pair<uint64_t, Type> it; - for(std::pair<uint64_t, Type> it : reverse) { - readResult<Type>(it.first, it.second, count, total_counter, hiderare); - if (top != -1 && count == top) break; - } - - if (count != top) { - readResult<Type>(it.first, it.second, count, total_counter, hiderare); - } -} - /** * @brief Calculate the number of line in a file * @param filename: name of the file diff --git a/src/core/SecurityRules.cpp b/src/core/SecurityRules.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a17d7f4e5c8c13797b3b5798d5e241695ff3c6dd --- /dev/null +++ b/src/core/SecurityRules.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <iomanip> + +#include "SecurityRules.h" + +using namespace std; + +std::ostream& operator<<(std::ostream& os, const SecurityRules& sec){ + os << "Selected security rules : " << endl; + os << setw(42) << "Length: " << sec.minLength << "+" << endl; + os << setw(42) << "Special: " << sec.minSpecial << "+" << endl; + os << setw(42) << "Digits: " << sec.minDigit << "+" << endl; + os << setw(42) << "Lowercase: " << sec.minLower << "+" << endl; + os << setw(42) << "Uppercase: " << sec.minUpper << "+" << endl; + return os; +} \ No newline at end of file diff --git a/src/core/Statistics.cpp b/src/core/Statistics.cpp index 7feff686fee6d44b7223731c553e0d4c3673b29c..ec9e2aba351dec1a0abf8036297c08ad87db63bc 100644 --- a/src/core/Statistics.cpp +++ b/src/core/Statistics.cpp @@ -86,4 +86,48 @@ std::ostream& operator<<(std::ostream& os, const Statistics& results){ os << setw(42) << "upper: " << m.minupper << " - " << m.maxupper << endl; os << setw(42) << "special: " << m.minspecial << " - " << m.maxspecial << endl; return os; +} + +void Statistics::show(std::ostream& os, const Settings& settings) const { + os << *this << endl; + os << endl << "Statistics relative to length: " << endl; + showMap(length, settings.top, total_counter, settings.hiderare); + + os << endl << "Statistics relative to charsets: " << endl; + showMap(charactersets, -1, total_counter, settings.hiderare); + + + os << endl << "Statistics relative to simplemasks: " << endl; + showMap(simplemasks, settings.top, total_counter, settings.hiderare); + + if (settings.limitSimplemask > 0) { + os << endl; + auto r = simplemasks.find("othermasks"); + if(r != simplemasks.end()){ + readResult(r->second, r->first, total_counter, settings.hiderare); + } + } + + + os << endl << "Statistics relative to advancedmask: " << endl; + showMap(advancedmasks, settings.top, total_counter, settings.hiderare); + + if (! settings.outfile_name.empty()){ + locale::global(locale("C")); + ofstream outfile_stream(settings.outfile_name); + multimap<uint64_t, string, greater<uint64_t>> reverse = flip_map(advancedmasks); + for(pair<uint64_t, string> it : reverse){ + if(it.second == "othermasks") continue; + outfile_stream << it.second << "," << it.first << endl; + } + outfile_stream.close(); + } + + if (settings.limitAdvancedmask > 0) { + os << endl; + auto r = advancedmasks.find("othermasks"); + if(r != advancedmasks.end()){ + readResult(r->second, r->first, total_counter, settings.hiderare); + } + } } \ No newline at end of file diff --git a/src/core/Statsgen.cpp b/src/core/Statsgen.cpp index 2ea59698c9deb5631fa9cc1f1f35eaf9ebc342d1..3b92dc19553108d70befd215304dfe7462d43df2 100644 --- a/src/core/Statsgen.cpp +++ b/src/core/Statsgen.cpp @@ -75,50 +75,8 @@ int Statsgen::generate_stats() { } void Statsgen::print_stats() const { - int count; cout << settings.sr << endl; - cout << results << endl; - - cout << endl << "Statistics relative to length: " << endl; - showMap(results.length, settings.top, results.total_counter, settings.hiderare, count); - - cout << endl << "Statistics relative to charsets: " << endl; - showMap(results.charactersets, -1, results.total_counter, settings.hiderare, count); - - - cout << endl << "Statistics relative to simplemasks: " << endl; - showMap(results.simplemasks, settings.top, results.total_counter, settings.hiderare, count); - - if (settings.limitSimplemask > 0) { - cout << endl; - auto r = results.simplemasks.find("othermasks"); - if(r != results.simplemasks.end()){ - readResult(r->second, r->first, count, results.total_counter, settings.hiderare); - } - } - - - cout << endl << "Statistics relative to advancedmask: " << endl; - showMap(results.advancedmasks, settings.top, results.total_counter, settings.hiderare, count); - - if (! settings.outfile_name.empty()){ - locale::global(locale("C")); - ofstream outfile_stream(settings.outfile_name); - multimap<uint64_t, string, greater<uint64_t>> reverse = flip_map(results.advancedmasks); - for(pair<uint64_t, string> it : reverse){ - if(it.second == "othermasks") continue; - outfile_stream << it.second << "," << it.first << endl; - } - outfile_stream.close(); - } - - if (settings.limitAdvancedmask > 0) { - cout << endl; - auto r = results.advancedmasks.find("othermasks"); - if(r != results.advancedmasks.end()){ - readResult(r->second, r->first, count, results.total_counter, settings.hiderare); - } - } + results.show(cout, settings); }