passgen
/
main.py
189 строк · 6.7 Кб
1import string2import tkinter as tk3import random4from tkinter import messagebox5
6excluded_chars = "Il0oO"7#default_special_chars = "!@#$%^&*()_+-"
8
9root = tk.Tk()10root.geometry("280x560")11#root.resizable(width=False, height=True)
12root.minsize(280,330)13root.maxsize(280,600)14root.title("Passwd")15root.resizable(0,1)16
17def generate_password(length, chars, password_length):18password = ''19char_pool = []20include_upper = use_upper.get()21include_lower = use_lower.get()22include_num = use_num.get()23include_special = use_special.get()24
25if include_upper:26char_pool.extend(string.ascii_uppercase)27if include_lower:28char_pool.extend(string.ascii_lowercase)29if include_num:30char_pool.extend(string.digits)31
32special_chars = []33if include_special:34special_chars_entry = special_entry.get().strip()35if special_chars_entry:36special_chars.extend(special_chars_entry)37else:38special_chars.extend(default_special_chars)39
40char_pool.extend(special_chars)41
42char_pool = [c for c in char_pool if c not in excluded_chars]43
44if len(char_pool) == 0:45messagebox.showwarning("Warning", "Please select at least one valid character type.")46return47
48if include_upper:49# ensure at least one uppercase character is included50password += random.choice(string.ascii_uppercase)51length -= 152
53if include_lower:54# ensure at least one lowercase character is included55password += random.choice(string.ascii_lowercase)56length -= 157
58if include_num:59# ensure at least one numeric character is included60password += random.choice(string.digits)61length -= 162
63if include_special:64# ensure at least one special character is included65if len(special_chars) == 0:66special_chars = char_pool67password += random.choice(special_chars)68length -= 169
70if len(char_pool) == 0:71messagebox.showwarning("Warning", "Please select at least one valid character type.")72return73
74password += ''.join(random.choices(char_pool, k=length))75
76# Replace excluded characters with allowed ones77password = [c if c not in excluded_chars else random.choice(char_pool) for c in password]78password = ''.join(password)79
80password_list = list(password)81random.shuffle(password_list)82password = ''.join(password_list)83
84# Check if all selected characters are included in the password85if include_upper and not any(c in password for c in string.ascii_uppercase):86password += random.choice(string.ascii_uppercase)87if include_lower and not any(c in password for c in string.ascii_lowercase):88password += random.choice(string.ascii_lowercase)89if include_num and not any(c in password for c in string.digits):90password += random.choice(string.digits)91if include_special and not any(c in password for c in special_chars):92password += random.choice(special_chars)93
94if len(password) != password_length:95return generate_password(length, chars, password_length)96
97return password98
99
100def generate_and_display_password():101password = None102password_end = ""103password_count = length_count.get()104for _ in range(password_count):105if not any((use_upper.get(), use_lower.get(), use_num.get(), use_special.get())):106messagebox.showwarning("Warning", "Please select at least one checkbox.")107return108include_special = use_special.get()109special_chars = special_entry.get().strip()110if include_special and not special_chars:111messagebox.showwarning("Warning", "Please enter some special characters.")112return113custom_chars = "".join(c for c in (114string.ascii_uppercase if use_upper.get() else "",115string.ascii_lowercase if use_lower.get() else "",116string.digits if use_num.get() else "",117special_chars if include_special else "",118) if c not in excluded_chars)119password_length = length_scale.get()120password = None121while not password:122password = generate_password(password_length, custom_chars, password_length)123password_end += password + "\n"124result_label.config(text=password_end, font=("Arial", 17, "bold"))125
126def copy_to_clipboard_1():127a = length_scale.get()128password = result_label.cget("text")129password = password[0:a]130root.clipboard_clear()131root.clipboard_append(password)132
133def copy_to_clipboard_a():134password = result_label.cget("text")135password = password[0:-1]136root.clipboard_clear()137root.clipboard_append(password)138
139length_scale = tk.Scale(root, from_=8, to=15, orient=tk.HORIZONTAL, label="Length")140length_scale.set(8)141length_scale.pack()142
143length_count = tk.Scale(root, from_=1, to=10, orient=tk.HORIZONTAL, label="Count")144length_count.set(1)145length_count.pack()146
147check_button_frame = tk.Frame(root)148check_button_frame.pack()149#custom_special_entry = tk.Entry(root)
150#custom_special_entry.pack()
151
152use_upper = tk.BooleanVar()153use_upper.set(True)154upper_checkbutton = tk.Checkbutton(check_button_frame, text="Upper", variable=use_upper)155upper_checkbutton.grid(row=0, column=0, padx=0, pady=0)156
157use_lower = tk.BooleanVar()158use_lower.set(True)159lower_checkbutton = tk.Checkbutton(check_button_frame, text="Lower", variable=use_lower)160lower_checkbutton.grid(row=0, column=1, padx=0, pady=0)161
162use_num = tk.BooleanVar()163use_num.set(True)164num_checkbutton = tk.Checkbutton(check_button_frame, text="Num", variable=use_num)165num_checkbutton.grid(row=1, column=0, padx=0, pady=0)166
167use_special = tk.BooleanVar()168use_special.set(True)169special_checkbutton = tk.Checkbutton(check_button_frame, text="Special", variable=use_special)170special_checkbutton.grid(row=1, column=1, padx=0, pady=0)171
172default_special_chars = "!@#$%^&*()_+-"173special_entry = tk.Entry(check_button_frame)174special_entry.insert(0, default_special_chars)175special_entry.grid(row=2, columnspan=2, padx=0, pady=0)176
177button = tk.Button(root, text="Generate Password", command=generate_and_display_password)178button.pack(pady=0)179
180copy_button = tk.Button(root, text="Copy 1", command=copy_to_clipboard_1)181copy_button.pack(pady=0)182
183copy_button = tk.Button(root, text="Copy All", command=copy_to_clipboard_a)184copy_button.pack(pady=0)185
186result_label = tk.Label(root, text="", font=("Arial", 17, "bold"))187result_label.pack()188
189root.mainloop()190