-
-
Save parhamfh/8571404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding utf8 | |
import subprocess | |
import sys | |
import uuid | |
import time | |
from hashlib import sha1 | |
from datetime import datetime as dt | |
# print sys.argv | |
### Test cases | |
test_body = """tree {0} | |
parent {1} | |
author {2} | |
committer {2} | |
Mined a Gitcoin! | |
nonce {3}""" | |
test_commit1 = { | |
'tree' : '4616b7a489522817943bdc201d6120af86f5020c', | |
'parent' : '000000c4f2d8c4a2a407d218018be48f55197b34', | |
'author' : 'Stripe CTF <ctf@stripe.com> 1390435282 +0000', | |
'nonce' : '034ddb08', | |
'sha1' : '0000009e3202cdc50b4baf1478292cd5fed12f0a' | |
} | |
test_commit2 = { | |
'tree' : '5fcb785f4a055f6066e753984b30c1923a8c07bc', | |
'parent' : '000000a2c85627166528f72f8f5d88108f9a04f1', | |
'author' : 'Stripe CTF <ctf@stripe.com> 1390435321 +0000', | |
'nonce' : '05047500', | |
'sha1' : '0000009ad10164b52652fb07fab4bb497fadec95' | |
} | |
### Constants | |
difficulty = "000001" | |
cspec = 'lvl1-t0uah66c@stripe-ctf.com:level1' | |
user = 'user-w7shf0ia' | |
commit_body = """tree {0} | |
parent {1} | |
author CTF user <me@example.com> {2} +0000 | |
committer CTF user <me@example.com> {2} +0000 | |
Give me a Gitcoin | |
{3}""" | |
### Ledger | |
### Hashing | |
def add_git_header(message, t = None): | |
if t == None: | |
t = 'commit' | |
return "{0} {1}\0{2}".format(t,len(message),message) | |
def githash(message, typ = None): | |
hash_message = add_git_header(message, t=typ) | |
return sha1(hash_message).hexdigest() | |
def format_body(body, test_dict=None): | |
if test_dict: | |
return body.format(test_dict['tree'], | |
test_dict['parent'], | |
test_dict['author'], | |
test_dict['nonce']) | |
tree = subprocess.check_output(['git','write-tree']).split()[0] | |
parent = subprocess.check_output(['git','rev-parse', 'HEAD']).split()[0] | |
timestamp = dt.now().strftime('%a %b %d %X CET %Y') | |
return body.format(tree, parent, timestamp, 100*str(uuid.uuid1())) | |
def try_commit_body(): | |
return githash(format_body(commit_body)) | |
### GIT | |
def gitpull(): | |
subprocess.call(['git','pull','origin']) | |
def gitpush(): | |
subprocess.call(['git', 'push','origin','master']) | |
def gitreset_to_hash(commit_hash): | |
subprocess.call(['git','reset','--hard',commit_hash]) | |
def git_prepare_index(): | |
subprocess.call(['git', 'fetch','origin','master']) | |
subprocess.call(['git','reset','--hard','origin/master']) | |
test = True | |
if test: | |
assert test_commit1['sha1'] == githash(format_body(test_body, test_dict=test_commit1)) | |
assert test_commit2['sha1'] == githash(format_body(test_body, test_dict=test_commit2)) | |
success = False | |
counter = 0 | |
while not success: | |
git_prepare_index() | |
start = time.time() | |
counter = 0 | |
while True: | |
commit_hash = try_commit_body() | |
counter += 1 | |
print commit_hash | |
if commit_hash < difficulty: | |
stop = time.time() | |
gitreset_to_hash(commit_hash) | |
try: | |
gitpush() | |
print "success :))" | |
except: | |
print "starting over, didnt make it" | |
break | |
success = True | |
break | |
print "took {0} to find gitcoin, generated {1} hashes".format(stop-start, counter) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment