Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- import secrets
- # Returns a randomly generated sequence of characters of provided length with at least one digit and upper case letter.
- # The first character must be a letter. It will also ensure that at least one character in the parameter, symbols, is provided
- # Input:
- # length: Int; Length of the random sequence. Default is 8.
- # chars: String; Character set for generating random character. Default is all letters and numbers.
- # symbols: String; Additional characters to add to character set Default is None.
- # Output:
- # passwd: String; Contains none if length provided is less than 4. Otherwise contains the random sequence as a string.
- def password_generator(length=8, chars=string.ascii_letters + string.digits, symbols=None):
- if length < 4:
- return None
- secrets.SystemRandom()
- if symbols is not None:
- chars = string.ascii_letters + string.digits + symbols
- while True:
- passwd = ''.join(secrets.choice(chars) for i in range(length))
- if symbols:
- if (any(c.islower() for c in passwd)
- and any(c.isupper() for c in passwd)
- and any(c.isdigit() for c in passwd)
- and passwd[0].isalpha()
- and any((c in symbols) for c in passwd)):
- break
- else:
- if (any(c.islower() for c in passwd)
- and any(c.isupper() for c in passwd)
- and any(c.isdigit() for c in passwd)
- and passwd[0].isalpha()):
- break
- return passwd
Add Comment
Please, Sign In to add comment