Python TypeError:哈希编码之前必须对Unicode对象进行编码

时间:2018-11-10 06:45:51

标签: python hash encoding typeerror

我已经看过这里的其他4个问题,但仍然不知道将.encode().encode('utf-8')放在哪里。我已将其注释掉,以显示我尝试过的各个地方。

import hashlib as hasher

    class Block:
        def __init__(self, index, timestamp, data, previous_hash):
            self.index = index
            self.timestamp = timestamp
            self.data = data 
            self.previous_hash = previous_hash #these four items used to calculate crypHash of each block
            self.hash = self.hash_block() #helps ensure integrity throughout blockchain

        def hash_block(self):
            sha = hasher.sha256() #.encode('utf-8')
            sha.update(str(self.index)+
                str(self.timestamp)+
                str(self.data)+
                str(self.previous_hash)) #.encode() inside brackets
            return sha.hexdigest()

这是回溯(自下而上阅读):

Traceback (most recent call last):
  File "blockchain.py", line 7, in <module>
    blockchain = [create_genesis_block()]
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/genesis.py", line 8, in create_genesis_block
    return Block(0, date.datetime.now(), "Genesis Block", "0")
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/block.py", line 9, in __init__
    self.hash = self.hash_block() #helps ensure integrity throughout blockchain
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/block.py", line 16, in hash_block
    str(self.previous_hash))
TypeError: Unicode-objects must be encoded before hashing

似乎表明str(self.previous_hash)) #.encode() inside brackets行应为str(self.previous_hash).encode()),但这给了我另一个错误:

Traceback (most recent call last):
  File "blockchain.py", line 7, in <module>
    blockchain = [create_genesis_block()]
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/genesis.py", line 8, in create_genesis_block
    return Block(0, date.datetime.now(), "Genesis Block", "0")
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/block.py", line 9, in __init__
    self.hash = self.hash_block() #helps ensure integrity throughout blockchain
  File "/media/nobu/win10Files/Blockchain/SnakeCoin/block.py", line 16, in hash_block
    str(self.previous_hash).encode())
TypeError: must be str, not bytes

因此,我尝试使用括号将encode()decode()进行各种组合,但这只会使我在错误之间交替。
因此,我很迷茫,不胜感激。 顺便说一句,此代码来自此处的中型文章:Medium Snake Coin

1 个答案:

答案 0 :(得分:1)

bytes个字符串可以散列。 str是Unicode字符串。编码为字节,解码为Unicode。

在您的.update()中,所有四个项目都将转换为str。因此,一旦将它们串联在一起,整个过程就.encode()变成了bytes

import hashlib as hasher

class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data 
        self.previous_hash = previous_hash #these four items used to calculate crypHash of each block
        self.hash = self.hash_block() #helps ensure integrity throughout blockchain

    def hash_block(self):
        sha = hasher.sha256()
        to_hash = str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)
        sha.update(to_hash.encode())
        return sha.hexdigest()