-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path13-blockchain.py
More file actions
68 lines (59 loc) · 1.87 KB
/
13-blockchain.py
File metadata and controls
68 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import hashlib
import struct
import pytest
# the hash generator: sha256
# reference hash, create via sha256hmac -u :
refString = u"ottos mops trotzt\n"
refHash = "bc607eac3d2c0949cfa2fd01a32788eec5be5b1cabb4e108326f205099349cc1"
#
# target difficulty. Add more "0" to make it harder
difficulty = "000"
# max number of tries to solve the difficulty
mx = 10000000
# hash function with variation of nonce to solve difficulty
def calc(s):
for nonce in range(mx):
# add a number between 1 .. 4000000000 as 32 bit string value
# to the input data
nc = struct.pack("<LL", 32, nonce)
#print(nc)
# binary hash
# create a new hash generator every time to start clean
hg = hashlib.sha256()
hg.update(bytes(s,"utf-8") + nc)
# hash as hex value (text)
hash = hg.hexdigest()
#print(hash)
if hash.startswith(difficulty):
break
# next nonce value
nonce += 1
# check nonce
if nonce == mx:
print("... too hard to compute")
return "0", 0
return hash, nonce
s = u"This is an example for bitcoin style proof-of-work"
for i in range (4):
data = str(i + 1000)
if i > 0:
data += "+" + h
print("Encoding: ", data)
h,n = calc(data)
print("Hash: ", h)
print("Nonce: ", n,"\n")
# Note the output values and verify that a small change in an early step
# change the output of all subsequent steps
# this feature is used to verify the operations in a block chain
# you can test this in the python shell
########## test #############
# specific pytest can be forced with
# py.test-version ...
# e.g. py.test-3.6 13-blockchain.py
def test_hash():
hg = hashlib.sha256()
print("String:",refString)
s = bytes(refString,"utf-8")
hg.update(s)
h = hg.hexdigest()
assert refHash == h