-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptgrep.py
More file actions
executable file
·70 lines (61 loc) · 2.12 KB
/
cryptgrep.py
File metadata and controls
executable file
·70 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
import gflags
import sys
FLAGS = gflags.FLAGS
def matches(word, inject, include, codeword):
if len(word) != len(codeword):
return False
mapping = {}
values = set()
for c, d in zip(word, codeword):
if d == ".":
pass
elif d.isupper():
if c != d:
return False
else:
if c not in include:
return False
if d in mapping:
if mapping[d] != c:
return False
else:
mapping[d] = c
if inject:
if c in values:
return False
values.add(c)
return True
def search_file(f, inject, include, codeword):
for line in f:
word = line.strip()
if matches(word.upper(), inject, include, codeword):
print word
def main():
gflags.DEFINE_bool("inject", True, "Require an injective mapping")
gflags.DEFINE_string("include", "", "String of characters allowed in range. If not given, defaults to entire alphabet if inject is False, otherwise defaults to alphabet minus characters that already exist.")
gflags.DEFINE_string("dict", "", "filename of the dictionary")
try:
argv = FLAGS(sys.argv)
if len(argv) != 2:
raise Exception("Bad format.")
codeword = argv[1]
if (not codeword) or any(c not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." for c in codeword):
raise Exception("Bad codeword.")
except Exception as e:
print '%s\nUsage: %s [options] codeword\n%s' % (e, sys.argv[0], FLAGS)
sys.exit(1)
inject = FLAGS.inject
if FLAGS.include:
include = FLAGS.include.upper()
elif inject:
include = [c for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if c not in codeword]
else:
include = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if FLAGS.dict:
with open(FLAGS.dict, "r") as f:
search_file(f, inject, include, codeword)
else:
search_file(sys.stdin, inject, include, codeword)
if __name__ == '__main__':
main()