Some usage text changes
[pgp-tools.git] / gpgparticipants / gpgparticipants-prefill
1 #!/usr/bin/env python2
2 """Fill in the first digits of the hash in a form created by gpgparticipants."""
3
4 __version__ = "1.0"
5
6 __author__ = "Stefan Huber"
7 __email__ = "shuber@sthu.org"
8 __copyright__ = "Copyright 2013, Stefan Huber"
9
10 __license__ = "MIT"
11
12 # Permission is hereby granted, free of charge, to any person
13 # obtaining a copy of this software and associated documentation
14 # files (the "Software"), to deal in the Software without
15 # restriction, including without limitation the rights to use,
16 # copy, modify, merge, publish, distribute, sublicense, and/or sell
17 # copies of the Software, and to permit persons to whom the
18 # Software is furnished to do so, subject to the following
19 # conditions:
20 #
21 # The above copyright notice and this permission notice shall be
22 # included in all copies or substantial portions of the Software.
23 #
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 # OTHER DEALINGS IN THE SOFTWARE.
32
33
34 import sys
35 import hashlib
36 import getopt
37
38
39 def insertspaces(s):
40 """Inserts a space after every 4-th character, and three spaces after every
41 8-th character of string s."""
42
43 def inpacks(s, n):
44 while len(s) > 0:
45 yield s[0:n]
46 s = s[n:]
47
48 out = " ".join([ " ".join(inpacks(octp, 4)) for octp in inpacks(s, 8)])
49 return out
50
51
52 def range_hex(length):
53 """Give all hex-strings from 00...0 until ff...f of given length."""
54
55 if length == 0:
56 yield ""
57
58 elif length == 1:
59 for c in "0123456789abcdef":
60 yield c
61 elif length > 1:
62 for prefix in range_hex(length-1):
63 for postfix in range_hex(1):
64 yield prefix + postfix
65
66
67 def usage():
68 """Print --help text"""
69
70 print("""Usage:
71 {0} <emptylist> <filledlist>
72 {0} --help
73 {0} -h
74
75 Takes a file produced by gpgparticipants as <emptylist> and trys to fill in
76 some digits into the SHA256 field such that the resulting list actually has
77 a SHA256 checksum that starts with those digits. Whenever a match is found a
78 file with the digits filled in is written to `<filledlist>.DIGITS`.
79
80 OPTIONS:
81
82 --fastforward If a match is found of given length and --fastforward
83 is given then the program immediately jumps to the next
84 length.
85 --min-length NUM Start search with given length
86
87 """.format(sys.argv[0]))
88
89
90 if __name__ == "__main__":
91
92 fastforward = False
93 minlength = 1
94
95 optlist, args = getopt.getopt(sys.argv[1:], 'h', ['fastforward', 'min-length='])
96 for o, a in optlist:
97
98 if o in ("-h", "--help"):
99 usage()
100 exit(0)
101
102 if o in ("--fastforward"):
103 fastforward = True
104
105 if o in ("--min-length"):
106 minlength = int(a)
107
108 if len(args) < 2:
109 print >>sys.stderr, "You need to give two filenames."""
110 exit(1)
111
112 emptyfile = open(args[0]).read()
113
114 idx = emptyfile.find("SHA256 Checksum:")
115 idx = emptyfile.find("_", idx)
116
117 for l in range(minlength, 32):
118 print "Looking at length", l
119
120 for h in range_hex(l):
121
122 H = insertspaces(h.upper())
123 filledfile = emptyfile[:idx] + H + emptyfile[idx+len(H):]
124 actual = hashlib.sha256(filledfile).hexdigest()
125
126 if actual[:len(h)] == h:
127 print "Found: ", H
128 open(args[1] + "." + h, "w").write(filledfile)
129
130 if fastforward:
131 break