Skip to content

Instantly share code, notes, and snippets.

@queirozsc
Last active March 25, 2018 13:43
Show Gist options
  • Save queirozsc/3e38dd117d22b33ec2996dde0174a57e to your computer and use it in GitHub Desktop.
Save queirozsc/3e38dd117d22b33ec2996dde0174a57e to your computer and use it in GitHub Desktop.
Python Generating Passwords
from string import punctuation, ascii_letters, digits
import random
def generate_password(pass_length):
symbols = ascii_letters + digits + punctuation
secure_random = random.SystemRandom()
password = "".join(secure_random.choice(symbols) for i in range(pass_length))
return password
generate_password(15)
from string import ascii_letters, digits
import random
def generate_specific_password(pass_length):
"""
Generates an alphanumeric password with at least one lowercase character,
at least one uppercase character and at least three digits
"""
alphabet = ascii_letters + digits
secure_random = random.SystemRandom()
while True:
password = ''.join(secure_random.choice(alphabet) for i in range(pass_length))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
return password
generate_specific_password(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment