使用 MD5( ) 在 Python 中进行编码和解码
在 Python 3.1.1 中的 Ubuntu 10.10 上运行此代码时
出现以下错误:
UnicodeDecodeError: 'utf8' codec can't Decode byte 0xd3 inposition 0: invalid continuation byte
以及错误的位置变化取决于我何时运行以下代码: (不是真正的密钥或秘密)
sandboxAPIKey = "wed23hf5yxkbmvr9jsw323lkv5g"
sandboxSharedSecret = "98HsIjh39z"
def buildAuthParams():
authHash = hashlib.md5();
#encoding because the update on md5() needs a binary rep of the string
temp = str.encode(sandboxAPIKey + sandboxSharedSecret + repr(int(time.time())))
print(temp)
authHash.update(temp)
#look at the string representation of the binary digest
print(authHash.digest())
#now I want to look at the string representation of the digest
print(bytes.decode(authHash.digest()))
这是运行的输出(签名和密钥信息从实际输出更改)
b'sdwe5yxkwewvr9j343434385gkbH4343h4343dz129443643474'
b'\x945EM3\xf5\xa6\xf6\x92\xd1\r\xa5K\xa3IO'
print(bytes.decode(authHash.digest()))
UnicodeDecodeError: 'utf8' codec can't decode byte 0x94 in position 0: invalid start byte
我假设我在调用解码时没有得到正确的结果,但我无法弄清楚它是什么是。 authHash.digest 的打印对我来说看起来是有效的。
我真的很感激任何关于如何让它发挥作用的想法
Running this code on Ubuntu 10.10 in Python 3.1.1
I am getting the following error:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd3 in position 0: invalid continuation byte
And the position of the error changes depending on when I run the following code:
(not the real keys or secret)
sandboxAPIKey = "wed23hf5yxkbmvr9jsw323lkv5g"
sandboxSharedSecret = "98HsIjh39z"
def buildAuthParams():
authHash = hashlib.md5();
#encoding because the update on md5() needs a binary rep of the string
temp = str.encode(sandboxAPIKey + sandboxSharedSecret + repr(int(time.time())))
print(temp)
authHash.update(temp)
#look at the string representation of the binary digest
print(authHash.digest())
#now I want to look at the string representation of the digest
print(bytes.decode(authHash.digest()))
Here is the output of a run (with the sig and key information changed from the real output)
b'sdwe5yxkwewvr9j343434385gkbH4343h4343dz129443643474'
b'\x945EM3\xf5\xa6\xf6\x92\xd1\r\xa5K\xa3IO'
print(bytes.decode(authHash.digest()))
UnicodeDecodeError: 'utf8' codec can't decode byte 0x94 in position 0: invalid start byte
I am assuming I am not getting something right with my call to decode but I can not figure out what it is. The print of the authHash.digest looks like valid to me.
I would really appreciate any ideas on how to get this to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您尝试将字节数组解码为字符串时,它会尝试将字节顺序匹配到编码集(默认情况下为 utf-8)的有效字符,则会引发异常,因为它无法将字节序列与utf-8 字母表中的有效字符。
如果您尝试使用 ascii 对其进行解码,也会发生同样的情况,任何大于 127 的值都是无效的 ascii 字符。
因此,如果您尝试获取 md5 哈希值的可打印版本,则应该对其进行十六进制消化,这是打印任何类型哈希值的标准方法,每个字节由 2 个十六进制数字表示。
为了做到这一点,你可以使用:
如果你需要在 url 中使用它,你可能需要将 bytearray 编码为 base64:
When you try to decode a bytearray into a string it tries to match sequentially the bytes to valid characters of an encoding set(by default, utf-8), the exception is being raised because it can't match a sequence of bytes to a valid character in the utf-8 alphabet.
The same will happen if you try to decode it using ascii, any value greater than 127 is an invalid ascii character.
So, if you are trying to get a printable version of the md5 hash, you should hexdigest it, this is the standard way of printing any type of hash, each byte is represented by 2 hexadecimal digits.
In order to do this you can use:
If you need to use it in a url, you probably need to encode the bytearray into base64: