#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # ## Only difference between Code_gen_passwd.py and gen_passwd.py is ## Code_gen_passwd.py uses CodeLIB.get_int and CodeLIB.get_ans ## instead of explicitly defining them within the program (get_int ## and get_characters in gen_passwd.py) """generates passwords by character-type Written by Cody A. Taylor (more well known as code_m). First major project in any programming language, and I am quite satified with the end result. This took about 14 weeks to write in all, with servel small changes as I learned the Python language. Report bugs or sugestions to codemister99@yahoo.com """ __all__ = ['vari', 'user_input', 'main', 'gen_pass'] __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __version__ = "0.4.1.1 Beta (CodeLIB version final)" import random, optparse import CodeLIB def vari(): """Create lists needed through the program I made this function for two reasons: can be used in other programs, and avoids global 'variables' """ lowercase = [] lowercase += "abcdefghijklmnopqrstuvwxyz" uppercase = [] uppercase += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" numbers = [] numbers += "1234567890" symbols = [] symbols += "`~!@#$%^&*(){}[]<>-_=+/?|;:,.'\"" password = [] return lowercase, uppercase, numbers, symbols, password def user_input(): """Input information Try optparse options first, if defaults given (assuming no options) then use questions to get the information """ parser = optparse.OptionParser(version="{0:>7}{1}\n{2:>45}".format("", __version__, __author__), description="A quick password generator, using " "'character-types' for ease", usage="Usage: %prog [char-options] [amount|length]") parser.add_option("-l", "--lowercase", dest="use_lowercase", action="store_true", default=False, help="allow lowercase characters") parser.add_option("-u", "--uppercase", dest="use_uppercase", action="store_true", default=False, help="allow uppercase characters") parser.add_option("-n", "--numbers", dest="use_numbers", action="store_true", default=False, help="allow number characters") parser.add_option("-s", "--symbols", dest="use_symbols", action="store_true", default=False, help="allow symbol characters") parser.add_option("-a", "--amount", dest="amount", type="int", default=10, help="number of passwords to output") parser.add_option("-L", "--length", dest="length", type="int", default=8, help="length of password") opts, args = parser.parse_args() if opts.amount < 1: print("Error: too few passwords given to amount") exit() elif opts.length < 4: print("Error: will not allow length less than 4") exit() if (opts.amount == 10 and opts.length == 8 and opts.use_lowercase == False and opts.use_uppercase == False and opts.use_numbers == False and opts.use_symbols == False): print("Use 'Enter' for defaults.", "Passwords can not be shorter than 4 characters.", "\nDefaults: length=8; amount=10; Lowercase=Yes; Uppercase=Yes; Numbers=Yes; Symbols=Yes") length = CodeLIB.get_int("Length", min=4, default=8, default_style="") amount = CodeLIB.get_int("Number of passwords", name="amount", min=1, default=10, default_style="") use_lowercase = CodeLIB.get_ans("Use lowercase", "? ", name="lowercase", default="Yes", default_style="") use_uppercase = CodeLIB.get_ans("Use Uppercase", "? ", name="uppercase", default="Yes", default_style="") use_numbers = CodeLIB.get_ans("Use numbers", "? ", name="numbers", default="Yes", default_style="") use_symbols = CodeLIB.get_ans("Use symbols", "? ", name="symbols", default="Yes", default_style="") return amount, length, use_lowercase, use_uppercase, use_numbers, use_symbols elif (opts.use_lowercase == False and opts.use_uppercase == False and opts.use_numbers == False and opts.use_symbols == False): return opts.amount, opts.length, True, True, True, True else: return opts.amount, opts.length, opts.use_lowercase, opts.use_uppercase, opts.use_numbers, opts.use_symbols def main(): """Generate and print passwords (simple as possible) """ amount, *info = user_input() for count in range(1, (amount + 1)): password = gen_pass(*info) if password != "Errors": print("{0:>3}: {1}".format(count, password)) else: break def gen_pass(length, use_lowercase, use_uppercase, use_numbers, use_symbols): """Use test and random functions to generate a secure password This is the meat of the program, the rest only assists this function. Order: variables set Error test (all 'use' types are False) while loop so password will be the correct length random number test "use", "number", "length" shuffle that character-type add a random character shuffle password return password >>> gen_pass(30, True, True, True, False) ... >>> gen_pass(20, False, False, False, False) 'Errors' """ lowercase, uppercase, numbers, symbols, password = vari() if use_lowercase == False and use_uppercase == False and use_numbers == False and use_symbols == False: print(" FATAL Error: no character types given, password cannot be generated.") return "Errors" while length > len(password): # adds characters; tests: True? Randint? Length? x = random.randint(0,5) if use_lowercase == True and x == 1 and length > len(password): random.shuffle(lowercase) for char in random.choice(lowercase): password += char if use_uppercase == True and x == 2 and length > len(password): random.shuffle(uppercase) for char in random.choice(uppercase): password += char if use_numbers == True and x == 3 and length > len(password): random.shuffle(numbers) for char in random.choice(numbers): password += char if use_symbols == True and x == 4 and length > len(password): random.shuffle(symbols) for char in random.choice(symbols): password += char ## Randomize password with random.shuffle(); if password != "Errors": random.shuffle(password) return "".join(password) if __name__ == "__main__": main()