From e06f39baaed5a9f7563dce4c58b95f564573aa07 Mon Sep 17 00:00:00 2001 From: "M. DUVAL" Date: Wed, 22 Mar 2017 21:50:11 +0100 Subject: [PATCH] Python version 5 --- Main.py | 88 ++++++++++++++++++++++++++++ testPass.py => ProcessBruteForce.py | 90 ++--------------------------- 2 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 Main.py rename testPass.py => ProcessBruteForce.py (52%) diff --git a/Main.py b/Main.py new file mode 100644 index 0000000..3a0c9a2 --- /dev/null +++ b/Main.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +import multiprocessing +import sys +import time +import locale +from ProcessBruteForce import * + +nbProcess = 4 +verbose = False + +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 , "azerty","",6,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) + if d != 0: + print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d:%02d" % (d,h, m, s)) + elif h != 0 : + print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d" % (h, m, s)) + else: + print("Found ! : " + passTrouve +" en " + "%d:%02d" % (m, s)) + else: + print("Found ! : " + passTrouve +" en " + str(time.time() - self.appTime) + " secondes") + +#si le fichier est lancé seul +if __name__ == '__main__' : + application = App() + application.run() + sys.exit() \ No newline at end of file diff --git a/testPass.py b/ProcessBruteForce.py similarity index 52% rename from testPass.py rename to ProcessBruteForce.py index 8227204..f1e5808 100644 --- a/testPass.py +++ b/ProcessBruteForce.py @@ -1,83 +1,11 @@ +#!/usr/bin/python3 # -*- coding: utf-8 -*- + import multiprocessing import sys import time -import locale tabChar = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ") -nbProcess = 4 -verbose = False - -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) - if d != 0: - print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d:%02d" % (d,h, m, s)) - elif h != 0 : - print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d" % (h, m, s)) - else: - print("Found ! : " + passTrouve +" en " + "%d:%02d" % (m, s)) - else: - print("Found ! : " + passTrouve +" en " + str(time.time() - self.appTime) + " secondes") class ProcessBrurteForce(multiprocessing.Process): def __init__(self,queue, idx, nbInstence,password, clef, limite, verbose): @@ -113,17 +41,13 @@ class ProcessBrurteForce(multiprocessing.Process): #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)) + if( self.idx == self.nbInstence - 1 ): + indexMax = len(tabChar) for i in range(indexMin,indexMax): self.tabPass[len(self.tabPass)-1] = tabChar[i] found,passTrouve =self.recursiveFunction(len(self.tabPass)-2) @@ -168,7 +92,5 @@ class ProcessBrurteForce(multiprocessing.Process): #si le fichier est lancé seul -if __name__ == '__main__' : - application = App() - application.run() +if __name__ == '__main__' : sys.exit() \ No newline at end of file