From 732477d0160db0909d72e49a27223286460ed6c4 Mon Sep 17 00:00:00 2001 From: "M. DUVAL" Date: Wed, 22 Mar 2017 21:48:48 +0100 Subject: [PATCH] Python version 4 --- testPass.py | 267 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 152 insertions(+), 115 deletions(-) diff --git a/testPass.py b/testPass.py index 6b46e04..8227204 100644 --- a/testPass.py +++ b/testPass.py @@ -1,22 +1,72 @@ -# -*- coding: utf-8 -*- -import time +# -*- coding: utf-8 -*- +import multiprocessing import sys +import time import locale -tempsN_1 = time.time(); -globalTime = time.time() -nbMot = 0 -passwordCrypte = "" tabChar = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ") +nbProcess = 4 verbose = False -def main(): - gestionParams() - tabPass =list("a") - global passwordCrypte - found, passTrouve = testPass(True,0,tabPass,passwordCrypte) - if found == True : - if(time.time() - globalTime) > 60 : - seconds = time.time() - globalTime + +class App(): + def __init__(self): + + ## La liste contenant nos object Processe + self.processes = list() + self.queue = multiprocessing.Queue() + self.infoQueue = multiprocessing.Queue() + self.found = False + self.appTime = time.time() + global nbProcess + global verbose + + for i in range(0,nbProcess): + p=ProcessBrurteForce(self.queue, i,nbProcess , "totor","",4,verbose) + self.processes.append(p) + + def run(self): + for proc in self.processes: + proc.start() + + procAlive=True + totalMotParSecondes =0 + while procAlive: + totalMotParSecondes =0 + procAlive =False + for proc in self.processes: + #proc.join() + resultat = self.queue.get() + if resultat[0]== 0 : + if resultat[1] == True : + #print("RESULT: %s" % resultat[2]) + self.displayResult(resultat[2]) + for proc in self.processes: + proc.terminate() + sys.exit() + elif resultat[0] == 1: + WordsSeconds = round(resultat[1]/resultat[2]) + totalMotParSecondes = totalMotParSecondes + WordsSeconds + locale.setlocale(locale.LC_ALL, 'french_france') + WordsSeconds = locale.format('%d', WordsSeconds, grouping=True) + print(resultat[4]+" : " + '{:>30}'.format(WordsSeconds) + " Mots/s \tMot actuel : " + resultat[3]) + + else: + print("/!\ useCase" + resultat[0] +"non traité") + + + #on verifie qu'il y a encore des process en cours (il n'y ap aps de do while en python + if proc.is_alive(): + procAlive =True + if verbose: + locale.setlocale(locale.LC_ALL, 'french_france') + totalMotParSecondes = locale.format('%d', totalMotParSecondes, grouping=True) + print("Mots Par Secondes : " +'{:>30}'.format(totalMotParSecondes)+ " Mots/s") + + return None + + def displayResult(self,passTrouve): + if(time.time() - self.appTime) > 60 : + seconds = time.time() - self.appTime m, s = divmod(seconds, 60) h, m = divmod(m, 60) d ,h = divmod(h, 24) @@ -27,111 +77,98 @@ def main(): else: print("Found ! : " + passTrouve +" en " + "%d:%02d" % (m, s)) else: - print("Found ! : " + passTrouve +" en " + str(time.time() - globalTime) + " secondes") - sys.exit() -def gestionParams(): - passSet = False - global passwordCrypte - global verbose - if len(sys.argv) > 1: - for argv in sys.argv: - if "--help" == argv or "-h" == argv: - helpMessage() - elif "--pass" in argv or "-p" in argv: - tabParam = "".join(str.split(argv,"=")[0]) - passwordCrypte = "".join(str.split(argv,tabParam + "=")) - for char in passwordCrypte: - if char not in tabChar: - print("Error : Unavaliable caractere \t" + char) - print() - helpMessage() - print("Searched password : \t"+ passwordCrypte) - passSet = True - elif "--verbose" == argv or "-v" == argv: - print("/!\ Verbose : Worst performance") - verbose = True - elif argv == sys.argv[0]: - print() - else: - print("Error : Unknown command \t" + argv) - print() - helpMessage() - else: - print("Error : You need to set a password to Search") - print() - helpMessage() + print("Found ! : " + passTrouve +" en " + str(time.time() - self.appTime) + " secondes") - if passSet == False: - print("Error : You need to set a password to Search") - print() - helpMessage() - -def helpMessage(): - print("Usage : " ) - print("\t--help, -h\t Print this message") - print("\t--pass=[passwordSearch], -p=[passwordSearch]") - print("\t\tSelect pasword to Search") - print("\t--verbose, -v\t More output") - print() - sys.exit() - -def testSpeed(tabPass): - global nbMot - global tempsN_1 - if (time.time() - tempsN_1) > 5 : - WordsSeconds = round(nbMot/(time.time() - tempsN_1)) - locale.setlocale(locale.LC_ALL, 'french_france') - WordsSeconds = locale.format('%d', WordsSeconds, grouping=True) - str_number = str(ord('a')) - print('{:>30}'.format(WordsSeconds) + " Mots/s \tMot actuel : " + "".join(tabPass)) - nbMot = 0 - tempsN_1 = time.time() - -def testPass(isFirst, pos, tabPass, password ): - found = False - passTrouve = "" - global nbMot - global tempsN_1 - global verbose - #Première boucle, permet de parcourir l'enssembles des caractères du tableau - for char in tabChar: - tabPass[pos] = char - if verbose == True: - nbMot = nbMot +1; - testSpeed(tabPass) +class ProcessBrurteForce(multiprocessing.Process): + def __init__(self,queue, idx, nbInstence,password, clef, limite, verbose): + super().__init__() + self.queue = queue + self.idx = idx + self.nbInstence = nbInstence + + #la queue contien 3 valeur :int useCase = 0, boolean found, list passTrouve + #la queue contien 5 valeur :int useCase = 1, nbMots, time secondes , list motEnCours nom du thread + #useCase = 0, on remonte les information contenant le mot Trouvé ou non + #useCase = 1, on remonte des information sur les performances + #gestion du temps + self.nbMot = 0 + self.procTime = time.time() + self.verbose = verbose + + self.password = password + self.clef = clef + self.limite = limite + self.tabPass = list("a") + self.isFound = False + return + + def testSpeed(self): + self.nbMot = self.nbMot+1 + if (time.time() - self.procTime) > 5 : + self.queue.put([1,self.nbMot,time.time() - self.procTime, "".join(self.tabPass),multiprocessing.current_process().name]) + self.nbMot = 0 + self.procTime = time.time() - if "".join(tabPass) == password: - found=True - passTrouve = "".join(tabPass) - return found, passTrouve - #else: - #print("".join(tabPass) + "\t | " + str(len(tabPass))) + def run(self): + #print(multiprocessing.current_process().name + "\tStart") + found , passTrouve = self.checkChar(0) + if not found: + while not found and len(self.tabPass) < self.limite: + self.tabPass = self.tabPass + list('a') + found,passTrouve =self.recursiveFunction(len(self.tabPass)-1) + if found == True : + passTrouve = "".join(self.tabPass) + + if not found : + indexMin = round((len(tabChar)/self.nbInstence)*self.idx) + indexMax = round(indexMin + (len(tabChar)/self.nbInstence)) + self.tabPass = self.tabPass + list(tabChar[indexMin]) + #print(multiprocessing.current_process().name + "\tRange : " + str(indexMin) + " - " + str(indexMax)) + for i in range(indexMin,indexMax): + self.tabPass[len(self.tabPass)-1] = tabChar[i] + found,passTrouve =self.recursiveFunction(len(self.tabPass)-2) + if found == True: + #la queue contien 2 valeur : boolean found, list passTrouve + #la queue contien 4 valeur : nbMots, time secondes , list motEnCours nom du thread + self.queue.put([0,found, passTrouve]) + return None - # Seconde boucle, ne s'execute que si on est dans la première instance de la fonction, - # elle permet de rajouter les caractère au fur et à mesure + print(multiprocessing.current_process().name + "\tStop") + #la queue contien 2 valeur : boolean found, list passTrouve + #la queue contien 4 valeur : nbMots, time secondes , list motEnCours nom du thread + self.queue.put([0,found, passTrouve]) + return None - if isFirst == True and found == False: - while found != True: - tabPass = tabPass + list('a') - found,passTrouve =testPass(False,len(tabPass)-1 , tabPass,password) - if found == True : - found=True - passTrouve = "".join(tabPass) - return found, passTrouve - - # Troisième boucle, elle permet d'itéré el caractère précédent dans la chainde de caractère - if pos > 0 and found == False: - for char in tabChar : - tabPass[pos] = char - found, passTrouve =testPass(False,pos-1, tabPass,password) - if found == True : + def recursiveFunction(self,index): + found , passTrouve = self.checkChar(index) + if index > 0 and not found: + for char in tabChar : + self.tabPass[index] = char + found, passTrouve =self.recursiveFunction(index-1) + if found == True : + passTrouve = "".join(self.tabPass) + return found, passTrouve + + return found, passTrouve + + #Cette fonction permet de replacer le dernier caractère de la Chaine + def checkChar(self,index): + found = False + passTrouve = "" + for char in tabChar: + if self.verbose: + self.testSpeed() + self.tabPass[index] = char + if "".join(self.tabPass) == self.password: found=True - passTrouve = "".join(tabPass) + passTrouve = "".join(self.tabPass) return found, passTrouve - passTrouve = "".join(tabPass) - return found , passTrouve - + #print("".join(self.tabPass)) + return found, passTrouve - -if __name__ == '__main__' : - main() + +#si le fichier est lancé seul +if __name__ == '__main__' : + application = App() + application.run() + sys.exit() \ No newline at end of file