Skip to content

Instantly share code, notes, and snippets.

@nickodell
Forked from shirriff/mine.py
Last active August 29, 2015 14:13
Show Gist options
  • Save nickodell/ccd95f87095b45259285 to your computer and use it in GitHub Desktop.
Save nickodell/ccd95f87095b45259285 to your computer and use it in GitHub Desktop.
Enumerate hashes - thanks to Ken Shirriff
#!/usr/bin/python
import hashlib, struct
# Mine genesis block
ver = 1
prev_block = "0000000000000000000000000000000000000000000000000000000000000000"
mrkl_root = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
time_ = 1231006505
bits = 0x1d00ffff
# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3))))
target_str = target_hexstr.decode('hex')
nonce = 0
while nonce < (2**32):
header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
print nonce, hash[::-1].encode('hex')
if hash[::-1] < target_str:
print 'success'
#break
nonce += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment