Computing an md5 hash of a data structure

后端 未结 7 1773
南旧
南旧 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:50

    UPDATE: this won't work for dictionaries due to key order randomness. Sorry, I've not thought of it.

    import hashlib
    import cPickle as pickle
    data = ['anything', 'you', 'want']
    data_pickle = pickle.dumps(data)
    data_md5 = hashlib.md5(data_pickle).hexdigest()
    

    This should work for any python data structure, and for objects as well.

提交回复
热议问题