Commit 61382ee2 authored by Jorand Jean-baptiste's avatar Jorand Jean-baptiste
Browse files

filter regex

parent 63e21270
Loading
Loading
Loading
Loading
+8 −1
Changes for Adaptation/include/statsgen.h: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@
#include <string>
#include <map>
#include <iostream>
#include <regex>


struct Policy{
	int digit;
@@ -20,6 +22,7 @@ public:
	void showHelp();
	void setHiderare(int);
	void setTop(int);
	void setRegex(std::string);

	void analyze_password(const std::wstring&, int &, std::wstring &, std::wstring &, std::wstring &,Policy &);
	int generate_stats(const std::string &);
@@ -32,12 +35,15 @@ private:
	void analyse_charset(std::wstring &, const Policy &);

	void updateMinMax(const Policy &);

	void readResult(int, std::wstring, int &);
	void readResult(int, int, int &);

	// Filters

	int hiderare = 0;
	int top = -1;
	std::wregex current_regex;
	bool use_regex = false;


	// dico
@@ -49,6 +55,7 @@ private:


	int total_counter = 0;
	int total_filter = 0;


	// Minimum password complexity counters
+13 −1
Changes for Adaptation/src/main.cpp: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -31,6 +31,15 @@ int main(int argc,char* argv[]) {
				i++;
			}

			else if (string(argv[i]) == "--regex") {
				if (argc == i+1) {
					wcerr << "Missing argument --top [value]" << endl;
					return -1;
				}
				test.setRegex(argv[i+1]);
				i++;
			}

			else if (string(argv[i]) == "--help" || string(argv[i]) == "-h") {
				test.showHelp();
				return 0;
@@ -43,12 +52,15 @@ int main(int argc,char* argv[]) {
	}



	if (filename == "") {
		cerr << "Missing filename" << endl;
		wcerr << "Missing filename" << endl;
		return -1;
	}




	if (test.generate_stats(filename)) {
		test.print_stats();
	}
+112 −54
Changes for Adaptation/src/statsgen.cpp: 112 added lines, 54 removed lines.
Original line number Diff line number Diff line
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <regex>
#include "statsgen.h"
using namespace std;



/**********************************************************************
 *                           INITIALISATION                           *
 **********************************************************************/


void Statsgen::showHelp() {
    wcout << "\nUsage database.txt [options]\n" << endl;

@@ -30,6 +36,26 @@ void Statsgen::setTop(int val) {
}


void Statsgen::setRegex(string expr) {
    wstring tmp(expr.length(),L' ');
    copy(expr.begin(), expr.end(), tmp.begin());

    wregex tmp_reg(tmp);
    current_regex = tmp_reg;

    use_regex = true;
}






/**********************************************************************
 *                          USEFUL FUNCTIONS                          *
 **********************************************************************/



multimap<int, int> flip_map(map<int,int> & src) {
    multimap<int,int> dst;
@@ -226,6 +252,10 @@ int Statsgen::generate_stats(const string & filename) {

        total_counter++;

        
        if ( (use_regex && regex_match(line, current_regex)) || !use_regex )  {
            total_filter++;

            analyze_password(line, pass_length, characterset, simplemask_string, advancedmask_string, pol);

            updateMinMax(pol);
@@ -234,6 +264,7 @@ int Statsgen::generate_stats(const string & filename) {
            stats_simplemasks[simplemask_string] += 1;
            stats_advancedmasks[advancedmask_string] += 1;
        }
    }


    if (!total_counter) {
@@ -248,11 +279,56 @@ int Statsgen::generate_stats(const string & filename) {



void Statsgen::print_stats() {
    wcout << "Analyzing " << total_counter << " passwords" << endl;


/**********************************************************************
 *                         PRINT STATISTIQUE                          *
 **********************************************************************/


void Statsgen::readResult(int res, int length, int & count) {
    float percentage;

    percentage = (float) (100*res) / total_counter;

    if (percentage >= hiderare) {
        wstring value = to_wstring(percentage);
        value = value.substr(0,5);

        wcout << setw(40) << right << length << ":  " 
            << setw(5) << right << value << "%" 
            << setw(5) << right << "(" << res << ")" << endl;

        count++;
    }
}

void Statsgen::readResult(int res, wstring carac, int & count) {
    float percentage;
    percentage = (float) (100*res) / total_counter;

    if (percentage >= hiderare) {
        wstring value = to_wstring(percentage);
        value = value.substr(0,5);

        wcout << setw(40) << right << carac << ":  " 
            << setw(5) << right << value << "%" 
            << setw(5) << right << "(" << res << ")" << endl;

        count++;
    }
}



void Statsgen::print_stats() {
    int count;
    float perc = (float) (100*total_filter) / total_counter;

    wcout << "\n\tSelected " << total_filter << " on " << total_counter << " passwords\t(" 
        << perc << " %)" << endl;



    wcout << "\nmin - max\n" << endl;
    wcout << setw(43) << right << "digit:  " 
@@ -270,24 +346,21 @@ void Statsgen::print_stats() {

    count = 0;
    multimap<int, int> reverseLength = flip_map(stats_length);
    map<int, int>::const_iterator it1;
    for(it1 = reverseLength.end(); it1 != reverseLength.begin(); it1--) {
        if (it1 == reverseLength.end()) continue;
        
    for(map<int, int>::const_iterator it = reverseLength.end(); it != reverseLength.begin(); it--) {
        if (it == reverseLength.end()) continue;
        readResult(it1->first, it1->second, count);
        if (top != -1 && count == top) break;
    }

    if (count != top) {
        readResult(it1->first, it1->second, count);
    }
    
        percentage = (float) (100*it->first) / total_counter;

        if (percentage >= hiderare) {
            wstring value = to_wstring(percentage);
            value = value.substr(0,5);

            wcout << setw(40) << right << it->second << ":  " 
                << setw(5) << right << value << "%" 
                << setw(5) << right << "(" << it->first << ")" << endl;

            count++;
            if (top != -1 && count == top) break;
        }
    }

    

@@ -295,73 +368,58 @@ void Statsgen::print_stats() {
    
    count = 0;
    multimap<int, wstring> reverseCharsets = flip_map(stats_charactersets);
    map<int, wstring>::const_iterator it2;

    for(map<int, wstring>::const_iterator it = reverseCharsets.end(); it != reverseCharsets.begin(); it--) {
        if (it == reverseCharsets.end()) continue;
    for(it2 = reverseCharsets.end(); it2 != reverseCharsets.begin(); it2--) {
        if (it2 == reverseCharsets.end()) continue;

        percentage = (float) (100*it->first) / total_counter;
        
        if (percentage >= hiderare) {
            wstring value = to_wstring(percentage);
            value = value.substr(0,5);

            wcout << setw(40) << right << it->second << ":  " 
                << setw(5) << right << value << "%" 
                << setw(5) << right << "(" << it->first << ")" << endl;

            count++;
        readResult(it2->first, it2->second, count);
        if (top != -1 && count == top) break;
    }

    if (count != top) {
        readResult(it2->first, it2->second, count);
    }
    



    wcout << "\nStatistics relative to simplemasks: \n" << endl;

    count = 0;
    multimap<int, wstring> reverseSimpleMasks = flip_map(stats_simplemasks);
    map<int, wstring>::const_iterator it3;

    for(map<int, wstring>::const_iterator it = reverseSimpleMasks.end(); it != reverseSimpleMasks.begin(); it--) {
        if (it == reverseSimpleMasks.end()) continue;
        
        percentage = (float) (100*it->first) / total_counter;

        if (percentage >= hiderare) {
            wstring value = to_wstring(percentage);
            value = value.substr(0,5);
    for(it3 = reverseSimpleMasks.end(); it3 != reverseSimpleMasks.begin(); it3--) {
        if (it3 == reverseSimpleMasks.end()) continue;
        
            wcout << setw(40) << right << it->second << ":  " 
                << setw(5) << right << value << "%" 
                << setw(5) << right << "(" << it->first << ")" << endl;

            count++;
        readResult(it3->first, it3->second, count);
        if (top != -1 && count == top) break;
    }

    if (count != top) {
        readResult(it3->first, it3->second, count);
    }




    wcout << "\nStatistics relative to advancedmask: \n" << endl;

    count = 0;
    multimap<int, wstring> reverseAdvancedMask = flip_map(stats_advancedmasks);
    map<int, wstring>::const_iterator it4;

    for(map<int, wstring>::const_iterator it = reverseAdvancedMask.end(); it != reverseAdvancedMask.begin(); it--) {
        if (it == reverseAdvancedMask.end()) continue;
        
        percentage = (float) (100*it->first) / total_counter;
        
        if (percentage >= hiderare) {
            wstring value = to_wstring(percentage);
            value = value.substr(0,5);

            wcout << setw(40) << right << it->second << ":  " 
                << setw(5) << right << value << "%" 
                << setw(5) << right << "(" << it->first << ")" << endl;
    for(it4 = reverseAdvancedMask.end(); it4 != reverseAdvancedMask.begin(); it4--) {
        if (it4 == reverseAdvancedMask.end()) continue;
        
            count++;
        readResult(it4->first, it4->second, count);
        if (top != -1 && count == top) break;
    }

    if (count != top) {
        readResult(it4->first, it4->second, count);
    }

}
−8

File deleted.