2012-09-25 19 views
10

我想序列化/反序列化md5上下文。但我不知道如何用Python做到这一点。 我想要做的伪代码。序列化MD5计算状态并稍后恢复?

import md5 
# Start hash generation 
m = md5.new() 
m.update("Content") 

# Serialize m 
serialized_m = serialize(m) 

# In another function/machine, deserialize m 
# and continue hash generation 
m2 = deserialize(serialized_m) 
m2.update("More content") 
m2.digest()  

这里有C++库。有Python吗?为什么md5库不支持它?有没有安全问题?谢谢。

编辑: 我想这样做,因为例如,HTTP服务器想要接受不同HTTP请求中的流数据。在请求之间以某种方式序列化md5上下文会很方便。

+0

你为什么要那么做? –

+3

http://stackoverflow.com/questions/5865824/hash-algorithm-for-dynamic-growing-streaming-data – Kevin

+0

谢谢。 pypy库说不使用它因为它没有测试=(为什么官方的python md5没有实现这个寿命? – Yey

回答

1

我问先生圭多V Rossum的。他回答说:“我认为没有办法,但它可能会提出一个不错的功能要求,你可以提交一个到bugs.python.org。”所以我做了。

http://bugs.python.org/issue16059

0

HASH对象不能序列:How to serialize hash objects in Python

假设你可以绕过散列的数据:

from Crypto.Hash import MD5 

# generate hash 
m = MD5.new() 
s = "foo" 
m.update(s) 

# serialize m 
serialized = s 

# deserialize and continue hash generation 
m2 = MD5.new(serialized) 
if m2.hexdigest() == m.hexdigest(): 
    print "success" 
m2.update("bar")