我正在尝试使用hashlib
库在python3
中调用服务。此代码中的最后一行给出例外:
auth = hashed.digest().decode("utf-8").rstrip('n')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 0: invalid start byte
这是代码段:
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')
我缺少什么?
hashed.digest().decode("utf-8").rstrip('n')
Digest返回字节,但没有任何字节序列是有效的UTF-8。
我不知道您要完成的工作,但是此代码是有缺陷的。