Advertisement
Guest User

Password Generator

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Author: Covey
  4.  
  5. import itertools
  6. import argparse
  7. import sys
  8. import re
  9.  
  10. # Variables to get data from command line arguments
  11. filename = 'data.txt'   # Default value
  12. count = 3;              # Default value
  13. substitute = False      # Default value
  14.  
  15. # Reads data from file into list. Throws exception if something fails
  16. try:
  17.     global datalist
  18.     datalist = open(filename,'r').readlines()
  19.     datalist = [i.strip() for i in datalist] + [i.lower().strip() for i in datalist]
  20. except:
  21.     print('\nNo datafile found! Exiting...')
  22.     sys.exit(1)
  23.  
  24. # Adds optional arguments to the program
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument('-c', '--count', help='Number of words to combine the passwordlist into', type=int)
  27. parser.add_argument('-s', '--substitute', help='Substitute known letter/integer combinations and add to passwordlist', action='store_true')
  28. parser.add_argument('-f', '--file', help='Specify path to datafile')
  29. parser.add_argument('-q','--quick', help='Setting this will write each password directly to the output file without any formating or checks. (Strange things may occur in output file)', action='store_true')
  30.  
  31. # This function gets called if the '-q | --quick' argument is set. It will combine all the words contained
  32. # in the datafile to a maximum of 'count' words, and write each password directly to the output file without
  33. # any formatting or checks. This may lead to strange output and duplicate listings.
  34. def generateQuick():
  35.     try:
  36.         file = open(filename, 'w')
  37.         [file.write(''.join(j)) for i in range(1, count + 1) for j in itertools.permutations(datalist, i)]
  38.         file.close()
  39.         print('Passwords generated and written to file!')
  40.     except:
  41.         print('\nFailed to write to file! Exiting...')
  42.         sys.exit(1)
  43.  
  44. # This function combines all the words contained in datafile into a passwordlist with all possible
  45. # combinations, no repeated elements, and a maximum length of 'count' words.
  46. def generateNormal():
  47.     global pwlist
  48.     pwlist = [''.join(j) for i in range(1, count + 1) for j in itertools.permutations(datalist, i)]
  49.     print('Passwords generated!')
  50.  
  51. # If argument set, replacing letters with look-alike integers.
  52. # NB! Using regular expressions like this may produce duplicate values
  53. def substitute():
  54.     global pwlist
  55.     # Substitutes 'i' and 'I' with the number 1
  56.     pwlist += [re.sub('(i)|(I)', '1', i) for i in pwlist]
  57.     # Substitutes 'a' and 'A' with the number 4
  58.     pwlist += [re.sub('(a)|(A)', '4', i) for i in pwlist]
  59.     # Substitutes 'o' and 'O' with the number 0
  60.     pwlist += [re.sub('(o)|(O)', '0', i) for i in pwlist]
  61.     # Substitutes 'e' and 'E' with the number 3
  62.     pwlist += [re.sub('(e)|(E)', '3', i) for i in pwlist]
  63.     # Substitutes 't' and 'T' with the number 7
  64.     pwlist += [re.sub('(t)|(T)', '7', i) for i in pwlist]
  65.     # Substitutes 's' and 'S' with the number 5
  66.     pwlist += [re.sub('(s)|(S)', '5', i) for i in pwlist]
  67.     # Substitutes 's' and 'S' with the symbol $
  68.     pwlist += [re.sub('(s)|(S)', '$', i) for i in pwlist]
  69.     # Substitutes 'l' and 'L' with the symbol £
  70.     pwlist += [re.sub('(l)|(L)', '£', i) for i in pwlist]
  71.     # Substitutes 'a' and 'A' with the symbol @
  72.     pwlist += [re.sub('(a)|(A)', '@', i) for i in pwlist]
  73.     # Substitutes 'b' with the number 6
  74.     pwlist += [re.sub('b', '6', i) for i in pwlist]
  75.  
  76. def formatPasswords():
  77.     global pwlist
  78.     # Removes duplicates present in the list
  79.     pwlist = list(set(pwlist))
  80.  
  81. def writeToFile():
  82.     global pwlist
  83.     try:
  84.         # Generates the file containing the passwords, over-writing old file if present
  85.         file = open('password_list.txt', 'w')
  86.         [file.write(i + "\n") for i in pwlist]
  87.         file.close()
  88.         print('Passwords written to file!')
  89.     except:
  90.         print('Failed to write into file! Exiting...')
  91.         sys.exit(1)
  92.  
  93. # Check to see if any arguments are present at runtime
  94. args = parser.parse_args()
  95. if args.count:
  96.     # Set the number of words to be combined from the datafile
  97.     count = args.c
  98. if args.substitute:
  99.     # Turns on substitution of look-alike chars
  100.     substitute = True
  101. if args.file:
  102.     # Uses custom path to datafile
  103.     filename = args.f
  104. if args.quick:
  105.     generateQuick()
  106.     sys.exit(0)
  107. else:
  108.     generateNormal()
  109.     if substitute:
  110.         substitute()
  111.     formatPasswords()
  112.     writeToFile()
  113.     sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement