Advertisement
Brusnik

Проект №1

Jul 6th, 2025
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.31 KB | None | 0 0
  1. import random as rd
  2. from zoneinfo import available_timezones
  3.  
  4.  
  5. def output_info_problems(problems):
  6.     problems_set = set(problems)
  7.  
  8.     if 'lower_letter_flag' in problems_set:
  9.         print('----- Отсутствуют строчные латинские буквы -----')
  10.         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')
  11.  
  12.     if 'capital_letter_flag' in problems_set:
  13.         print('----- Отсутствуют заглавные латинские буквы -----')
  14.         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')
  15.  
  16.     if 'digit_flag' in problems_set:
  17.         print('----- Отсутствуют цифры -----')
  18.         print('<<<<< Можете добавить 1 или более символов из этого списка: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] >>>>>\n')
  19.  
  20.     if 'special_sym_flag' in problems_set:
  21.         print('----- Отсутствуют специальные символы -----')
  22.         print('<<<<< Можете добавить 1 или более символов из этого списка: [ !, @, #, $, %, ^, &, *, (, ), -, + ] >>>>>\n')
  23.  
  24.     if 'length_flag' in problems_set:
  25.         print('----- Длина пароля менее восьми символов -----')
  26.  
  27.  
  28. def input_info():
  29.     valid_chars = set('!@#$%^&*()-+0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  30.  
  31.     length = int(input("Введите длину будущего пароля: "))
  32.     while length < 1:
  33.         print("Длина пароля должна быть положительным целым числом!")
  34.         length = int(input("Введите длину будущего пароля: "))
  35.  
  36.     string_sym = input("Введите все символы для пароля (без пробелов): ")
  37.     set_sym = set(string_sym)
  38.     while not all(ch in valid_chars for ch in set_sym):
  39.         print("Введенная последовательность содержит недопустимые символы!")
  40.         string_sym = input("Введите все символы для пароля (без пробелов): ")
  41.         set_sym = set(string_sym)
  42.  
  43.     return length, list(set_sym)
  44.  
  45.  
  46.  
  47. def output_info(evaluation, problems, password):
  48.     print()
  49.     if evaluation == 'reliable':
  50.         print('Ваш пароль:', password)
  51.         print('Этот пароль считается НАДЁЖНЫМ, никаких проблем не обнаружено!')
  52.         return False
  53.  
  54.     elif evaluation == 'average':
  55.         print('Ваш пароль:', password, '\n')
  56.         print('Этот пароль считается СРЕДНИМ, поскольку содержит некоторые недостатки...')
  57.         print('Проблемы данного пароля:')
  58.         output_info_problems(problems)
  59.  
  60.     else:
  61.         print('Ваш пароль:', password, '\n')
  62.         print('Этот пароль считается НЕНАДЁЖНЫМ и не рекомендуется к использованию, поскольку содержит много недостатков...')
  63.         print('Проблемы данного пароля:')
  64.         output_info_problems(problems)
  65.  
  66.     print("Желаете создать новый пароль?")
  67.     answer = input("Да/Нет: ")
  68.     return answer.lower() == 'да' or answer.lower() == 'y' or answer.lower() == 'yes'
  69.  
  70.  
  71.  
  72. def generate_password(length, syms):
  73.     spec_sign_set = set("!@#$%^&*()-+")
  74.     digit_set = set("0123456789")
  75.     lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
  76.     capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  77.  
  78.     available = {
  79.         "special_sign": [ch for ch in syms if ch in spec_sign_set],
  80.         "digit": [ch for ch in syms if ch in digit_set],
  81.         "lower_letter": [ch for ch in syms if ch in lower_letter_set],
  82.         "capital_letter": [ch for ch in syms if ch in capital_letter_set],
  83.     }
  84.  
  85.     required_chars = []
  86.     for group in available.values():
  87.         if group:
  88.             required_chars.append(rd.choice(group))
  89.  
  90.     if length < len(required_chars):
  91.         full_password_list = rd.sample(required_chars, k=length)
  92.     else:
  93.         remaining_length = length - len(required_chars)
  94.         remaining_chars = rd.choices(syms, k=remaining_length)
  95.  
  96.         full_password_list = required_chars + remaining_chars
  97.         rd.shuffle(full_password_list)
  98.  
  99.     return ''.join(full_password_list)
  100.  
  101.  
  102. def evaluation_password(password, length):
  103.     dict_flags = {'lower_letter_flag': False, 'capital_letter_flag': False, 'digit_flag': False, 'special_sym_flag': False, 'length_flag': False}
  104.  
  105.     spec_sign_set = set("!@#$%^&*()-+")
  106.     digit_set = set("0123456789")
  107.     lower_letter_set = set("abcdefghijklmnopqrstuvwxyz")
  108.     capital_letter_set = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  109.  
  110.     if length >= 8:
  111.         dict_flags['length_flag'] = True
  112.  
  113.     if any(ch in spec_sign_set for ch in password):
  114.         dict_flags['special_sym_flag'] = True
  115.  
  116.     if any(ch in lower_letter_set for ch in password):
  117.         dict_flags['lower_letter_flag'] = True
  118.  
  119.     if any(ch in capital_letter_set for ch in password):
  120.         dict_flags['capital_letter_flag'] = True
  121.  
  122.     if any(ch in digit_set for ch in password):
  123.         dict_flags['digit_flag'] = True
  124.  
  125.     sum_flags = sum([int(i) for i in dict_flags.values()])
  126.  
  127.     problems = [i[0] for i in dict_flags.items() if i[1] == False]
  128.  
  129.     if sum_flags == 5:
  130.         return 'reliable', []
  131.     elif sum_flags >= 3:
  132.         return 'average', problems
  133.     else:
  134.         return 'weak', problems
  135.  
  136.  
  137. def main():
  138.     print("Здравствуйте! Вас приветствует программа для генерации надёжного пароля!\n")
  139.  
  140.     repeat_flag = True
  141.  
  142.     while repeat_flag:
  143.         length, syms = input_info()
  144.  
  145.         password = generate_password(length, syms)
  146.  
  147.         evaluation, problems = evaluation_password(password, length)
  148.  
  149.         repeat_flag = output_info(evaluation, problems, password)
  150.  
  151.  
  152.  
  153. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement