Commit a9dfc25b authored by Mathieu Valois's avatar Mathieu Valois
Browse files

Use Settings structure to split settings and results

parent 5a9a8bbe
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@


struct SecurityRules {
	uint64_t nbSecurePassword;
	uint minLength;
	uint minSpecial;
	uint minDigit;
+16 −10
Original line number Diff line number Diff line
@@ -33,15 +33,29 @@ struct minMax {
	uint maxspecial = 0;
};

struct Settings {
	std::string filename;			// input file
	std::string regex_str;			// select passwords you want
	bool withcount = false;			// if the file is formated [occurrences] [password]

	uint limitSimplemask = 12;		//minimal occurence fto be displayed
	uint limitAdvancedmask = 12;
	int hiderare = 0; 				// Hide low statistics
	int top = 10;					// Show only a top of statistics
	uint nbThread = 1;				// Number of usable threads, default 1
	std::string outfile_name;		// File where to write masks
	bool debug_enabled = false;		// Enable debug output
	SecurityRules sr = { 8, 0, 1, 1, 1 }; // Statistics on passwords matching these rules
};

struct Statistics {
	Statistics operator+(const Statistics& other) const;
	void operator+=(const Statistics& other);
	bool operator==(const Statistics& other) const;
	int thread_id;
	std::string filename;

	uint64_t total_counter = 0;
	uint64_t total_filter = 0;
	uint64_t nb_secure_passwords = 0;

	IntOccurrence length;
	StringOccurrence simplemasks;
@@ -49,14 +63,6 @@ struct Statistics {
	StringOccurrence charactersets;

	minMax minMaxValue;

	std::string regex_str;
	bool withcount = false;

	uint limitSimplemask;
	uint limitAdvancedmask;

	SecurityRules sr;
};

#pragma omp declare reduction(dataSum: Statistics : omp_out += omp_in ) initializer(omp_priv(omp_orig))
+12 −26
Original line number Diff line number Diff line
@@ -41,19 +41,18 @@ public:
	int generate_stats();
	void print_stats() const;

	inline void setHiderare(const int& hr) { hiderare = hr; }
	inline void setTop(const int& t) { top = t; }
	inline void setRegex(const std::string& reg) { regex_str = reg;	}
	inline void setWithcount(const bool& wc) { withcount = wc; }
	inline void setLimitSimplemask(const int& limit) { limitSimplemask = limit; }
	inline void setLimitAdvancedmask(const int& limit) { limitAdvancedmask = limit; }
	inline void setNbThread(const int& nb) { nbThread = nb; }
	void configureThread(Statistics& td) const;
	inline void setHiderare(const int& hr) { settings.hiderare = hr; }
	inline void setTop(const int& t) { settings.top = t; }
	inline void setRegex(const std::string& reg) { settings.regex_str = reg;	}
	inline void setWithcount(const bool& wc) { settings.withcount = wc; }
	inline void setLimitSimplemask(const int& limit) { settings.limitSimplemask = limit; }
	inline void setLimitAdvancedmask(const int& limit) { settings.limitAdvancedmask = limit; }
	inline void setNbThread(const int& nb) { settings.nbThread = nb; }
	void setSecurityRules(const uint& length, const uint& special, const uint& digit, const uint& upper, const uint& lower);
	inline void setOutfile(const std::string& outfile) { outfile_name = outfile; }
	inline void enableDebug() { debug_enabled = true; }
	inline void setOutfile(const std::string& outfile) { settings.outfile_name = outfile; }
	inline void enableDebug() { settings.debug_enabled = true; }

	inline int getNbThreads() const { return nbThread; }
	inline int getNbThreads() const { return settings.nbThread; }
	inline uint64_t getNbLines() const { return nblines; }
	inline uint64_t getProcessed() const { return processed; }
	inline const Statistics& getResults() const { return results; }
@@ -62,23 +61,10 @@ public:
	bool operator==(const Statsgen& other) const;

private:
	std::string filename;
	// results of the computation
	Statistics results;

	// Filters
	int hiderare = 0; 				// Hide low statistics
	int top = 10;					// Show only a top of statistics
	std::string regex_str;		// Regex for the interesting passwords
	bool withcount = false;			// Know if the database is at the format withcount or not
	int limitSimplemask = 12;		// Limit the size of Simple Mask
	int limitAdvancedmask = 12;		// Limit the size of Advanced Mask
	uint nbThread = 1;				// Number of usable threads, default 1
	std::string outfile_name;		// File where to write masks
	bool debug_enabled = false;		// Enable debug output

	// Security policy
	SecurityRules _sr = { 0, 8, 0, 1, 1, 1	};
	// stores the settings of the computation
	Settings settings;

	// nb lines processed
	uint64_t nblines = 0;
+2 −2
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ Statistics Statistics::operator+(const Statistics& other) const {
	td.total_counter = total_counter + other.total_counter;
	td.total_filter = total_filter + other.total_filter;

	td.sr.nbSecurePassword += other.sr.nbSecurePassword;
	td.nb_secure_passwords += other.nb_secure_passwords;

	td.minMaxValue.updateMinMax(other.minMaxValue);

@@ -61,7 +61,7 @@ Statistics Statistics::operator+(const Statistics& other) const {
bool Statistics::operator==(const Statistics& o) const {
	return total_counter == o.total_counter
	&& total_filter == o.total_filter
	&& sr.nbSecurePassword == o.sr.nbSecurePassword
	&& nb_secure_passwords == o.nb_secure_passwords
	&& minMaxValue == o.minMaxValue
	&& length == o.length
	&& charactersets == o.charactersets
+35 −43
Original line number Diff line number Diff line
@@ -22,29 +22,22 @@

using namespace std;

Statsgen::Statsgen(const std::string& name):filename(name){}

void Statsgen::setSecurityRules(const uint& length, const uint& special, const uint& digit, const uint& upper, const uint& lower) {
	_sr = { _sr.nbSecurePassword, length, special, digit, upper, lower };
Statsgen::Statsgen(const std::string& name){
	settings.filename = name;
}

void Statsgen::configureThread(Statistics& td) const {
	td.filename = filename;
	td.regex_str = regex_str;
	td.withcount = withcount;
	td.limitSimplemask = limitSimplemask;
	td.limitAdvancedmask = limitAdvancedmask;
	td.sr = { 0, _sr.minLength, _sr.minSpecial, _sr.minDigit, _sr.minLower, _sr.minUpper };
void Statsgen::setSecurityRules(const uint& length, const uint& special, const uint& digit, const uint& upper, const uint& lower) {
	settings.sr = { length, special, digit, upper, lower };
}

int Statsgen::generate_stats() {
	int nbthreads;
	processed = 0;
	finished = false;
	results = Statistics();
	configureThread(results);
	omp_set_num_threads(nbThread);
	nblines = nbline_file(filename);
	if (!nblines){ // error reading the file
	omp_set_num_threads(settings.nbThread);
	nblines = nbline_file(settings.filename);
	if (!nblines){
		cerr << "[ERROR] Empty file or not existing file" << endl;
		return 0;
	}
@@ -54,16 +47,16 @@ int Statsgen::generate_stats() {
	{
#pragma omp single
		{
			nbThread = omp_get_num_threads();
			nbthreads = omp_get_num_threads();
		}
		uint thread_id = omp_get_thread_num();
		ifstream inputfile(filename);
		ifstream inputfile(settings.filename);
		string line;
		int nbpasswords = 1;
		istringstream iss;
		for(uint numline = 0; numline < nblines; ++numline){
			getline(inputfile, line);
			if(withcount){
			if(settings.withcount){
				iss = istringstream(line);
				iss >> nbpasswords;
				iss >> line;
@@ -71,7 +64,7 @@ int Statsgen::generate_stats() {
			else {
				nbpasswords = 1;
			}
			if((numline % nbThread) == thread_id){
			if((numline % nbthreads) == thread_id){
				handle_password(line, nbpasswords, results);
#pragma omp atomic
				++processed;
@@ -98,14 +91,14 @@ void Statsgen::print_stats() const {


	cout << "\nSecurity rules : " << endl;
	cout << "\tMinimal length of a password: " << results.sr.minLength << endl;
	cout << "\tMinimum of special characters in a password: " << results.sr.minSpecial << endl;
	cout << "\tMinimum of digits in a password: " << results.sr.minDigit << endl;
	cout << "\tMinimum of lower characters in a password: " << results.sr.minLower << endl;
	cout << "\tMinimum of upper characters in a password: " << results.sr.minUpper << endl;
	cout << "\tMinimal length of a password: " << settings.sr.minLength << endl;
	cout << "\tMinimum of special characters in a password: " << settings.sr.minSpecial << endl;
	cout << "\tMinimum of digits in a password: " << settings.sr.minDigit << endl;
	cout << "\tMinimum of lower characters in a password: " << settings.sr.minLower << endl;
	cout << "\tMinimum of upper characters in a password: " << settings.sr.minUpper << endl;

	float perce = percentage(results.sr.nbSecurePassword, results.total_counter);
	cout << "\n\t\t--> " << results.sr.nbSecurePassword << " passwords\t(" << perce << " %) respect the security rules\n" << endl;
	float perce = percentage(results.nb_secure_passwords, results.total_counter);
	cout << "\n\t\t--> " << results.nb_secure_passwords << " passwords\t(" << perce << " %) respect the security rules\n" << endl;


	cout << "\nmin - max\n" << endl;
@@ -121,30 +114,30 @@ void Statsgen::print_stats() const {


	cout << "\nStatistics relative to length: \n" << endl;
	showMap(results.length, top, results.total_counter, hiderare, count);
	showMap(results.length, settings.top, results.total_counter, settings.hiderare, count);

	cout << "\nStatistics relative to charsets: \n" << endl;
	showMap(results.charactersets, -1, results.total_counter, hiderare, count);
	showMap(results.charactersets, -1, results.total_counter, settings.hiderare, count);


	cout << "\nStatistics relative to simplemasks: \n" << endl;
	showMap(results.simplemasks, top, results.total_counter, hiderare, count);
	showMap(results.simplemasks, settings.top, results.total_counter, settings.hiderare, count);

	if (limitSimplemask > 0) {
	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, hiderare);
			readResult(r->second, r->first, count, results.total_counter, settings.hiderare);
		}
	}


	cout << "\nStatistics relative to advancedmask: \n" << endl;
	showMap(results.advancedmasks, top, results.total_counter, hiderare, count);
	showMap(results.advancedmasks, settings.top, results.total_counter, settings.hiderare, count);

	if (! outfile_name.empty()){
	if (! settings.outfile_name.empty()){
		locale::global(locale("C"));
		ofstream outfile_stream(outfile_name);
		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;
@@ -153,11 +146,11 @@ void Statsgen::print_stats() const {
		outfile_stream.close();
	}

	if (limitAdvancedmask > 0) {
	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, hiderare);
			readResult(r->second, r->first, count, results.total_counter, settings.hiderare);
		}
	}
}
@@ -213,21 +206,21 @@ pair<uint, uint> Statsgen::get_masks(const string& password, PasswordStats& c) c

void Statsgen::handle_password(const string& password, const uint64_t& nbPasswords, Statistics& stats) const {
	stats.total_counter += nbPasswords;
	if(stats.regex_str.size() && !regex_match(password, std::regex(stats.regex_str))){
	if(settings.regex_str.size() && !regex_match(password, std::regex(settings.regex_str))){
		return;
	}
	PasswordStats c;

	pair<uint, uint> masks = get_masks(password, c);

	if(c.pol.satisfies(stats.sr, password.size())){
		stats.sr.nbSecurePassword++;
	if(c.pol.satisfies(settings.sr, password.size())){
		stats.nb_secure_passwords++;
	}

	if (masks.first > stats.limitSimplemask) {
	if (masks.first > settings.limitSimplemask) {
		c.simplemask_string = "othermasks";
	}
	if (masks.second > stats.limitAdvancedmask) {
	if (masks.second > settings.limitAdvancedmask) {
		c.advancedmask_string = "othermasks";
	}

@@ -244,8 +237,7 @@ bool Statsgen::operator==(const Statsgen& o) const {
}

bool SecurityRules::operator==(const SecurityRules& o) const {
	return nbSecurePassword == o.nbSecurePassword
	&& minLength == o.minLength
	return minLength == o.minLength
	&& minSpecial == o.minSpecial
	&& minDigit == o.minDigit
	&& minLower == o.minLower
Loading