-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptohash.py
24 lines (22 loc) · 870 Bytes
/
cryptohash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import struct
import json
import hashlib
def cryptohash(msg, number, diffculty):
""" Create a sha256 hash for a given block at a given difficulty """
leading_bytes = '0'
block_hash = hashlib.sha256()
# Compute hash for the ’block’ first
block_hash.update(json.dumps(msg).encode())
for nonce in range(0, number):
# Convert out ‘number’ to binary encoding
nonce_bin = struct.pack("<I", nonce)
block_hash_copy = block_hash.copy()
# Compute cryptohash(msg, number)
block_hash_copy.update(nonce_bin)
hash_hex = block_hash_copy.hexdigest()
# Check if leading 16 bytes are zero
if hash_hex[:diffculty] != leading_bytes * diffculty:
continue
return (nonce, hash_hex)
# make the code easier if can't create a hash
cryptohash(msg, number, diffculty - 1)