Skip to content

Commit af7a838

Browse files
committed
picnic work, new password idea
1 parent 9ddbc92 commit af7a838

File tree

4 files changed

+62
-44
lines changed

4 files changed

+62
-44
lines changed

bin/new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def main():
191191
192192
print('str_arg = "{{}}"'.format(str_arg))
193193
print('int_arg = "{{}}"'.format(int_arg))
194-
print('file_arg = "{{}}"'.format(file_arg.name))
194+
print('file_arg = "{{}}"'.format(file_arg.name if file_arg else ''))
195195
print('flag_arg = "{{}}"'.format(flag_arg))
196196
print('positional = "{{}}"'.format(pos_arg))
197197

password/harvest.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import argparse
99
import os
10-
import sys
1110
import spacy
11+
from collections import Counter
1212

1313

1414
# --------------------------------------------------
@@ -33,9 +33,11 @@ def get_args():
3333
default='words')
3434

3535
parser.add_argument('-l',
36-
'--lower',
37-
help='Lowercase output',
38-
action='store_true')
36+
'--limit',
37+
metavar='int',
38+
type=int,
39+
default=0,
40+
help='Limit to this many')
3941

4042
return parser.parse_args()
4143

@@ -53,34 +55,34 @@ def main():
5355
# Load English tokenizer, tagger, parser, NER and word vectors
5456
nlp = spacy.load("en_core_web_sm")
5557

56-
nouns, adjs, verbs = set(), set(), set()
58+
nouns, adjs, verbs = Counter(), Counter(), Counter()
5759
for fh in args.file:
5860
doc = nlp(fh.read())
5961

6062
for token in doc:
61-
pos, word = token.pos_, token.lemma_
62-
63-
if args.lower:
64-
word = word.lower()
63+
pos, word = token.pos_, token.lemma_.lower()
6564

6665
if pos == 'NOUN':
67-
nouns.add(word)
66+
nouns.update([word])
6867
elif pos == 'VERB':
69-
verbs.add(word)
68+
verbs.update([word])
7069
elif pos == 'ADJ':
71-
adjs.add(word)
70+
adjs.update([word])
71+
72+
def limiter(words):
73+
return sorted(list(map(lambda t: t[0], words.most_common(
74+
args.limit)))) if args.limit else sorted(words)
7275

7376
def write(words, name):
7477
if words:
7578
out_fh = open(os.path.join(out_dir, name), 'wt')
76-
out_fh.write('\n'.join(words))
79+
out_fh.write('\n'.join(limiter(words)) + '\n')
7780

7881
write(verbs, 'verbs.txt')
7982
write(nouns, 'nouns.txt')
8083
write(adjs, 'adjs.txt')
8184

82-
total = sum(map(len, [verbs, adjs, nouns]))
83-
print(f'Done, wrote {total} to "{out_dir}".')
85+
print(f'Done, see output in "{out_dir}".')
8486

8587

8688
# --------------------------------------------------

password/password.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,30 @@ def get_args():
1919
parser.add_argument('file',
2020
metavar='FILE',
2121
type=argparse.FileType('r'),
22-
nargs='+',
23-
help='Input file(s)')
22+
nargs='*',
23+
help='Input file(s)',
24+
default=[open('/usr/share/dict/words')])
2425

2526
parser.add_argument('-n',
2627
'--num',
2728
metavar='INT',
2829
type=int,
29-
default=20,
30+
default=3,
3031
help='Number of passwords to generate')
3132

32-
parser.add_argument('-m',
33-
'--min_length',
33+
parser.add_argument('-w',
34+
'--num_words',
3435
metavar='INT',
3536
type=int,
36-
default=8,
37-
help='Minimum password length')
37+
default=4,
38+
help='Number of words to use for password')
3839

39-
parser.add_argument('-x',
40-
'--max_length',
40+
parser.add_argument('-m',
41+
'--min_word_len',
4142
metavar='INT',
4243
type=int,
43-
default=20,
44-
help='Maximum password length')
44+
default=3,
45+
help='Minimum word length')
4546

4647
parser.add_argument('-s',
4748
'--seed',
@@ -65,32 +66,50 @@ def main():
6566
random.seed(args.seed)
6667
words = set()
6768

69+
clean = lambda word: re.sub('[^a-zA-Z]', '', word)
6870
for fh in args.file:
69-
for word in fh.read().split():
70-
words.add(re.sub('[^a-zA-Z]', '', word))
71+
for word in filter(lambda w: len(w) > args.min_word_len,
72+
map(clean,
73+
fh.read().lower().split())):
74+
words.add(word.title())
7175

72-
words = list(words)
76+
words = sorted(list(words))
7377

74-
for i in range(args.num):
75-
password = ''
76-
while len(password) < args.max_length:
77-
password += random.choice(words)
78+
for _ in range(args.num):
79+
password = ''.join(random.sample(words, args.num_words))
80+
print(l33t(password) if args.l33t else password)
7881

79-
if args.l33t:
80-
password = l33t(password)
81-
82-
print(password)
8382

8483
# --------------------------------------------------
8584
def l33t(text):
8685
"""l33t"""
8786

88-
xform = {'a': '@', 'A': '4', 'o': '0', 'O': '0'}
87+
text = ransom(text)
88+
xform = {
89+
'a': '@',
90+
'A': '4',
91+
'o': '0',
92+
'O': '0',
93+
't': '+',
94+
'e': '3',
95+
'E': '3',
96+
'I': '1',
97+
'S': '5'
98+
}
8999
for x, y in xform.items():
90100
text = text.replace(x, y)
91101

92102
return text
93103

104+
105+
# --------------------------------------------------
106+
def ransom(text):
107+
"""Randomly choose an upper or lowercase letter to return"""
108+
109+
return ''.join(
110+
map(lambda c: c.upper() if random.choice([0, 1]) else c.lower(), text))
111+
112+
94113
# --------------------------------------------------
95114
if __name__ == '__main__':
96115
main()

picnic/Makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
.PHONY: pdf test
2-
3-
pdf:
4-
pandoc README.md -o README.pdf
1+
.PHONY: test
52

63
test:
7-
pytest -v test.py
4+
pytest -xv test.py

0 commit comments

Comments
 (0)