Created
February 8, 2021 21:40
-
-
Save ninjatrench/7338b30e1a54147086a0c73e59cfa5a7 to your computer and use it in GitHub Desktop.
Crypto - Proof of Work sample code in Python
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
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 "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