Computing an md5 hash of a data structure

后端 未结 7 1762
南旧
南旧 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 10:34

    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
    

提交回复
热议问题