Commit 6fc27e7a authored by Jorand Jean-baptiste's avatar Jorand Jean-baptiste
Browse files

add commentaires

parent 69f4cad6
Loading
Loading
Loading
Loading
+1
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,5 +3,6 @@ Utils/createNewDB
Adaptation/main
Adaptation/bin
AdaptationParallel/main
AdaptationParallel/Stats
AdaptationParallel/bin
Pack/
 No newline at end of file
−98

File deleted.

+109 −45
Changes for AdaptationParallel/include/statsgen.h: 109 added lines, 45 removed lines.
Original line number Diff line number Diff line
/**
 * @file statsgen.h
 * @brief Statsgen class and all needed structures
 *
 * @author Jean-Baptiste Jorand
 * @author Yannick Bass
 */


#include <string>
#include <unordered_map>
@@ -5,6 +13,10 @@
#include <regex>



/**
 * @brief Simplify number of arguments for functions
 */
struct Policy{
	int digit = 0;
	int lower = 0;
@@ -13,7 +25,10 @@ struct Policy{
};


struct Conteneur{
/**
 * @brief Simplify number of arguments for functions
 */
struct Container {
	int pass_length = 0;
    std::wstring characterset = L"";
    std::wstring advancedmask_string = L"";
@@ -22,7 +37,24 @@ struct Conteneur{
};


/**
 * @brief Simplify number of arguments for functions
 */
struct minMax {
	int mindigit = -1;				// save the number minimum of digit in a password
	int maxdigit = -1;				// save the number maximum of digit in a password
	int minlower = -1;				// save the number minimum of lower in a password
	int maxlower = -1;				// save the number maximum of lower in a password
	int minupper = -1;				// save the number minimum of upper in a password
	int maxupper = -1;				// save the number maximum of upper in a password
	int minspecial = -1;			// save the number minimum of special character in a password
	int maxspecial = -1;			// save the number maximum of special character in a password
};


/**
 * @brief All needed variables to give to each thread
 */
struct thread_data {
	int thread_id;
	std::string filename;
@@ -37,14 +69,7 @@ struct thread_data {
	std::unordered_map<std::wstring, int> advancedmasks;
	std::unordered_map<std::wstring, int> charactersets;

	int mindigit = -1;
	int maxdigit = -1;
	int minlower = -1;
	int maxlower = -1;
	int minupper = -1;
	int maxupper = -1;
	int minspecial = -1;
	int maxspecial = -1;
	minMax minMaxValue;

	std::wregex current_regex;
	bool use_regex = false;
@@ -53,69 +78,108 @@ struct thread_data {



/**
 * @brief Calcul and save of all analysed statistics
 */
class Statsgen {

public:

	Statsgen() {};
	
	/**
	 * @brief show all options for the command
	 */
	void showHelp();

	/**
	 * @brief Modify the attribute "hiderare", defining if 
	 * user wants to see statistics below 1%
	 * @param : new value for "hiderare"
	 */
	void setHiderare(int);

	/**
	 * @brief Modify the attribute "top", defining the number
	 * of best results the user wants to see
	 * @param : new value for "top"
	 */
	void setTop(int);

	/**
	 * @brief Modify the attribute "regex", defining the regular 
	 * expression of the interesting passwords
	 * @param : new value for "regex"
	 */
	void setRegex(std::string);

	/**
	 * @brief Modify the attribute "withcount", useful to know
	 * if the database uses the format withcount
	 * @param : new value for "withcount"
	 */
	void setWithcount(bool);

	/**
	 * @brief Modify the attribute "limitSimplemask", a filter
	 * for the size of the simple masks
	 * @param : new value for "limitSimplemask"
	 */
	void setLimitSimplemask(int);

	/**
	 * @brief Modify the attribute "limitAdvancedmask", a filter
	 * for the size of the advanced masks
	 * @param : new value for "limitAdvancedmask"
	 */
	void setLimitAdvancedmask(int);

	/**
	 * @brief Modify the attribute "nbThread", defining the number
	 * of threads the user wants to use
	 * @param : new value for "nbThread"
	 */
	void setNbThread(int);


	/**
	 * @brief Calculate all statistics for a database which the
	 * name is given by argument
	 * @param : database's name
	 * @return 0 if error, 1 if success
	 */
	int generate_stats(const std::string &);
	
	/**
	 * @brief Print all calculated statistics
	 */
	void print_stats();

private:
	// Filters

	int hiderare = 0;
	int top = -1;
	std::wregex current_regex;
	bool use_regex = false;
	bool withcount = false;
	int limitSimplemask = 0;
	int limitAdvancedmask = 0;
	int nbThread = 1;

	int hiderare = 0; 				// Hide low statistics
	int top = -1;					// Show only a top of statistics
	std::wregex current_regex;		// Regex for the interesting passwords
	bool use_regex = false;			// Know if we use a regex or not
	bool withcount = false;			// Know if the database is at the format withcount or not
	int limitSimplemask = 0;		// Limit the size of Simple Mask
	int limitAdvancedmask = 0;		// Limit the size of Advanced Mask
	int nbThread = 1;				// Number of usable threads, default 1

	// dico

	std::unordered_map<int, int> stats_length;
	std::unordered_map<std::wstring, int> stats_simplemasks;
	std::unordered_map<std::wstring, int> stats_advancedmasks;
	std::unordered_map<std::wstring, int> stats_charactersets;
	// Dictionary

	std::unordered_map<int, int> stats_length;					// Passwords' length linked to their occurrence
	std::unordered_map<std::wstring, int> stats_simplemasks;	// Passwords' simple mask linked to their occurrence
	std::unordered_map<std::wstring, int> stats_advancedmasks;	// Passwords' advanced mask linked to their occurrence
	std::unordered_map<std::wstring, int> stats_charactersets;	// Passwords' characterset linked to their occurrence

	double total_counter = 0;
	double total_filter = 0;

	// Counters

	// Minimum password complexity counters
	double total_counter = 0;		// number of analysed passwords
	double total_filter = 0;		// number of analysed passwords after a filter

	int mindigit = -1;
	int maxdigit = -1;
	int minlower = -1;
	int maxlower = -1;
	int minupper = -1;
	int maxupper = -1;
	int minspecial = -1;
	int maxspecial = -1;
	minMax minMaxValue;				// save all genral data from passwords 
};



void analyze_password(const std::wstring &, Conteneur &);
void analyse_letter(const char &, char &, std::wstring &, std::wstring &, Policy &, int &, int &);
void analyse_charset(std::wstring &, const Policy &);
void updateMinMax(int &, int &, int &, int &, int &, int &, int &, int &, const Policy &);


void * generate_stats_thread(void * );
 No newline at end of file
+301 −4
Changes for AdaptationParallel/include/utils.h: 301 added lines, 4 removed lines.
Original line number Diff line number Diff line
/**
 * @file utils.h
 * @brief useful functions
 *
 * @author Jean-Baptiste Jorand
 * @author Yannick Bass
 */

#include <map>
#include <iostream>
#include <unordered_map>
@@ -8,10 +16,18 @@ using namespace std;
template<typename K, typename V>
using UnorderedMapIterator = typename unordered_map<K, V>::const_iterator;


template<typename K, typename V>
using MapIterator = typename map<K, V>::const_iterator;



/**
 * @brief Useful functions to order an unoredered map
 * whatever the key's type
 * @param src : unoredered map than needed to be ordered
 * @return ordered map
 */
template<typename A> 
multimap<int, A> flip_map(const std::unordered_map<A, int> & src) {
    multimap<int, A> dst;
@@ -24,7 +40,10 @@ multimap<int, A> flip_map(const std::unordered_map<A, int> & src) {




/**
 * @brief print some results
 * @param 
 */
template<typename Type>
void readResult(int res, Type carac, int & count, const int & total_counter, const int & hiderare) {
    float percentage;
@@ -43,9 +62,14 @@ void readResult(int res, Type carac, int & count, const int & total_counter, con
}





/**
 * @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 unordered_map<Type, int> & stats, const int & top, const int & total_counter, const int & hiderare, int & count) {
	count = 0;
@@ -67,6 +91,279 @@ void showMap(const unordered_map<Type, int> & stats, const int & top, const int



/**
 * @brief analyse a letter in a password
 * @param letter : current letter
 * @param last_simplemask : last analyzed part of the simple masks 
 * @param simplemask_string : current simple mask
 * @param advancedmask_string : current advanced mask
 * @param policy : current number of digit, lower, upper and special for the current password
 * @param sizeAdvancedMask : size of the current advanced mask 
 * @param sizeSimpleMask : size of the current simple mask
 */
void analyse_letter(const char & letter, char & last_simplemask, wstring & simplemask_string, wstring & advancedmask_string, Policy & policy, int & sizeAdvancedMask, int & sizeSimpleMask) {
    sizeAdvancedMask++;

    if (letter >= L'0' && letter <= L'9') {
        policy.digit++;
        advancedmask_string += L"?d";
        if (last_simplemask != 'd') {
            sizeSimpleMask++;
            simplemask_string += L"digit";
            last_simplemask = 'd';
        }
    }
    else if(letter >= L'a' && letter <= L'z') {
        policy.lower++;
        advancedmask_string += L"?l";
        if (last_simplemask != 'l') {
            sizeSimpleMask++;
            simplemask_string += L"lower";
            last_simplemask = 'l';
        }
    }
    else if(letter >= L'A' && letter <= L'Z') {
        policy.upper++;
        advancedmask_string += L"?u";
        if (last_simplemask != 'u') {
            sizeSimpleMask++;
            simplemask_string += L"upper";
            last_simplemask = 'u';
        }
    }
    else {
        policy.special++;
        advancedmask_string += L"?s";

        if (last_simplemask != 's') {
            sizeSimpleMask++;
            simplemask_string += L"special";
            last_simplemask = 's';
        }
    }
}



/**
 * @brief analyse the characterset of the current password
 * @param charset : characterset of the current password
 * @param policy : current number of digit, lower, upper and special for the current password
 */
void analyse_charset(wstring & charset, const Policy & policy) {
    if (policy.digit && !policy.lower && !policy.upper && !policy.special) {
        charset = L"numeric";
    }
    else if (!policy.digit && policy.lower && !policy.upper && !policy.special) {
        charset = L"loweralpha";
    }
    else if (!policy.digit && !policy.lower && policy.upper && !policy.special) {
        charset = L"upperalpha";
    }
    else if (!policy.digit && !policy.lower && !policy.upper && policy.special) {
        charset = L"special";
    }
    else if (!policy.digit && policy.lower && policy.upper && !policy.special) {
        charset = L"mixedalpha";
    }
    else if (policy.digit && policy.lower && !policy.upper && !policy.special) {
        charset = L"loweralphanum";
    }
    else if (policy.digit && !policy.lower && policy.upper && !policy.special) {
        charset = L"upperalphanum";
    }
    else if (!policy.digit && policy.lower && !policy.upper && policy.special) {
        charset = L"loweralphaspecial";
    }
    else if (!policy.digit && !policy.lower && policy.upper && policy.special) {
        charset = L"upperalphaspecial";
    }
    else if (policy.digit && !policy.lower && !policy.upper && policy.special) {
        charset = L"specialnum";
    }

    else if (!policy.digit && policy.lower && policy.upper && policy.special) {
        charset = L"mixedalphaspecial";
    }
    else if (policy.digit && !policy.lower && policy.upper && policy.special) {
        charset = L"upperalphaspecialnum";
    }
    else if (policy.digit && policy.lower && !policy.upper && policy.special) {
        charset = L"loweralphaspecialnum";
    }
    else if (policy.digit && policy.lower && policy.upper && !policy.special) {
        charset = L"mixedalphanum";
    }

    else {
        charset = L"all";
    }
}



/**
 * @brief analyse a password
 * @param password : current password
 * @param c : container of all useful data of the password
 */
void analyze_password(const wstring & password, Container & c) {
    c.pass_length = password.size();

    char last_simplemask = 'a';
    int sizeAdvancedMask = 0;
    int sizeSimpleMask = 0;
    wchar_t letter;

    for (int i=0; i < c.pass_length; i++) {
        letter = password[i];
        analyse_letter(letter, last_simplemask, c.simplemask_string, c.advancedmask_string, c.pol, sizeAdvancedMask, sizeSimpleMask);
    }

    analyse_charset(c.characterset, c.pol);
}




/**
 * @brief update minima and maxima of all general data from analysed passwords 
 * with the analysed passwords
 * @param minMaxValue : save all minima and maxima
 * @param pol : policy of the last analyzed password
 */
void updateMinMax(minMax & minMaxValue, const Policy & pol) {
    if (minMaxValue.mindigit == -1 || minMaxValue.mindigit > pol.digit) {
        minMaxValue.mindigit = pol.digit;
    }
    if (minMaxValue.maxdigit == -1 || minMaxValue.maxdigit < pol.digit) {
        minMaxValue.maxdigit = pol.digit;
    }

    if (minMaxValue.minlower == -1 || minMaxValue.minlower > pol.lower) {
        minMaxValue.minlower = pol.lower;
    }
    if (minMaxValue.maxlower == -1 || minMaxValue.maxlower < pol.lower) {
        minMaxValue.maxlower = pol.lower;
    }

    if (minMaxValue.minupper == -1 || minMaxValue.minupper > pol.upper) {
        minMaxValue.minupper = pol.upper;
    }
    if (minMaxValue.maxupper == -1 || minMaxValue.maxupper < pol.upper) {
        minMaxValue.maxupper = pol.upper;
    }

    if (minMaxValue.minspecial == -1 || minMaxValue.minspecial > pol.special) {
        minMaxValue.minspecial = pol.special;
    }
    if (minMaxValue.maxspecial == -1 || minMaxValue.maxspecial < pol.special) {
        minMaxValue.maxspecial = pol.special;
    }
}





/**
 * @brief action of all threads
 * @param threadarg : all useful argument for the thread
 */
void * generate_stats_thread(void * threadarg) {
    struct thread_data *my_data;
    my_data = (struct thread_data *) threadarg;

    wifstream readfile(my_data->filename);
    wstring line;
    int nbline = 0;

    while(readfile.good()) {
        ++nbline;
        getline(readfile, line);
        if (nbline < my_data->lineBegin) {
            continue;
        }
        if (nbline >= my_data->lineEnd) {
            break;
        }


        if (line.size() == 0) {
            wcout << "Error empty password, line " << my_data->total_counter << endl;
            continue;
        }

        Container c;

        if (my_data->withcount) {
            uint i = 0;
            bool number=false;
            for(i=0; i < line.length(); i++) {
                if(iswdigit(line.at(i))) {
                    number=true;
                }
                else if (!iswdigit(line.at(i)) && number) {
                    break;
                }
            }
            wstring password = line.substr(i+1,line.length());
            int nbPasswords = stoi(line.substr(0,i));
            
            my_data->total_counter += nbPasswords;

            if ( !my_data->use_regex || (my_data->use_regex && regex_match(password,my_data->current_regex)) ) {
                my_data->total_filter += nbPasswords;
                analyze_password(password, c);

                my_data->length[ c.pass_length ] += nbPasswords;
                my_data->charactersets[ c.characterset ] += nbPasswords;
                my_data->simplemasks[ c.simplemask_string ] += nbPasswords;
                my_data->advancedmasks[ c.advancedmask_string ] += nbPasswords;
            }
        }
        else {
            my_data->total_counter++;
            if ( !my_data->use_regex || (my_data->use_regex && regex_match(line,my_data->current_regex)) ) {
                analyze_password(line, c);
                
                my_data->total_filter++;

                my_data->length[ c.pass_length ] += 1;
                my_data->charactersets[ c.characterset ] += 1;
                my_data->simplemasks[ c.simplemask_string ] += 1;
                my_data->advancedmasks[ c.advancedmask_string ] += 1;
            }
        }
         updateMinMax(my_data->minMaxValue, c.pol);
    }

    readfile.close();

    pthread_exit(NULL);
}



/**
 * @brief calculate the number of line in a file
 * @param filename : name of the file
 * @return : number of line
 */
int nbline_file(const string & filename) {
    wifstream readfile(filename);
    wstring line;
    int nb = 0;

    while(readfile.good()) {
        getline(readfile, line);
        ++nb;
    }

    return nb;
}





+4 −4
Changes for AdaptationParallel/makefile: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -6,18 +6,18 @@ BIN = bin/



.PHONY 	 = main clean
.PHONY 	 = Stats clean


all: main
all: Stats


main: $(BIN)main.o $(BIN)statsgen.o
Stats: $(BIN)Stats.o $(BIN)statsgen.o
	$(CXX) $^ $(CXXFLAGS) -o $@



$(BIN)main.o: $(SRC)main.cpp $(INC)statsgen.h
$(BIN)Stats.o: $(SRC)main.cpp $(INC)statsgen.h
	$(CXX) $(CXXFLAGS) $(SRC)main.cpp -c -o $@

$(BIN)statsgen.o: $(SRC)statsgen.cpp $(INC)statsgen.h
Loading