import tkinter as tk
import os
import string

# ----------------------------
# Dictionnaires à charger
# ----------------------------

dictionary_files = [
    "10k-most-common-passwords.txt",
    "500-worst-passwords.txt",
    "darkweb2017_top-10000.txt",
    "french_passwords_top20000.txt",
    "hotmail.txt",
    "milw0rm-dictionary.txt",
    "probable-v2_top-12000.txt",
    "rockyou.txt"
]

dictionaries = {}

# ----------------------------
# Chargement dictionnaires
# ----------------------------

def load_dictionaries():

    for filename in dictionary_files:

        if os.path.exists("MotDePasse\\"+filename):

            words = set()

            try:
                with open("MotDePasse\\"+filename, "r", encoding="utf8", errors="ignore") as f:

                    for line in f:
                        w = line.strip().lower()
                        if w != "":
                            words.add(w)

                dictionaries[filename] = words

            except:
                dictionaries[filename] = None

        else:
            dictionaries[filename] = None


# ----------------------------
# Détection types caractères
# ----------------------------

def detect_charset(password):

    size = 0

    if any(c.islower() for c in password):
        size += 26

    if any(c.isupper() for c in password):
        size += 26

    if any(c.isdigit() for c in password):
        size += 10

    if any(c in string.punctuation for c in password):
        size += 32

    if size == 0:
        size = 1

    return size


# ----------------------------
# Calcul brute force
# ----------------------------

def bruteforce_time(password):

    length = len(password)

    charset = detect_charset(password)

    guesses_per_second = 60000

    combinations = charset ** length

    seconds = combinations / 2 / guesses_per_second

    return format_time(seconds), seconds


# ----------------------------
# Format durée
# ----------------------------

def format_time(seconds):

    if seconds < 1:
        return "Instantané"

    if seconds < 60:
        return str(int(seconds)) + " secondes"

    if seconds < 3600:
        return str(int(seconds/60)) + " minutes"

    if seconds < 86400:
        return str(int(seconds/3600)) + " heures"

    if seconds < 86400*30:
        return str(int(seconds/86400)) + " jours"

    if seconds < 86400*365:
        return str(int(seconds/(86400*30))) + " mois"

    years = seconds/(86400*365)

    return str(int(years)) + " ans"


# ----------------------------
# Couleur robustesse
# ----------------------------

def color_time(seconds):

    if seconds < 60:
        return "red"

    if seconds < 86400:
        return "orange"

    if seconds < 86400*365:
        return "gold"

    return "green"


# ----------------------------
# Test mot de passe
# ----------------------------

def tester():

    password = entry.get()

    if password == "":
        return

    text, seconds = bruteforce_time(password)

    result_labels["BruteForce"].config(text=text, fg=color_time(seconds))

    pwd = password.lower()

    for file in dictionary_files:

        label = result_labels[file]

        words = dictionaries[file]

        if words is None:
            label.config(text="Fichier absent", fg="orange")
            continue

        if pwd in words:
            label.config(text="Trouvé", fg="red")
        else:
            label.config(text="Non trouvé", fg="green")


# ----------------------------
# Interface graphique
# ----------------------------

root = tk.Tk()

root.title("Testeur de mot de passe")

root.geometry("700x450")


title = tk.Label(root, text="Test de robustesse mot de passe",
                 font=("Arial",18,"bold"))
title.pack(pady=10)


entry = tk.Entry(root, show="*", font=("Arial",14), width=30)
entry.pack(pady=10)


btn = tk.Button(root, text="Tester", command=tester,
                font=("Arial",12,"bold"))
btn.pack(pady=10)


frame = tk.Frame(root)
frame.pack(pady=20)

result_labels = {}

rows = ["BruteForce"] + dictionary_files

for r in rows:

    line = tk.Frame(frame)
    line.pack(anchor="w")

    label1 = tk.Label(line, text=r, width=30, anchor="w")
    label1.pack(side="left")

    label2 = tk.Label(line, text="---", width=20, anchor="w")
    label2.pack(side="left")

    result_labels[r] = label2


# ----------------------------
# Chargement dictionnaires
# ----------------------------

load_dictionaries()

root.mainloop()