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