Skip to content

Instantly share code, notes, and snippets.

@sundhaug92
Created September 22, 2017 14:33
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 sundhaug92/9f5a9f926abfa539956c7454c68a5f88 to your computer and use it in GitHub Desktop.
Save sundhaug92/9f5a9f926abfa539956c7454c68a5f88 to your computer and use it in GitHub Desktop.
Proof of work implementation for Python3
from hashlib import sha512
def count_noughts(s):
n=0
for c in s:
if c == '0':n+=1
else:
return n
return 0
def work(prefix,noughts,search_depth=10):
if search_depth < 1:
return None
for _ in range(256):
h=sha512((prefix+chr(_)).encode()).hexdigest()
if noughts==count_noughts(h):return chr(_)
for d in range(search_depth-1):
for _ in range(256):
r=work(prefix+chr(_),noughts,d)
if r is not None:return chr(_)+r
s=work('',3)
print('{}:{}'.format(sha512(s.encode()).hexdigest(),s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment