Commit 019a9747 authored by Mathieu Valois's avatar Mathieu Valois
Browse files

move map display to Statistics

parent 405cb61e
Loading
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -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))
+0 −50
Original line number Diff line number Diff line
@@ -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
+16 −0
Original line number Diff line number Diff line
#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
+44 −0
Original line number Diff line number Diff line
@@ -87,3 +87,47 @@ std::ostream& operator<<(std::ostream& os, const Statistics& results){
	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
+1 −43
Original line number Diff line number Diff line
@@ -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);
}