optimized password generation function

This commit is contained in:
StNicolay 2022-11-14 16:54:31 +03:00
parent a61deca6fa
commit 9d5c52bebe

View File

@ -106,17 +106,17 @@ def check_account(name: str, login: str, passwd: str) -> bool:
def gen_passwd() -> str: def gen_passwd() -> str:
"""Generates password of length 32""" """Generates password of length 32"""
choices = SystemRandom().choices choices = SystemRandom().choices
chars = frozenset(string.ascii_letters + string.digits + string.punctuation) # Remove backtick and pipe from pucntuation
# Remove backtick and pipe characters and convert into tuple punctuation = set(string.punctuation).difference("`|")
chars = tuple(chars.difference("`|")) chars = tuple(string.ascii_letters + string.digits + "".join(punctuation))
while True: while True:
passwd = "".join(choices(chars, k=32)) passwd = "".join(choices(chars, k=32))
passwd_chars = frozenset(passwd)
# If there is at least one lowercase character, uppercase character # If there is at least one lowercase character, uppercase character
# and one punctuation character # and one punctuation character
if ( if (
passwd_chars.intersection(string.ascii_lowercase) any(c.islower() for c in passwd)
and passwd_chars.intersection(string.ascii_uppercase) and any(c.isupper() for c in passwd)
and passwd_chars.intersection(string.punctuation) and any(c.isdigit() for c in passwd)
and any(c in punctuation for c in passwd)
): ):
return passwd return passwd