Created
December 1, 2015 20:10
Python password generator
This file contains hidden or 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 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