Guest User

Untitled

a guest
Dec 9th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. import secrets
  4. import argparse
  5. import shutil
  6.  
  7. lower = [chr(c) for c in range(ord('a'), ord('z') + 1)]
  8. upper = [chr(c) for c in range(ord('A'), ord('Z') + 1)]
  9. number = [chr(c) for c in range(ord('0'), ord('9') + 1)]
  10. special = """!@#$%^&*()_+{}=-[];,.<>/?'""" + '"'
  11.  
  12. def parse_args():
  13. parser = argparse.ArgumentParser(description='Generate secure passwords with configurable character classes')
  14.  
  15. parser.add_argument('-a', help='include lower case letters, default is yes', action='store_true')
  16. parser.add_argument('-A', help='include upper case letters, default is yes', action='store_true')
  17. parser.add_argument('-n', help='include numbers, default is yes', action='store_true')
  18. parser.add_argument('-s', help='include special characters, default is no', action='store_true')
  19.  
  20. parser.add_argument('count', help='the number of password to generate, default is 5',
  21. type=int, default=5, nargs='?')
  22. parser.add_argument('length', help='the length of the password being generated, default is 16',
  23. type=int, default=16, nargs='?')
  24.  
  25. return parser.parse_args()
  26.  
  27. def generate_characters(args):
  28. if (not args.a and not args.A and not args.n and not args.s):
  29. return "".join(lower) + "".join(upper) + "".join(number)
  30.  
  31. chars = ""
  32.  
  33. if args.a:
  34. chars += "".join(lower)
  35.  
  36. if args.A:
  37. chars += "".join(upper)
  38.  
  39. if args.n:
  40. chars += "".join(number)
  41.  
  42. if args.s:
  43. chars += special
  44.  
  45. return chars
  46.  
  47. def generate_pw(chars, length):
  48. return "".join([secrets.choice(chars) for i in range(length)])
  49.  
  50. def pretify(pws):
  51. columns = shutil.get_terminal_size((80,20)).columns
  52.  
  53. n = int(columns / (len(pws[0]) + 1))
  54.  
  55. rows = [' '.join(pws[i:i + n]) for i in range(0, len(pws), n)]
  56.  
  57. return '\n'.join(rows)
  58.  
  59. def main():
  60. args = parse_args()
  61.  
  62. chars = generate_characters(args)
  63.  
  64. pws = [generate_pw(chars, args.length) for i in range(args.count)]
  65.  
  66. print(pretify(pws))
  67.  
  68. if __name__ == '__main__':
  69. main()
Add Comment
Please, Sign In to add comment