Last active
July 31, 2020 04:20
-
-
Save voice1/05acf1cbbc7f5d5b8e601578f7aa60bb to your computer and use it in GitHub Desktop.
Generate random password types of different complexity using pure python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Simple function to create random generated password types. | |
See examples at the end of gist. | |
""" | |
import os, string, random | |
def genpass(digits=False, length=13, special_char_set='!@#$%', | |
repeat_chars=True, | |
require_special_char=True, | |
require_numbers=True, | |
require_uppercase=True, | |
require_lowercase=True): | |
""" | |
Generate different password types. | |
:param digits: Returns only random digits. | |
:param length: Max length of password generated. | |
:param special_char_set: Use the supplied chars as special chars. | |
:param repeat_chars: True allows repeating of a char. | |
:param require_special_char: If set to False, no special characters are generated. | |
:param require_numbers: If set to False, no numbers are generated. | |
:param require_uppercase: | |
:param require_lowercase: | |
:return: | |
""" | |
random.seed = (os.urandom(1024)) | |
if digits: | |
# Used for pin numbers | |
chars = string.digits | |
password = ''.join(random.choice(chars) for i in range(length)) | |
return password | |
# Generate individual lists of strings. | |
letters_lower = string.ascii_lowercase | |
letters_upper = string.ascii_uppercase | |
numbers = string.digits | |
if not special_char_set: | |
special_char_set = string.punctuation | |
# Generate normal string of options | |
chars = letters_lower + letters_upper | |
# if required_* set, generate at least 1 required char from lists and concat | |
# results to req_chars | |
req_chars = '' | |
if require_lowercase: | |
req_chars += ''.join((random.choice(letters_lower) for i in range(1))) | |
if require_uppercase: | |
req_chars += ''.join((random.choice(letters_upper) for i in range(1))) | |
if require_numbers: | |
chars += numbers | |
req_chars += ''.join((random.choice(numbers) for i in range(1))) | |
if require_special_char: | |
chars += special_char_set | |
req_chars += ''.join((random.choice(special_char_set) for i in range(1))) | |
if not repeat_chars: | |
limited_chars = ''.join(random.sample(chars, len(chars))) | |
password = ''.join(random.choice(limited_chars) for i in range(length)) | |
else: | |
password = ''.join(random.choice(chars) for i in range(length)) | |
# if there is a req_char set, then concat the generated password with the req_char and shuffle | |
if req_chars: | |
# shorten the passsword by the number of req_chars, and concat the req_chars to the password. | |
password = password[:-len(req_chars)] | |
password += req_chars | |
# Convert password to list and shuffle results. | |
char_list = list(password) | |
random.shuffle(char_list) | |
password = ''.join(char_list) | |
return password | |
# Examples: | |
genpass(digits=True, length=50) | |
#'32960559941701058915204312703775203197166016081777' | |
genpass(length=50) | |
#'fMI2X5oicUv%FlT!dHEuSQKkvmM9sexCXhPC99D%Y$!UqTuZx4' | |
genpass(length=50, require_special_char=False) | |
#'G5F0nAOyHkwTgo38S9b89BVw7AkYr13HOgOzMMl9fjfvdexpsE' | |
genpass(length=50, require_numbers=False) | |
#'aWa!#dq!asLjOKiDVonmsJRH@IxICFaCMsH!rSzZPRQAqncnrv' | |
genpass(length=50, require_numbers=False, require_special_char=False) | |
#'OjwneoMymKREuiCoZMERYMOFqOrAyZbHZOMYftlSpyuarEvJzy' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment