使用 MD5( ) 在 Python 中进行编码和解码

发布于 2024-10-11 13:57:28 字数 1184 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-10-18 13:57:28

当您尝试将字节数组解码为字符串时,它会尝试将字节顺序匹配到编码集(默认情况下为 utf-8)的有效字符,则会引发异常,因为它无法将字节序列与utf-8 字母表中的有效字符。

如果您尝试使用 ascii 对其进行解码,也会发生同样的情况,任何大于 127 的值都是无效的 ascii 字符。

因此,如果您尝试获取 md5 哈希值的可打印版本,则应该对其进行十六进制消化,这是打印任何类型哈希值的标准方法,每个字节由 2 个十六进制数字表示。

为了做到这一点,你可以使用:

authHash.hexdigest()

如果你需要在 url 中使用它,你可能需要将 bytearray 编码为 base64:

base64.b64encode(authHash.digest())

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:

authHash.hexdigest()

If you need to use it in a url, you probably need to encode the bytearray into base64:

base64.b64encode(authHash.digest())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文