Skip to content

Instantly share code, notes, and snippets.

@friek
Created December 1, 2015 20:10
Show Gist options
  • Save friek/d0c3ca56cc4ef8dc98c8 to your computer and use it in GitHub Desktop.
Save friek/d0c3ca56cc4ef8dc98c8 to your computer and use it in GitHub Desktop.
Python password generator
#!/usr/bin/env python
import crypt
import random
import string
import sys
PASSWORD_CHARS = string.ascii_uppercase + string.ascii_lowercase + string.digits
MD5_SALT_CHARS = PASSWORD_CHARS + '/'
DES_SALT_CHARS = string.ascii_uppercase + string.ascii_lowercase
def random_string(N, charset=PASSWORD_CHARS):
# http://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python
return ''.join(random.choice(charset) for _ in range(N))
if __name__ == '__main__':
pw_len = 9
if len(sys.argv) > 1:
pw_len = int(sys.argv[1])
password = random_string(pw_len)
# This only works when the libc crypt function supports it.
md5 = crypt.crypt(password, '$1$' + random_string(7, MD5_SALT_CHARS) + '$')
des = crypt.crypt(password, random_string(2, DES_SALT_CHARS))
print("Password: {0}\nMD5: {1}\nDES: {2}".format(password, md5, des))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment