1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-23 16:38:38 +00:00
bitcoinbook/code/hash_example.py

19 lines
473 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
2018-02-03 00:33:46 +00:00
input_data = text + str(nonce)
2014-09-25 16:02:07 +00:00
# calculate the SHA-256 hash of the input (text+nonce)
2018-02-03 00:33:46 +00:00
hash_data = hashlib.sha256(input_data).hexdigest()
2014-09-25 16:02:07 +00:00
# show the input and hash result
2018-02-03 00:33:46 +00:00
print(input_data, '=>', hash_data)