Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as rd
- def output_info_problems(problems):
- problems_set = set(problems)
- if 'lower_letter_flag' in problems_set:
- print('----- Отсутствуют строчные латинские буквы -----')
- 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')
- if 'capital_letter_flag' in problems_set:
- print('----- Отсутствуют заглавные латинские буквы -----')
- 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')
- if 'digit_flag' in problems_set:
- print('----- Отсутствуют цифры -----')
- print('<<<<< Можете добавить 1 или более символов из этого списка: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] >>>>>\n')
- if 'special_sym_flag' in problems_set:
- print('----- Отсутствуют специальные символы -----')
- print('<<<<< Можете добавить 1 или более символов из этого списка: [ !, @, #, $, %, ^, &, *, (, ), -, + ] >>>>>\n')
- if 'length_flag' in problems_set:
- print('----- Длина пароля менее восьми символов -----')
- def input_info():
- valid_chars = set('!@#$%^&*()-+0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
- length = int(input("Введите длину будущего пароля: "))
- while length < 1:
- print("Длина пароля должна быть положительным целым числом!")
- length = int(input("Введите длину будущего пароля: "))
- string_sym = input("Введите все символы для пароля (без пробелов): ")
- set_sym = set(string_sym)
- while not all(ch in valid_chars for ch in set_sym):
- print("Введенная последовательность содержит недопустимые символы!")
- string_sym = input("Введите все символы для пароля (без пробелов): ")
- set_sym = set(string_sym)
- return length, list(set_sym)
- def output_info(evaluation, problems, password):
- print()
- if evaluation == 'reliable':
- print('Ваш пароль:', password)
- print('Этот пароль считается НАДЁЖНЫМ, никаких проблем не обнаружено!')
- return False
- elif evaluation == 'average':
- print('Ваш пароль:', password, '\n')
- print('Этот пароль считается СРЕДНИМ, поскольку содержит некоторые недостатки...')
- print('Проблемы данного пароля:')
- output_info_problems(problems)
- else:
- print('Ваш пароль:', password, '\n')
- print('Этот пароль считается НЕНАДЁЖНЫМ и не рекомендуется к использованию, поскольку содержит много недостатков...')
- print('Проблемы данного пароля:')
- output_info_problems(problems)
- print("Желаете создать новый пароль?")
- answer = input("Да/Нет: ")
- return answer.lower() == 'да' or answer.lower() == 'y' or answer.lower() == 'yes'
- def generate_password(length, syms):
- spec_sign_set = set("!@#$%^&*()-+")
- digit_set = set("0123456789")
- lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
- capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- available = {
- "special_sign": [ch for ch in syms if ch in spec_sign_set],
- "digit": [ch for ch in syms if ch in digit_set],
- "lower_letter": [ch for ch in syms if ch in lower_letter_set],
- "capital_letter": [ch for ch in syms if ch in capital_letter_set],
- }
- required_chars = []
- for group in available.values():
- if group:
- required_chars.append(rd.choice(group))
- if length < len(required_chars):
- full_password_list = rd.sample(required_chars, k=length)
- else:
- remaining_length = length - len(required_chars)
- remaining_chars = rd.choices(syms, k=remaining_length)
- full_password_list = required_chars + remaining_chars
- rd.shuffle(full_password_list)
- return ''.join(full_password_list)
- def evaluation_password(password, length):
- dict_flags = {'lower_letter_flag': False, 'capital_letter_flag': False, 'digit_flag': False, 'special_sym_flag': False, 'length_flag': False}
- spec_sign_set = set("!@#$%^&*()-+")
- digit_set = set("0123456789")
- lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
- capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- if length >= 8:
- dict_flags['length_flag'] = True
- if any(ch in spec_sign_set for ch in password):
- dict_flags['special_sym_flag'] = True
- if any(ch in lower_letter_set for ch in password):
- dict_flags['lower_letter_flag'] = True
- if any(ch in capital_letter_set for ch in password):
- dict_flags['capital_letter_flag'] = True
- if any(ch in digit_set for ch in password):
- dict_flags['digit_flag'] = True
- sum_flags = sum([int(i) for i in dict_flags.values()])
- problems = [i[0] for i in dict_flags.items() if i[1] == False]
- if sum_flags == 5:
- return 'reliable', []
- elif sum_flags >= 3:
- return 'average', problems
- else:
- return 'weak', problems
- def main():
- print("Здравствуйте! Вас приветствует программа для генерации надёжного пароля!\n")
- repeat_flag = True
- while repeat_flag:
- length, syms = input_info()
- password = generate_password(length, syms)
- evaluation, problems = evaluation_password(password, length)
- repeat_flag = output_info(evaluation, problems, password)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement