Python 散列 uuid 的 2 和 3 个区别

Python 2 and 3 differences in hashing a uuid

我有一些适用于 python 2:

的 py2 代码
    import uuid
    import hashlib

    playername = "OfflinePlayer:%s" % name  # name is just a text only username
    m = hashlib.md5()
    m.update(playername)
    d = bytearray(m.digest())
    d[6] &= 0x0f
    d[6] |= 0x30
    d[8] &= 0x3f
    d[8] |= 0x80
    print(uuid.UUID(bytes=str(d)))

但是,当 python3 中的代码 when 运行 时,它会在尝试 m.update() 时生成 "TypeError: Unicode-objects must be encoded before hashing"。我尝试首先使用默认的 utf-8 对它进行编码:

m.update(playername.encode())

但现在这一行 -

print(uuid.UUID(bytes=str(d)))

产生此错误:

  File "/usr/lib/python3.5/uuid.py", line 149, in __init__
    raise ValueError('bytes is not a 16-char string')
ValueError: bytes is not a 16-char string

然后我尝试将其解码回来,但按位运算显然已经毁了它(我猜?):

    print(uuid.UUID(bytes=(d.decode())))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfd in position 2: invalid start byte

老实说,我不知道 "big-picture" 中按位运算的目的是什么。一般来说,代码片段应该根据用户名的拼写每次都产生相同的预期 UUID。

我只想让这段代码在 Python3 中完成与在 Python 中相同的工作 2 :(

提前致谢。

几件事:

m.update(playername.encode('utf-8'))

应该正确编码您的字符串。

print(uuid.UUID(bytes=bytes(d)))

应该 return UUID 正确。

示例:

https://repl.it/EyuG/0