|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +import zlib |
| 8 | + |
| 9 | +lock = threading.Lock() |
| 10 | +terminate = False; |
| 11 | + |
| 12 | +def hack(git_object, counter_start, counter_increment, goal, destination_clear, destination_compressed): |
| 13 | + global lock |
| 14 | + global terminate |
| 15 | + print(f"hack(..., {counter_start}, {counter_increment}, {goal})", file=sys.stderr); |
| 16 | + header, content = git_object.split(b'\x00', 1) |
| 17 | + hack_str = content.decode("utf-8") |
| 18 | + counter = counter_start; |
| 19 | + while True: |
| 20 | + with lock: |
| 21 | + if terminate: |
| 22 | + return |
| 23 | + commit_content = hack_str + "\n" + str(counter) + "\n" |
| 24 | + commit_content_bytes = commit_content.encode("utf-8") |
| 25 | + commit_content_bytes_len = len(commit_content_bytes) |
| 26 | + header = f"commit {commit_content_bytes_len}\x00" |
| 27 | + header_content = header + commit_content |
| 28 | + to_be_hashed = header_content.encode("utf-8") |
| 29 | + s = hashlib.sha1(to_be_hashed).hexdigest() |
| 30 | + if s.startswith(goal): |
| 31 | + with lock: |
| 32 | + terminate = True |
| 33 | + print(f"SHA1: {s}", file=sys.stderr); |
| 34 | + print(f"Write to: {destination_clear} (plain)", file=sys.stderr); |
| 35 | + with open(destination_clear, "wb") as f: |
| 36 | + f.write(to_be_hashed) |
| 37 | + print(f"Write to: {destination_compressed} (compressed)", file=sys.stderr); |
| 38 | + with open(destination_compressed, "wb") as f: |
| 39 | + bb = zlib.compress(to_be_hashed) |
| 40 | + f.write(bb) |
| 41 | + return |
| 42 | + counter = counter + counter_increment |
| 43 | + |
| 44 | +filename = sys.argv[1] |
| 45 | +compressed_contents = open(filename, 'rb').read() |
| 46 | +decompressed_contents = zlib.decompress(compressed_contents) |
| 47 | +#print(f"------\n{s}\n------)", file=sys.stderr); |
| 48 | + |
| 49 | +cpus = len(os.sched_getaffinity(0)) |
| 50 | +print(f"\rCPUs detected: {cpus}", file=sys.stderr) |
| 51 | + |
| 52 | +threads = [threading.Thread(target=hack, args=(decompressed_contents,i,cpus,sys.argv[2], sys.argv[3], sys.argv[4])) for i in range(0, cpus)] |
| 53 | +for thread in threads: |
| 54 | + thread.start() |
| 55 | + |
| 56 | +for thread in threads: |
| 57 | + thread.join() |
0 commit comments