pwgen.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/python
  2. """generate word compound passwords
  3. usage: pwgen [-c] [len default=20]
  4. -c use conjunction
  5. """
  6. import random, sys
  7. target = 20 # default pw len
  8. use_conjunction = False
  9. n = 1
  10. if len(sys.argv)>1 and sys.argv[1]=="-c":
  11. use_conjunction = True
  12. n = 2
  13. if len(sys.argv)>n:
  14. target_arg = sys.argv[n] # if a len is spec'd on the cmd line
  15. try:
  16. target_arg = int(target_arg)
  17. except:
  18. print "sorry, you need to give me a number"
  19. sys.exit()
  20. if target_arg<12 or target_arg>40:
  21. print "sorry, can't do that length"
  22. sys.exit()
  23. target = target_arg
  24. f = open("/usr/share/dict/words")
  25. d = f.readlines()
  26. f.close()
  27. num = len(d)
  28. w=['','','']
  29. for i in range(3):
  30. w[i] = d[random.randint(0,num-1)][:-1] # toss out trailing \n
  31. while len(w[i]) > 10:
  32. w[i] = d[random.randint(0,num-1)][:-1] # toss out long words
  33. whole = len(w[0])+len(w[1])
  34. punc = " !@#$%^&*()-_=+[{]}\|;:/?.>,<`~"
  35. this_punc = punc[int(random.random() * len(punc))]
  36. conjunctions = [" and ", " and a ", " and the "," or ", " or the ", " or a ",
  37. " with a ", " with ", " with the ", " by a ", " by the ",
  38. " on a ", " on the "," on ", " in a ", " in the ", " in ",
  39. " for ", " for a ", " for the "]
  40. if use_conjunction == False:
  41. if whole >= target-6: # if 2 words is enough
  42. r = target-whole
  43. pw = w[0] + this_punc*r + w[1]
  44. else: # otherwise use 3
  45. whole = whole + len(w[2])
  46. r = target-whole
  47. pw = w[0] + this_punc*int(r/2) + w[1] + this_punc*int(r/2) + w[2]
  48. else:
  49. pw = w[0] + conjunctions[random.randint(0,len(conjunctions)-1)] + w[1]
  50. print "pass phrase: "+pw