Skip to content

Instantly share code, notes, and snippets.

@webglider
Created April 22, 2016 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webglider/ca63408b056ba229596a96a52936ae56 to your computer and use it in GitHub Desktop.
Save webglider/ca63408b056ba229596a96a52936ae56 to your computer and use it in GitHub Desktop.
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