From 90f99960fe6f4f268cb7b4aebcbc2cbe2184cf00 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 24 Mar 2021 23:24:46 +0100 Subject: [PATCH] Transformation en fonction --- jour04/run.py | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/jour04/run.py b/jour04/run.py index 9a3d411..4ae6b31 100644 --- a/jour04/run.py +++ b/jour04/run.py @@ -1,29 +1,26 @@ import hashlib +def calculMd5(mot, nbZeros): + """ + Calcule la partie manquante de mot ayant un hash md5 commencant + par un nombre donné de 0. + + :param mot string: le début du mot + :param nbZeros int: le nombre de zéro du hash md5 + """ + i = 1 + find = False + md = None + while not find: + md = hashlib.md5((mot + str(i)).encode("utf-8")) + if md.hexdigest()[0:nbZeros] == "0" * nbZeros: + find = True; + else: + i += 1 + return i + myInput = "iwrupvqb" -i = 1 -find = False -md = None - print("Traitement de la partie 1…") - -while not find: - md = hashlib.md5((myInput + str(i)).encode("utf-8")) - if md.hexdigest()[0:5] == "00000": - find = True; - else: - i += 1 - -print(i) - +print(calculMd5(myInput, 5)) print("Traitement de la partie 2…") - -find = False -while not find: - md = hashlib.md5((myInput + str(i)).encode("utf-8")) - if md.hexdigest()[0:6] == "000000": - find = True; - else: - i += 1 - -print(i) +print(calculMd5(myInput, 6))