1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-12-26 00:18:11 +00:00
bitcoinbook/code/hash_example.py

19 lines
449 B
Python
Raw Normal View History

2013-08-18 22:35:38 +00:00
# example of iterating a nonce in a hashing algorithm's input
2017-09-06 05:23:23 +00:00
from __future__ import print_function
2013-08-18 22:35:38 +00:00
import hashlib
2017-09-06 05:23:23 +00:00
2013-08-18 22:35:38 +00:00
text = "I am Satoshi Nakamoto"
2014-09-25 16:02:07 +00:00
# iterate nonce from 0 to 19
2017-09-06 05:23:23 +00:00
for nonce in range(20):
2014-09-25 16:02:07 +00:00
# add the nonce to the end of the text
2017-09-06 05:23:23 +00:00
input = text + str(nonce)
2014-09-25 16:02:07 +00:00
# calculate the SHA-256 hash of the input (text+nonce)
2017-09-06 05:23:23 +00:00
hash = hashlib.sha256(input).hexdigest()
2014-09-25 16:02:07 +00:00
# show the input and hash result
2017-08-24 16:22:28 +00:00
print(input, '=>', hash)