Skip to content

Commit 908ac19

Browse files
committed
update
1 parent 1f919f3 commit 908ac19

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/13-blockchain.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import hashlib
22
import struct
33

4+
import pytest
5+
46
# the hash generator: sha256
7+
# reference hash, create via sha256hmac -u :
8+
refString = u"ottos mops trotzt\n"
9+
refHash = "bc607eac3d2c0949cfa2fd01a32788eec5be5b1cabb4e108326f205099349cc1"
10+
#
511

6-
# target difficulty. Add more "0" to make it harder
12+
# target difficulty. Add more "0" to make it harder
713
difficulty = "000"
814
# max number of tries to solve the difficulty
915
mx = 10000000
1016

11-
# hash function with variation of nonce to solve difficulty
17+
# hash function with variation of nonce to solve difficulty
1218
def calc(s):
1319
for nonce in range(mx):
1420
# add a number between 1 .. 4000000000 as 32 bit string value
@@ -42,9 +48,18 @@ def calc(s):
4248
h,n = calc(data)
4349
print("Hash: ", h)
4450
print("Nonce: ", n,"\n")
45-
51+
4652
# Note the output values and verify that a small change in an early step
4753
# change the output of all subsequent steps
4854
# this feature is used to verify the operations in a block chain
4955
# you can test this in the python shell
5056

57+
########## test #############
58+
def test_hash():
59+
hg = hashlib.sha256()
60+
print("String:",refString)
61+
s = bytes(refString,"utf-8")
62+
hg.update(s)
63+
h = hg.hexdigest()
64+
assert refHash == h
65+

0 commit comments

Comments
 (0)