· 3 months ago · Jul 06, 2025, 04:35 PM
1import random as rd
2
3
4def output_info_problems(problems):
5 problems_set = set(problems)
6
7 if 'lower_letter_flag' in problems_set:
8 print('----- Отсутствуют строчные латинские буквы -----')
9 print('<<<<< Можете добавить 1 или более символов из этого списка: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z ] >>>>>\n')
10
11 if 'capital_letter_flag' in problems_set:
12 print('----- Отсутствуют заглавные латинские буквы -----')
13 print('<<<<< Можете добавить 1 или более символов из этого списка: [ A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z ] >>>>>\n')
14
15 if 'digit_flag' in problems_set:
16 print('----- Отсутствуют цифры -----')
17 print('<<<<< Можете добавить 1 или более символов из этого списка: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] >>>>>\n')
18
19 if 'special_sym_flag' in problems_set:
20 print('----- Отсутствуют специальные символы -----')
21 print('<<<<< Можете добавить 1 или более символов из этого списка: [ !, @, #, $, %, ^, &, *, (, ), -, + ] >>>>>\n')
22
23 if 'length_flag' in problems_set:
24 print('----- Длина пароля менее восьми символов -----')
25
26
27def input_info():
28 valid_chars = set('!@#$%^&*()-+0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
29
30 length = int(input("Введите длину будущего пароля: "))
31 while length < 1:
32 print("Длина пароля должна быть положительным целым числом!")
33 length = int(input("Введите длину будущего пароля: "))
34
35 string_sym = input("Введите все символы для пароля (без пробелов): ")
36 set_sym = set(string_sym)
37 while not all(ch in valid_chars for ch in set_sym):
38 print("Введенная последовательность содержит недопустимые символы!")
39 string_sym = input("Введите все символы для пароля (без пробелов): ")
40 set_sym = set(string_sym)
41
42 return length, list(set_sym)
43
44
45
46def output_info(evaluation, problems, password):
47 print()
48 if evaluation == 'reliable':
49 print('Ваш пароль:', password)
50 print('Этот пароль считается НАДЁЖНЫМ, никаких проблем не обнаружено!')
51 return False
52
53 elif evaluation == 'average':
54 print('Ваш пароль:', password, '\n')
55 print('Этот пароль считается СРЕДНИМ, поскольку содержит некоторые недостатки...')
56 print('Проблемы данного пароля:')
57 output_info_problems(problems)
58
59 else:
60 print('Ваш пароль:', password, '\n')
61 print('Этот пароль считается НЕНАДЁЖНЫМ и не рекомендуется к использованию, поскольку содержит много недостатков...')
62 print('Проблемы данного пароля:')
63 output_info_problems(problems)
64
65 print("Желаете создать новый пароль?")
66 answer = input("Да/Нет: ")
67 return answer.lower() == 'да' or answer.lower() == 'y' or answer.lower() == 'yes'
68
69
70
71def generate_password(length, syms):
72 spec_sign_set = set("!@#$%^&*()-+")
73 digit_set = set("0123456789")
74 lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
75 capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
76
77 available = {
78 "special_sign": [ch for ch in syms if ch in spec_sign_set],
79 "digit": [ch for ch in syms if ch in digit_set],
80 "lower_letter": [ch for ch in syms if ch in lower_letter_set],
81 "capital_letter": [ch for ch in syms if ch in capital_letter_set],
82 }
83
84 required_chars = []
85 for group in available.values():
86 if group:
87 required_chars.append(rd.choice(group))
88
89 if length < len(required_chars):
90 full_password_list = rd.sample(required_chars, k=length)
91 else:
92 remaining_length = length - len(required_chars)
93 remaining_chars = rd.choices(syms, k=remaining_length)
94
95 full_password_list = required_chars + remaining_chars
96 rd.shuffle(full_password_list)
97
98 return ''.join(full_password_list)
99
100
101def evaluation_password(password, length):
102 dict_flags = {'lower_letter_flag': False, 'capital_letter_flag': False, 'digit_flag': False, 'special_sym_flag': False, 'length_flag': False}
103
104 spec_sign_set = set("!@#$%^&*()-+")
105 digit_set = set("0123456789")
106 lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
107 capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
108
109 if length >= 8:
110 dict_flags['length_flag'] = True
111
112 if any(ch in spec_sign_set for ch in password):
113 dict_flags['special_sym_flag'] = True
114
115 if any(ch in lower_letter_set for ch in password):
116 dict_flags['lower_letter_flag'] = True
117
118 if any(ch in capital_letter_set for ch in password):
119 dict_flags['capital_letter_flag'] = True
120
121 if any(ch in digit_set for ch in password):
122 dict_flags['digit_flag'] = True
123
124 sum_flags = sum([int(i) for i in dict_flags.values()])
125
126 problems = [i[0] for i in dict_flags.items() if i[1] == False]
127
128 if sum_flags == 5:
129 return 'reliable', []
130 elif sum_flags >= 3:
131 return 'average', problems
132 else:
133 return 'weak', problems
134
135
136def main():
137 print("Здравствуйте! Вас приветствует программа для генерации надёжного пароля!\n")
138
139 repeat_flag = True
140
141 while repeat_flag:
142 length, syms = input_info()
143
144 password = generate_password(length, syms)
145
146 evaluation, problems = evaluation_password(password, length)
147
148 repeat_flag = output_info(evaluation, problems, password)
149
150
151
152main()