#!/usr/bin/python3 ''' Generate a random password If you give a user an easy password, they'll probably never change it. If you give a user a password that's a pain to type, they'll probably change it to something only they know. ''' # pylint: disable=superfluous-parens # superfluous-parens: Parentheses are good for clarity and portability import crypt import random def main(): '''Main function''' legal_password_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_-+=' passwd = [] for i in range(20): dummy = i passwd.append(random.choice(legal_password_characters)) passwd = ''.join(passwd) print('plaintext: %s' % passwd) if hasattr(crypt, 'METHOD_CRYPT'): method_crypt = getattr(crypt, 'METHOD_CRYPT') crypt_version = crypt.crypt(passwd, method_crypt) else: legal_salt_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' crypt_version = crypt.crypt(passwd, legal_salt_characters) print('traditional version: %s' % crypt_version) if hasattr(crypt, 'METHOD_MD5'): method_md5 = getattr(crypt, 'METHOD_MD5') md5_version = crypt.crypt(passwd, method_md5) print('md5 version: %s' % md5_version) main()