Skip to content

Instantly share code, notes, and snippets.

@ninjatrench
Created February 8, 2021 21:40
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 ninjatrench/7338b30e1a54147086a0c73e59cfa5a7 to your computer and use it in GitHub Desktop.
Save ninjatrench/7338b30e1a54147086a0c73e59cfa5a7 to your computer and use it in GitHub Desktop.
Crypto - Proof of Work sample code in Python
from hashlib import sha1
import sys
def pow(challenge = "Bitcoin", numZeros = 2):
"""
Calculating the token that matches the proof of work.
Sample values by default: "Bitcoin" and 2 consecutive zeroes.
"""
# Building the sequence of zeroes
strZeros = '0' * numZeros
index = 0
while True:
value = sha1( challenge + str(index) ).hexdigest()
print value
if value[:numZeros] == strZeros:
print
print "Match found: " + challenge + str(index)
print "\tChallenge --> " + challenge
print "\tToken --> " + challenge + str(index)
print "\tHash --> " + value
break
else:
index+=1
def show_usage():
print("Usage: hashcash_sample.py <CHALLENGE> <NUMBER_ZEROS>")
if __name__ == "__main__":
if len(sys.argv) == 3:
pow(sys.argv[1], int(sys.argv[2]))
else:
print("Hey! Two parameters are required.")
show_usage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment