0

I am working on a Proof-of-Work system in Python.
For generating hash value I am using this function

from Cryptodome.Hash import SHA256

def blockhash(sender, msg, signature, nonce, timestamp):
    h = SHA256.new()
    hash_str = str(sender) + str(msg) + str(signature) + str(nonce) + str(timestamp)
    h.update(hash_str.encode())
    finalhash = h.hexdigest()
    return finalhash

When I generate a "transaction" it will do the Proof-of-Work while incrementing it by one and see if the hash starts with 4 zeros.

        while finalhash[:4] != "0000":
            finalhash = blockhash(self.sender, self.msg, self.signature, self.nonce, self.timestamp)
            self.nonce += 1
            print(str(self.nonce))
            print(finalhash)

If a transactions begins with two zeros it puts this file into a dictionary. This dictionary is written into a text file. When I want to verify this transaction and see if the values truely match the hash I always get the wrong hash even with the same data.

finalhash = blockhash(t["sender"], t["msg"], t["signature"], t["nonce"], t["timestamp"])

EDIT:
The input values are all being converted to a string. sender, msg and signature are bytes where nonce is an integer and timestamp a float.

Emil Engler
  • 489
  • 5
  • 14

1 Answers1

1

Solution: I found it out myself. The nonce is being incremented later on. If I want to find out the hash I need to reduce the nonce by one, then it matches. My mistake. Simply put self.nonce += 1 before calculating the hash

Emil Engler
  • 489
  • 5
  • 14