Computing an md5 hash of a data structure

后端 未结 7 1756
南旧
南旧 2020-12-04 10:13

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, canonical

相关标签:
7条回答
  • 2020-12-04 11:01

    You could use the builtin pprint that will cover some more cases than the proposed json.dumps() solution. For example datetime-objects will be handled correctly.

    Your example rewritten to use pprint instead of json:

    >>> import hashlib, random, pprint
    >>> for i in range(10):
    ...     k = [i*i for i in range(1000)]
    ...     random.shuffle(k)
    ...     d = dict.fromkeys(k, 1)
    ...     print hashlib.md5(pprint.pformat(d)).hexdigest()
    ... 
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    b4e5de6e1c4f3c6540e962fd5b1891db
    
    0 讨论(0)
提交回复
热议问题