Skip to content

Instantly share code, notes, and snippets.

@erickclasen
Created May 6, 2022 02:19
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 erickclasen/9c1eb09791217ec1502d8f38c7c46731 to your computer and use it in GitHub Desktop.
Save erickclasen/9c1eb09791217ec1502d8f38c7c46731 to your computer and use it in GitHub Desktop.
Demonstrates SHA256 hashing. Shows a basic proof of work example as well.
'''
Original seed idea from
https://www.pythoncentral.io/hashing-strings-with-python/
'''
import hashlib
from datetime import datetime
mystring = input('Enter String to hash: ')
# Assumes the default UTF-8
hash_object = hashlib.sha256(mystring.encode())
print(hash_object.hexdigest())
for DIF in range(1,32):
for n in range(1,1000000000000):
x = mystring + str(n)
hash_object = hashlib.sha256(x.encode())
d = hash_object.hexdigest()
#print(x,d)
tally = 0
for z in range(0,DIF):
if d[z] == '0':
tally += 1
if tally == DIF:
break
timestamp = str(datetime.now())
print(timestamp,DIF,x,d)
@erickclasen
Copy link
Author

Screenshot_2022-05-05_22-18-31
Example output, shows a hash of the word "hello"
Then it applies a nonce appended to hello to get hashes with zeros at the beginning, simulating proof of work for an example.
Shows timestamps to show how long it takes to generate the hashes as the difficulty rises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment