Skip to content

Instantly share code, notes, and snippets.

@webglider

webglider/pow.py

Created Apr 22, 2016
Embed
What would you like to do?
Proof of Work based throttling
# Playing around with Proof of Work
import random, binascii, md5
# random byte string generator
# k byte random string is output
def rand_bytes(k):
num = random.getrandbits(8*k)
s = hex(num)[2:]
if(s[-1]=='L'):
s = s[:-1]
return binascii.a2b_hex(s.zfill(2*k))
# Work to find a k byte string with first l bits of md5 hash zero
def work(l, k):
while True:
cand = rand_bytes(k)
m = md5.new()
m.update(cand)
h = m.digest()
h_bin = bin(int(binascii.b2a_hex(h), 16))[2:]
h_bin = h_bin.zfill(16*8)
if(h_bin[:l] == '0'*l):
print 'Found Solution'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment