gpgpparticipants: add fillinsha256sum.py
authorStefan Huber <shuber@sthu.org>
Tue, 15 Oct 2013 21:36:30 +0000 (23:36 +0200)
committerStefan Huber <shuber@sthu.org>
Tue, 15 Oct 2013 21:36:30 +0000 (23:36 +0200)
gpgparticipants/fillinsha256sum.py [new file with mode: 0755]

diff --git a/gpgparticipants/fillinsha256sum.py b/gpgparticipants/fillinsha256sum.py
new file mode 100755 (executable)
index 0000000..3986d57
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/env python2
+
+__author__ = "Stefan Huber <shuber@sthu.org>"
+
+import sys
+import hashlib
+import getopt
+
+
+def insertspaces(s):
+    """Inserts a space after every 4-th character, and three spaces after every
+    8-th character of string s."""
+
+    def inpacks(s, n):
+        while len(s) > 0:
+            yield s[0:n]
+            s = s[n:]
+
+    out = "   ".join([ " ".join(inpacks(octp, 4)) for octp in inpacks(s, 8)])
+    return out
+
+
+def range_hex(length):
+    """Give all hex-strings from 00...0 until ff...f of given length."""
+
+    if length == 0:
+        yield ""
+
+    elif length == 1:
+        for c in "0123456789abcdef":
+            yield c
+    elif length > 1:
+        for prefix in range_hex(length-1):
+            for postfix in range_hex(1):
+                yield prefix + postfix
+
+
+def usage():
+    """Print --help text"""
+
+    print("""Usage:
+  {0} <emptylist> <filledlist>
+  {0} --help
+  {0} -h
+
+Takes a file produced by gpgparticipants as <emptylist> and trys to fill in
+some digits into the SHA256 field such that the resulting list actually has
+a SHA256 checksum that starts with those digits. Whenever a match is found a
+file with the digits filled in is written to `<filledlist>.DIGITS`.
+
+OPTIONS:
+
+  --fastforward        If a match is found of given length and --fastforward
+                       is given then the programm immediately jumps to the next
+                       length.
+  --min-length NUM     Start looking for hex strings of given length
+
+""".format(sys.argv[0]))
+
+if __name__ == "__main__":
+
+    fastforward = False
+    minlength = 1
+
+    optlist, args = getopt.getopt(sys.argv[1:], 'h', ['fastforward', 'min-length='])
+    for o, a in optlist:
+
+        if o in ("-h", "--help"):
+            usage()
+            exit(0)
+
+        if o in ("--fastforward"):
+            fastforward = True
+
+        if o in ("--min-length"):
+            minlength = int(a)
+
+    if len(args) < 2:
+        print >>sys.stderr, "You need to give two filenames."""
+        exit(1)
+
+    emptyfile = open(args[0]).read()
+
+    idx = emptyfile.find("SHA256 Checksum:")
+    idx = emptyfile.find("_", idx)
+
+    for l in range(minlength, 32):
+        print "Looking at length", l
+
+        for h in range_hex(l):
+
+            H = insertspaces(h.upper())
+            filledfile = emptyfile[:idx] + H + emptyfile[idx+len(H):]
+            actual = hashlib.sha256(filledfile).hexdigest()
+
+            if actual[:len(h)] == h:
+                print "Found: ", H
+                open(args[1] + "." + H, "w").write(filledfile)
+
+                if fastforward:
+                    break