Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:在散列之前必须对Unicode对象进行编码_Python_Unicode_Hash_Encode - Fatal编程技术网

Python TypeError:在散列之前必须对Unicode对象进行编码

Python TypeError:在散列之前必须对Unicode对象进行编码,python,unicode,hash,encode,Python,Unicode,Hash,Encode,我试图从这个网站复制python程序: . Python程序如下所示: import hashlib, json import random def hashMe(msg=''): if type(msg)!=str: msg = json.dumps(msg, sort_keys=True) return str(hashlib.sha256(msg).hexdigest(), 'uft-8') def makeTransaction(maxValue=3

我试图从这个网站复制python程序: .

Python程序如下所示:

import hashlib, json
import random

def hashMe(msg=''):
    if type(msg)!=str:
        msg = json.dumps(msg, sort_keys=True) 
    return str(hashlib.sha256(msg).hexdigest(), 'uft-8')

def makeTransaction(maxValue=3):
    sign= int(random.getrandbits(1))*2-1 
    amount = random.randint(1,maxValue)
    alicePays = sign*amount
    bobPays = -1*alicePays
    return {u'Alice':alicePays, u'Bob':bobPays}

txnBuffer = [makeTransaction() for i in range(30)]


def updateState(txn, state):
    state = state.copy() 
    for key in txn:  
        if key in state.key():
            state[key] += txn[key]
        else:
            state[key] = txn[key]
    return state

def isValidTxn(txn, state):
    if sum(txn.values()) is not 0:
        return False    
    for key in txn.keys():
        acctBalance = state[key]
    else:
        acctBalance = 0    
    if (acctBalance + txn[key]) <0:
        return False   
    return True

state = {u'Alice':50, u'Bob':50}
genesisBlockTxns = [state]
genesisBlockContents = {u'blockNumber':0,u'parentHash':None,u'txnCount':1,u'txns':genesisBlockTxns}
genesisHash = hashMe( genesisBlockContents )
导入hashlib,json 随机输入 def hashMe(msg=''): 如果输入(msg)=str: msg=json.dumps(msg,sort\u keys=True) 返回str(hashlib.sha256(msg.hexdigest(),'uft-8') def makeTransaction(最大值=3): 符号=int(随机.getrandbits(1))*2-1 amount=random.randint(1,maxValue) alicePays=签名*金额 BOB付款=-1*阿里赛帕斯 返回{u'Alice':alicePays,u'Bob':bobPays} txnBuffer=[makeTransaction()用于范围(30)内的i] def更新状态(txn,状态): state=state.copy() 对于txn中的键: 如果键处于状态。键(): 状态[键]+=txn[键] 其他: 状态[键]=txn[键] 返回状态 def isValidTxn(txn,状态): 如果总和(txn.values())不是0: 返回错误 对于txn.keys()中的键: acctBalance=状态[键] 其他: acctBalance=0
如果(acctBalance+txn[key])消息非常清楚:必须使用
encode
对Unicode对象进行编码,然后才能对其进行散列。它指的是什么Unicode对象?传入哈希函数
hashlib.sha256
,即
msg

一旦你通过了它,你会发现从
hexdigest()
返回的已经是一个不需要应用
str
的字符串。无论如何,您拼写的
utf-8
都不正确

return hashlib.sha256(msg.encode('utf-8')).hexdigest()

信息非常清楚:必须使用
encode
对Unicode对象进行编码,然后才能对其进行散列。它指的是什么Unicode对象?传入哈希函数
hashlib.sha256
,即
msg

一旦你通过了它,你会发现从
hexdigest()
返回的已经是一个不需要应用
str
的字符串。无论如何,您拼写的
utf-8
都不正确

return hashlib.sha256(msg.encode('utf-8')).hexdigest()

欢迎来到堆栈溢出。有关如何提问的指导,请阅读:欢迎来到Stack Overflow。有关如何提问的指导,请阅读:非常感谢。它起作用了@马克·兰索姆尼,谢谢。它起作用了@标记赎金
return hashlib.sha256(msg.encode('utf-8')).hexdigest()