passgen

Форк
0
/
main.py 
189 строк · 6.7 Кб
1
import string
2
import tkinter as tk
3
import random
4
from tkinter import messagebox
5

6
excluded_chars = "Il0oO"
7
#default_special_chars = "!@#$%^&*()_+-"
8

9
root = tk.Tk()
10
root.geometry("280x560")
11
#root.resizable(width=False, height=True)
12
root.minsize(280,330)
13
root.maxsize(280,600)
14
root.title("Passwd")
15
root.resizable(0,1)
16

17
def generate_password(length, chars, password_length):
18
    password = ''
19
    char_pool = []
20
    include_upper = use_upper.get()
21
    include_lower = use_lower.get()
22
    include_num = use_num.get()
23
    include_special = use_special.get()
24
    
25
    if include_upper:
26
        char_pool.extend(string.ascii_uppercase)
27
    if include_lower:
28
        char_pool.extend(string.ascii_lowercase)
29
    if include_num:
30
        char_pool.extend(string.digits)
31
        
32
    special_chars = []
33
    if include_special:
34
        special_chars_entry = special_entry.get().strip()
35
        if special_chars_entry:
36
            special_chars.extend(special_chars_entry)
37
        else:
38
            special_chars.extend(default_special_chars)
39
            
40
        char_pool.extend(special_chars)
41
    
42
    char_pool = [c for c in char_pool if c not in excluded_chars]
43
    
44
    if len(char_pool) == 0:
45
        messagebox.showwarning("Warning", "Please select at least one valid character type.")
46
        return
47
    
48
    if include_upper:
49
        # ensure at least one uppercase character is included
50
        password += random.choice(string.ascii_uppercase)
51
        length -= 1
52
    
53
    if include_lower:
54
        # ensure at least one lowercase character is included
55
        password += random.choice(string.ascii_lowercase)
56
        length -= 1
57
    
58
    if include_num:
59
        # ensure at least one numeric character is included
60
        password += random.choice(string.digits)
61
        length -= 1
62
    
63
    if include_special:
64
        # ensure at least one special character is included
65
        if len(special_chars) == 0:
66
            special_chars = char_pool
67
        password += random.choice(special_chars)
68
        length -= 1
69
    
70
    if len(char_pool) == 0:
71
        messagebox.showwarning("Warning", "Please select at least one valid character type.")
72
        return
73
    
74
    password += ''.join(random.choices(char_pool, k=length))
75
    
76
    # Replace excluded characters with allowed ones
77
    password = [c if c not in excluded_chars else random.choice(char_pool) for c in password]
78
    password = ''.join(password)
79
    
80
    password_list = list(password)
81
    random.shuffle(password_list)
82
    password = ''.join(password_list)
83
    
84
    # Check if all selected characters are included in the password
85
    if include_upper and not any(c in password for c in string.ascii_uppercase):
86
        password += random.choice(string.ascii_uppercase)
87
    if include_lower and not any(c in password for c in string.ascii_lowercase):
88
        password += random.choice(string.ascii_lowercase)
89
    if include_num and not any(c in password for c in string.digits):
90
        password += random.choice(string.digits)
91
    if include_special and not any(c in password for c in special_chars):
92
        password += random.choice(special_chars)
93
    
94
    if len(password) != password_length:
95
        return generate_password(length, chars, password_length)
96
    
97
    return password
98

99

100
def generate_and_display_password():
101
        password = None
102
        password_end = ""
103
        password_count = length_count.get()
104
        for _ in range(password_count):
105
            if not any((use_upper.get(), use_lower.get(), use_num.get(), use_special.get())):
106
                messagebox.showwarning("Warning", "Please select at least one checkbox.")
107
                return
108
            include_special = use_special.get()
109
            special_chars = special_entry.get().strip()
110
            if include_special and not special_chars:
111
                messagebox.showwarning("Warning", "Please enter some special characters.")
112
                return
113
            custom_chars = "".join(c for c in (
114
                string.ascii_uppercase if use_upper.get() else "",
115
                string.ascii_lowercase if use_lower.get() else "",
116
                string.digits if use_num.get() else "",
117
                special_chars if include_special else "",
118
            ) if c not in excluded_chars)
119
            password_length = length_scale.get()
120
            password = None
121
            while not password:
122
                password = generate_password(password_length, custom_chars, password_length)
123
                password_end += password + "\n"
124
            result_label.config(text=password_end, font=("Arial", 17, "bold"))
125

126
def copy_to_clipboard_1():
127
    a = length_scale.get()
128
    password = result_label.cget("text")
129
    password = password[0:a]
130
    root.clipboard_clear()
131
    root.clipboard_append(password)
132

133
def copy_to_clipboard_a():
134
    password = result_label.cget("text")
135
    password = password[0:-1]
136
    root.clipboard_clear()
137
    root.clipboard_append(password)
138

139
length_scale = tk.Scale(root, from_=8, to=15, orient=tk.HORIZONTAL, label="Length")
140
length_scale.set(8)
141
length_scale.pack()
142

143
length_count = tk.Scale(root, from_=1, to=10, orient=tk.HORIZONTAL, label="Count")
144
length_count.set(1)
145
length_count.pack()
146

147
check_button_frame = tk.Frame(root)
148
check_button_frame.pack()
149
#custom_special_entry = tk.Entry(root)
150
#custom_special_entry.pack()
151

152
use_upper = tk.BooleanVar()
153
use_upper.set(True)
154
upper_checkbutton = tk.Checkbutton(check_button_frame, text="Upper", variable=use_upper)
155
upper_checkbutton.grid(row=0, column=0, padx=0, pady=0)
156

157
use_lower = tk.BooleanVar()
158
use_lower.set(True)
159
lower_checkbutton = tk.Checkbutton(check_button_frame, text="Lower", variable=use_lower)
160
lower_checkbutton.grid(row=0, column=1, padx=0, pady=0)
161

162
use_num = tk.BooleanVar()
163
use_num.set(True)
164
num_checkbutton = tk.Checkbutton(check_button_frame, text="Num", variable=use_num)
165
num_checkbutton.grid(row=1, column=0, padx=0, pady=0)
166

167
use_special = tk.BooleanVar()
168
use_special.set(True)
169
special_checkbutton = tk.Checkbutton(check_button_frame, text="Special", variable=use_special)
170
special_checkbutton.grid(row=1, column=1, padx=0, pady=0)
171

172
default_special_chars = "!@#$%^&*()_+-"
173
special_entry = tk.Entry(check_button_frame)
174
special_entry.insert(0, default_special_chars)
175
special_entry.grid(row=2, columnspan=2, padx=0, pady=0)
176

177
button = tk.Button(root, text="Generate Password", command=generate_and_display_password)
178
button.pack(pady=0)
179

180
copy_button = tk.Button(root, text="Copy 1", command=copy_to_clipboard_1)
181
copy_button.pack(pady=0)
182

183
copy_button = tk.Button(root, text="Copy All", command=copy_to_clipboard_a)
184
copy_button.pack(pady=0)
185

186
result_label = tk.Label(root, text="", font=("Arial", 17, "bold"))
187
result_label.pack()
188

189
root.mainloop()
190

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.