AttributeError: '_hashlib.HASH' object has no attribute 'startswith'

AttributeError: '_hashlib.HASH' object has no attribute 'startswith'

class Block:
   def __init__(self):
      self.verified_transactions = []
      self.previous_block_hash = ""
      self.Nonce = ""

   def mine(message, difficulty=1):
      assert difficulty >= 1
      prefix = '1' * difficulty
      for i in range(1000):
         digest = hashlib.sha256(str(hash(message)).encode('utf-8') + str(i).encode('utf-8'))
         if digest.startswith(prefix):
            print ("after " + str(i) + " iterations found nonce: "+ digest)
         return digest
error: Traceback (most recent call last):
  File "c:/Users/thoma/Desktop/Client.py", line 113, in <module>
    Block.mine ("test message", 2)
  File "c:/Users/thoma/Desktop/Client.py", line 94, in mine
    if str(digest.startswith(prefix)):
AttributeError: '_hashlib.HASH' object has no attribute 'startswith'

----完整代码-----

# import libraries
import hashlib
import random
import string
import json
import binascii
import numpy as np
import pandas as pd
import pylab as pl
import logging
import datetime
import collections
# following imports are required by PKI
import Cryptodome
import Cryptodome.Random
from Cryptodome.Hash import SHA
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import PKCS1_v1_5


transactions = []
last_block_hash = ""
TPCoins = []

def sha256(message):
      return hashlib.sha256(message.encode('ascii')).hexdigest()

class Client:
   def __init__(self):
      random = Cryptodome.Random.new().read
      self._private_key = RSA.generate(1024, random)
      self._public_key = self._private_key.publickey()
      self._signer = PKCS1_v1_5.new(self._private_key)

   @property
   def identity(self):
      return binascii.hexlify(self._public_key.exportKey(format='DER')).decode('ascii')

class Transaction:
   def __init__(self, sender, recipient, value):
      self.sender = sender
      self.recipient = recipient
      self.value = value
      self.time = datetime.datetime.now() 

   def to_dict(self):
      if self.sender == "Genesis":
         identity = "Genesis"
      else:
         identity = self.sender.identity

      return collections.OrderedDict({
         'sender': identity,
         'recipient': self.recipient,
         'value': self.value,
         'time' : self.time})

   def sign_transaction(self):
      private_key = self.sender._private_key
      signer = PKCS1_v1_5.new(private_key)
      h = SHA.new(str(self.to_dict()).encode('utf8'))
      return binascii.hexlify(signer.sign(h)).decode('ascii')

   def display_transaction(transaction):
      #for transaction in transactions:
      dict = transaction.to_dict()
      print ("sender: " + dict['sender'])
      print ('-----')
      print ("recipient: " + dict['recipient'])
      print ('-----')
      print ("value: " + str(dict['value']))
      print ('-----')
      print ("time: " + str(dict['time']))
      print ('-----')

   def dump_blockchain (self):
      print ("Number of blocks in the chain: " + str(len (self)))
      for x in range (len(TPCoins)):
         block_temp = TPCoins[x]
         print ("block # " + str(x))
         for transaction in block_temp.verified_transactions:
            Transaction.display_transaction (transaction)
            print ('--------------')
         print ('=====================================')

class Block:
   def __init__(self):
      self.verified_transactions = []
      self.previous_block_hash = ""
      self.Nonce = ""

   def mine(message, difficulty=1):
      assert difficulty >= 1
      prefix = '1' * difficulty
      for i in range(1000):
         digest = hashlib.sha256(str(hash(message)).encode('utf-8') + str(i).encode('utf-8'))
         if digest.startswith(prefix):
            print ("after " + str(i) + " iterations found nonce: "+ digest)
         return digest


#genesis block & users initialisation
Thomas = Client()
IoT_Sensor = Client()
Node = Client()
IoT_Device = Client()
t0 = Transaction("Genesis",Thomas.identity,200.0)
block0 = Block()
block0.previous_block_hash = None
Nonce = None
block0.verified_transactions.append (t0)
digest = hash (block0)
last_block_hash = digest
TPCoins.append (block0)
Transaction.dump_blockchain(TPCoins)
Block.mine ("test message", 2)

有什么想法吗?我正在编写教程 @ python blockchain tutorial

我认为问题是变量 digest 不是 str 而是 hashlib.sha256 对象,因此无法识别 .startswith() 。我试过转换为 str() 但没有任何运气。

感谢大家的帮助。

感谢 furas 的解决方案:)

def mine(message, difficulty=1):
      assert difficulty >= 1
      prefix = '1' * difficulty
      for i in range(1000):
         digest = sha256(str(hash(message)) + str(i))
         if digest.startswith(prefix):
            print ("after " + str(i) + " iterations found nonce: "+ digest)
            return digest

输出:

after 303 iterations found nonce: 11337d455c3d425b33cbeeb23fc87d6a3e7c05bf0d27f648e6c9df2f87eea2fb

再看教程

它使用自己的函数 sha256() 而不是标准的 hashlib.sha256() 并且这个 sha256() 最后有 .hexdigest():

def sha256(message):
    return hashlib.sha256(message.encode('ascii')).hexdigest()

后来它在 digest.startswith()

之前使用这个 sha256()
  digest = sha256(...)
  if digest.startswith(prefix):
      # ... code ...

并且您在代码中忘记了 .hexdigest()

  digest = hashlib.sha256(...).hexdigest()
  if digest.startswith(prefix):
      # ... code ...

顺便说一句:我认为教程中的 return digest 放错了地方 - 它会在第一个循环中 return digest 但它应该在 if digest.startswith(prefix):

     digest = hashlib.sha256(str(hash(message)).encode('utf-8') + str(i).encode('utf-8')).hexdigest()

     if digest.startswith(prefix):
        print("after", i, "iterations found nonce:", digest)
        return digest

如果在 1000 次迭代后它找不到带前缀的摘要,那么它将 return None