File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import hashlib
22import 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
713difficulty = "000"
814# max number of tries to solve the difficulty
915mx = 10000000
1016
11- # hash function with variation of nonce to solve difficulty
17+ # hash function with variation of nonce to solve difficulty
1218def 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+
You can’t perform that action at this time.
0 commit comments