Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Last active July 18, 2017 04:53
Show Gist options
  • Save paulosuzart/8ae106425714f6bbf8fd61e2a9b0a6f0 to your computer and use it in GitHub Desktop.
Save paulosuzart/8ae106425714f6bbf8fd61e2a9b0a6f0 to your computer and use it in GitHub Desktop.
from string import ascii_lowercase as lowercase
from string import ascii_uppercase as uppercase
from string import digits, punctuation
from random import randint, sample, shuffle
sources = [lowercase, digits, uppercase, punctuation]
def make_password(length, complexity):
assert(length >= 4)
assert(complexity >= 1 and complexity <= 4)
pass_list = []
# make sure we have the minimum for the complexities already present
for n in range(complexity):
pass_list.extend(sample(sources[n], 1))
# now extend the list with N elements where N is total length minus complexity
for x in range(length - complexity):
# the elements will be get from any source that is covered by required complexity
pass_list.extend(sample(sources[randint(0, complexity - 1)], 1))
# shuffle to look better
shuffle(pass_list)
return ''.join(pass_list)
if __name__ == '__main__':
length = input('Please provide the size of your pass: ')
complexity = input('Please provide the complexity of your pass (1 to 4): ')
print(make_password(int(length), int(complexity)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment