|
| 1 | +import argparse |
| 2 | +import random |
| 3 | +import string |
| 4 | + |
| 5 | + |
| 6 | +def PasswordGenerator(combine, length): |
| 7 | + """ Generates a random password having the specified length. Default length 8 is passed """ |
| 8 | + |
| 9 | + # Sends the combined string to choice method |
| 10 | + password = ''.join(random.choice(combine) for x in range(length)) |
| 11 | + |
| 12 | + print(f"String Of Length {length} is: {password}") |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + parser = argparse.ArgumentParser( |
| 17 | + description='GENERATE RANDOM PASSWORD OF LENGTH SPECIFIED WITH MIX AND MATCH. DEFAULT HAS ASCII,DIGITS AND ' |
| 18 | + 'PUNCTUATIONS INCLUDED WITH LENGTH OF EIGHT') |
| 19 | + |
| 20 | + # Argument is for a length of the password |
| 21 | + parser.add_argument('-l', dest='length', type=int, nargs='?', const=8, help='Length For Password', required=True) |
| 22 | + |
| 23 | + # Argument is for ascii characters |
| 24 | + parser.add_argument('-a', dest='ascii', action='store_true', help='Use ASCII Characters') |
| 25 | + |
| 26 | + # Argument is for digits |
| 27 | + parser.add_argument('-d', dest='digit', action='store_true', help='Use Digits') |
| 28 | + |
| 29 | + # Argument is for punctuations |
| 30 | + parser.add_argument('-p', dest='punctuation', action='store_true', help='Use Special Characters') |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + # Checks if the number is a positive integer |
| 35 | + if args.length > 0: |
| 36 | + lenPass = args.length |
| 37 | + |
| 38 | + # create alphanumerical from string constants |
| 39 | + combine = [string.ascii_letters, string.digits, string.punctuation] |
| 40 | + |
| 41 | + # Generate string using ascii,digit and punctuation |
| 42 | + if args.ascii and args.digit and args.punctuation: |
| 43 | + # Call the function |
| 44 | + PasswordGenerator(str(combine[0] + combine[1] + combine[2]), lenPass) |
| 45 | + |
| 46 | + # Generate string using ascii and digits |
| 47 | + elif args.ascii and args.digit: |
| 48 | + # Call the function |
| 49 | + PasswordGenerator(str(combine[0] + combine[1]), lenPass) |
| 50 | + |
| 51 | + # Generate string using ascii and punctuation |
| 52 | + elif args.ascii and args.punctuation: |
| 53 | + # Call the function |
| 54 | + PasswordGenerator(str(combine[0] + combine[2]), lenPass) |
| 55 | + |
| 56 | + # Generate string using digit and punctuation |
| 57 | + elif args.digit and args.punctuation: |
| 58 | + # Call the function |
| 59 | + PasswordGenerator(str(combine[1] + combine[2]), lenPass) |
| 60 | + |
| 61 | + # Generate string using ascii |
| 62 | + elif args.ascii: |
| 63 | + # Call the function |
| 64 | + PasswordGenerator(str(combine[0]), lenPass) |
| 65 | + |
| 66 | + # Generate string using digit |
| 67 | + elif args.digit: |
| 68 | + # Call the function |
| 69 | + PasswordGenerator(str(combine[1]), lenPass) |
| 70 | + |
| 71 | + # Generate string using punctuation |
| 72 | + elif args.punctuation: |
| 73 | + # Call the function |
| 74 | + PasswordGenerator(str(combine[2]), lenPass) |
| 75 | + |
| 76 | + else: |
| 77 | + # Call the function |
| 78 | + PasswordGenerator(str(combine[0] + combine[1] + combine[2]), lenPass) |
| 79 | + |
| 80 | + else: |
| 81 | + print("Enter A Positive Integer") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
0 commit comments