Created
April 22, 2016 23:46
-
-
Save webglider/ca63408b056ba229596a96a52936ae56 to your computer and use it in GitHub Desktop.
Proof of Work based throttling
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
# 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