计算数据结构的 md5 哈希 [英] Computing an md5 hash of a data structure
问题描述
我想计算不是字符串而是整个数据结构的 md5 哈希.我了解执行此操作的方法的机制(调度值的类型、规范化字典键顺序和其他随机性、递归到子值等).但它似乎是一种通常有用的操作,所以我很惊讶我需要自己滚动它.
Python 中是否有一些更简单的方法来实现这一点?
更新:已经建议使用pickle,这是一个好主意,但是pickle 不能规范化字典键顺序:
<预><代码>>>>导入 cPickle 作为泡菜>>>导入 hashlib,随机>>>对于范围内的我(10):... k = [i*i for i in range(1000)]... random.shuffle(k)... d = dict.fromkeys(k, 1)... p = pickle.dumps(d)... 打印 hashlib.md5(p).hexdigest()...51b5855799f6d574c722ef9e50c2622b43d6b52b885f4ecb4b4be7ecdcfbb04ee7be0e6d923fe1b30c6fbd5dcd3c20b9aebb2298be19908e523e86a3f37122077db3fe10dcdb70652f845b02b655706143945441efe82483ba65fda471d792548e4196468769333d170b6bb179b4aee0951446fa44dba9a1a26e7df9083dcadf06b09465917d3881707a4909f67451ae386e3f08a3c1156edd1bd0f3862df481bencode 排序字典所以:
导入 hashlib进口代码数据 = ['only', 'lists', [1,2,3],'dictionaries', {'a':0,'b':1}, 'numbers', 47, 'strings']data_md5 = hashlib.md5(bencode.bencode(data)).hexdigest()打印数据_md5
印刷品:
af1b88ca9fd8a3e828b40ed1b9a2cb20
I want to compute an md5 hash not of a string, but of an entire data structure. I understand the mechanics of a way to do this (dispatch on the type of the value, canonicalize dictionary key order and other randomness, recurse into sub-values, etc). But it seems like the kind of operation that would be generally useful, so I'm surprised I need to roll this myself.
Is there some simpler way in Python to achieve this?
UPDATE: pickle has been suggested, and it's a good idea, but pickling doesn't canonicalize dictionary key order:
>>> import cPickle as pickle
>>> import hashlib, random
>>> for i in range(10):
... k = [i*i for i in range(1000)]
... random.shuffle(k)
... d = dict.fromkeys(k, 1)
... p = pickle.dumps(d)
... print hashlib.md5(p).hexdigest()
...
51b5855799f6d574c722ef9e50c2622b
43d6b52b885f4ecb4b4be7ecdcfbb04e
e7be0e6d923fe1b30c6fbd5dcd3c20b9
aebb2298be19908e523e86a3f3712207
7db3fe10dcdb70652f845b02b6557061
43945441efe82483ba65fda471d79254
8e4196468769333d170b6bb179b4aee0
951446fa44dba9a1a26e7df9083dcadf
06b09465917d3881707a4909f67451ae
386e3f08a3c1156edd1bd0f3862df481
bencode sorts dictionaries so:
import hashlib
import bencode
data = ['only', 'lists', [1,2,3],
'dictionaries', {'a':0,'b':1}, 'numbers', 47, 'strings']
data_md5 = hashlib.md5(bencode.bencode(data)).hexdigest()
print data_md5
prints:
af1b88ca9fd8a3e828b40ed1b9a2cb20
这篇关于计算数据结构的 md5 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!