Python md5无法解码字节
我正在尝试使用Python md5无法解码字节,python,python-3.x,md5,url-encoding,hashlib,Python,Python 3.x,Md5,Url Encoding,Hashlib,我正在尝试使用hashlib库调用python3中的服务。此代码的最后一行引发异常: auth=hashed.digest().decode(“utf-8”).rstrip(“\n”) UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0x96:无效的开始字节 以下是代码片段: import hashlib m = hashlib.md5() m.update("".encode("utf-8")) data_hash = base64.b64encode(m.
hashlib
库调用python3
中的服务。此代码的最后一行引发异常:
auth=hashed.digest().decode(“utf-8”).rstrip(“\n”)
UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0x96:无效的开始字节
以下是代码片段:
import hashlib
m = hashlib.md5()
m.update("".encode("utf-8"))
data_hash = base64.b64encode(m.digest()).decode("utf-8")
# Create Authorization Header
canonical = '%s,application/vnd.api+json,%s,%s,%s' % (action, data_hash, url, format_date_time(stamp))
canonical = canonical.encode("utf-8")
hashed = hmac.new(bytearray(secret, "utf-8"), canonical, sha1)
auth = hashed.digest().decode("utf-8").rstrip('\n')
我遗漏了什么?hash.digest().decode(“utf-8”).rstrip('\n')
摘要返回字节,但没有任何字节序列是有效的UTF-8
我不知道你想实现什么,但这段代码有缺陷。你希望摘要是UTF-8编码的吗?…或者用不同的措辞,是什么让你希望摘要是UTF-8编码的?