如何计算 unicode 字典的 MD5 校验和?

How to calculate MD5 checksum for a unicode dictionary?

我在 python 中有一本字典,其中包含 unicode 值。我想计算这本字典的 md5 和。我尝试使用这个问题的答案:Computing an md5 hash of a data structure

import hashlib
import bencode
data = {'unicode_text': 'سلام'}
data_md5 = hashlib.md5(bencode.bencode(data)).hexdigest()
print data_md5

但是问题是bencodereturns这个错误:

KeyError: <type 'unicode'>

bencode library seems not to support unicode objects (anyway, it's written for Python 2, and I'm guessing you're using Python 3). How about using the built-in json 模块?

import hashlib
import json
data = {'unicode_text': 'سلام'}
data_md5 = hashlib.md5(json.dumps(data, sort_keys=True)).hexdigest()
print data_md5