Skip to content

Commit

Permalink
Fixed critical bugs
Browse files Browse the repository at this point in the history
Fixed critical bugs that prevented the app to generate a passphrase.
  • Loading branch information
Dessmondd authored Sep 12, 2023
1 parent 80f77d2 commit 55be756
Showing 1 changed file with 61 additions and 31 deletions.
92 changes: 61 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import random
import tkinter.filedialog
from modules.password_strength_tester import test_password
from tkinter import ttk
from ttkthemes import ThemedStyle


wordlist = [
"apple", "banana", "cherry", "date", "elder", "fig",
Expand All @@ -20,43 +23,49 @@
"peppermint", "spearmint", "mango", "coconut", "passionfruit",
"blueberry", "vanilla", "hazelnut", "almond", "caramel",
"strawberry", "chocolate", "lemon", "cinnamon", "raspberry",
# ... add more words to the list
]

def generate_passphrase(num_words,add_numbers, min_digits=None, max_digits=None, add_special_chars=True, capitalize_percentage=60, include_spaces=True):
def generate_passphrase(num_words, add_numbers, add_special_chars, min_digits=None, max_digits=None, capitalize_percentage=60, include_spaces=True):
words = []

for _ in range(num_words):
selected_word = secrets.choice(wordlist)
words.append(selected_word.capitalize())

if add_numbers or (min_digits or max_digits):
if add_numbers:
if not min_digits:
min_digits = secrets.randbelow(3) + 1
if not max_digits:
max_digits = secrets.randbelow(4) + 1
max_digits = secrets.randbelow(4) + 1

num_digits = secrets.randbelow(max(1, max_digits - min_digits + 1)) + min_digits
for _ in range(num_digits):
# Ensure that the upper bound for insertion is positive
insert_pos = secrets.randbelow(max(1, len(words) + 1))
words.insert(insert_pos, secrets.choice(string.digits))

num_digits = secrets.randbelow(max_digits - min_digits + 1) + min_digits
for _ in range(num_digits):
words.insert(secrets.randbelow(len(words) + 1), secrets.choice(string.digits))

if add_special_chars:
words.insert(secrets.randbelow(len(words) + 1), secrets.choice(string.punctuation))

secrets.SystemRandom().shuffle(words)

for _ in range(round(len(words) * (capitalize_percentage / 100))):
random_word = secrets.randbelow(len(words))
words[random_word] = words[random_word].capitalize()

if include_spaces:
passphrase = ' '.join(words)
passphrase = ' '.join(words)
else:
passphrase = ''.join(words)
passphrase = ''.join(words)

return passphrase



def toggle_spaces():
include_spaces.set(not include_spaces.get())
generate_button_clicked()

def load_custom_wordlist():
file_path = tkinter.filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
Expand All @@ -74,68 +83,89 @@ def load_custom_wordlist():

def generate_button_clicked():
try:
num_words = int(num_words_entry.get())
num_words_str = num_words_entry.get()
num_words = int(num_words_str)


if num_words <= 0:
raise ValueError("Number of words must be a positive integer")

add_numbers = numbers_var.get()
add_special_chars = special_chars_var.get()
include_spaces_value = include_spaces.get()

passphrase = generate_passphrase(num_words, add_numbers, add_special_chars,capitalize_percentage=100, include_spaces=True)
passphrase = generate_passphrase(num_words, add_numbers, add_special_chars, capitalize_percentage=100, include_spaces=include_spaces_value)

sentence = "Your new password is: " + passphrase
passphrase_label.config(text=sentence)
copy_button.config(state=tk.NORMAL)

strength_result = test_password(passphrase)
strength_label.config(text=f"Password Strength: {strength_result['score']}/4")
feedback_label.config(text="\n".join(strength_result['feedback']))

except ValueError as e:
messagebox.showerror("Error", f"An error occurred while generating the passphrase:\n{e}")



except ValueError:
messagebox.showerror("Error", "Please enter a valid number")

def copy_to_clipboard():
passphrase = passphrase_label.cget("text").replace("Your new password is: ", "")
pyperclip.copy(passphrase)



app = tk.Tk()
app.title("Memorable Password Generator")
app.resizable(width=False, height=False)
style = ttk.Style()
style.theme_use("xpnative")


num_words_label = tk.Label(app, text="Enter the number of words in the passphrase:")
num_words_label = ttk.Label(app, text="Enter the number of words in the passphrase:")
num_words_label.pack()

num_words_entry = tk.Entry(app)

num_words_entry = ttk.Entry(app)
num_words_entry.pack()

# Checkbox for including numbers
numbers_var = tk.BooleanVar()
numbers_check = tk.Checkbutton(app, text="Include a number", variable=numbers_var)
numbers_check = ttk.Checkbutton(app, text="Include a number", variable=numbers_var)
numbers_check.pack()

special_chars_var = tk.BooleanVar()
special_chars_check = tk.Checkbutton(app, text="Include a special character", variable=special_chars_var)
# Checkbox for including special characters
special_chars_var = tk.BooleanVar(value=True)
special_chars_check = ttk.Checkbutton(app, text="Include a special character", variable=special_chars_var)
special_chars_check.pack()

passphrase_label = tk.Label(app, text="", wraplength=300)

passphrase_label = ttk.Label(app, text="", wraplength=300)
passphrase_label.pack()

copy_button = tk.Button(app, text="Copy to Clipboard", command=copy_to_clipboard, state=tk.DISABLED)

copy_button = ttk.Button(app, text="Copy to Clipboard", command=copy_to_clipboard, state=tk.DISABLED)
copy_button.pack()
load_custom_wordlists = tk.Button(app, text="Load Custom Wordlist", command=load_custom_wordlist)


load_custom_wordlists = ttk.Button(app, text="Load Custom Wordlist", command=load_custom_wordlist)
load_custom_wordlists.pack()

# Checkbox for including spaces
include_spaces = tk.BooleanVar(value=True) # Include spaces by default

spaces_check = tk.Checkbutton(app, text="Include Spaces", command=include_spaces)
spaces_check = ttk.Checkbutton(app, text="Include Spaces", variable=include_spaces)
spaces_check.pack()
generate_button = tk.Button(app, text="Generate Memorable Passphrase", command=generate_button_clicked)


generate_button = ttk.Button(app, text="Generate Memorable Passphrase", command=generate_button_clicked)
generate_button.pack()

strength_label = tk.Label(app, text="", wraplength=300)
strength_label = ttk.Label(app, text="", wraplength=300)
strength_label.pack()

feedback_label = tk.Label(app, text="", wraplength=300)

feedback_label = ttk.Label(app, text="", wraplength=300)
feedback_label.pack()


app.mainloop()

app.mainloop()

0 comments on commit 55be756

Please sign in to comment.