64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
#!/usr/bin/python
|
|||
|
|
|
||
|
|
"""generate word compound passwords
|
||
|
|
|
||
|
|
usage: pwgen [-c] [len default=20]
|
||
|
|
-c use conjunction
|
||
|
|
"""
|
||
|
|
import random, sys
|
||
|
|
|
||
|
|
target = 20 # default pw len
|
||
|
|
use_conjunction = False
|
||
|
|
|
||
|
|
n = 1
|
||
|
|
if len(sys.argv)>1 and sys.argv[1]=="-c":
|
||
|
|
use_conjunction = True
|
||
|
|
n = 2
|
||
|
|
if len(sys.argv)>n:
|
||
|
|
target_arg = sys.argv[n] # if a len is spec'd on the cmd line
|
||
|
|
try:
|
||
|
|
target_arg = int(target_arg)
|
||
|
|
except:
|
||
|
|
print "sorry, you need to give me a number"
|
||
|
|
sys.exit()
|
||
|
|
if target_arg<12 or target_arg>40:
|
||
|
|
print "sorry, can't do that length"
|
||
|
|
sys.exit()
|
||
|
|
target = target_arg
|
||
|
|
|
||
|
|
f = open("/usr/share/dict/words")
|
||
|
|
d = f.readlines()
|
||
|
|
f.close()
|
||
|
|
|
||
|
|
num = len(d)
|
||
|
|
|
||
|
|
w=['','','']
|
||
|
|
for i in range(3):
|
||
|
|
w[i] = d[random.randint(0,num-1)][:-1] # toss out trailing \n
|
||
|
|
while len(w[i]) > 10:
|
||
|
|
w[i] = d[random.randint(0,num-1)][:-1] # toss out long words
|
||
|
|
|
||
|
|
whole = len(w[0])+len(w[1])
|
||
|
|
|
||
|
|
punc = " !@#$%^&*()-_=+[{]}\|;:/?.>,<`~"
|
||
|
|
this_punc = punc[int(random.random() * len(punc))]
|
||
|
|
|
||
|
|
conjunctions = [" and ", " and a ", " and the "," or ", " or the ", " or a ",
|
||
|
|
" with a ", " with ", " with the ", " by a ", " by the ",
|
||
|
|
" on a ", " on the "," on ", " in a ", " in the ", " in ",
|
||
|
|
" for ", " for a ", " for the "]
|
||
|
|
|
||
|
|
if use_conjunction == False:
|
||
|
|
if whole >= target-6: # if 2 words is enough
|
||
|
|
r = target-whole
|
||
|
|
pw = w[0] + this_punc*r + w[1]
|
||
|
|
else: # otherwise use 3
|
||
|
|
whole = whole + len(w[2])
|
||
|
|
r = target-whole
|
||
|
|
pw = w[0] + this_punc*int(r/2) + w[1] + this_punc*int(r/2) + w[2]
|
||
|
|
else:
|
||
|
|
pw = w[0] + conjunctions[random.randint(0,len(conjunctions)-1)] + w[1]
|
||
|
|
|
||
|
|
print "pass phrase: "+pw
|
||
|
|
|