Question regarding checksuming of a file

Roy Smith roy at panix.com
Sun May 14 03:48:54 CEST 2006


In article <126d1u4ke4itg7f at corp.supernews.com>,
 Andrew Robert <andrew.arobert at gmail.com> wrote:

> Good evening,
> 
> I need to generate checksums of a file, store the value in a variable,
> and pass it along for later comparison.
> 
> The MD5 module would seem to do the trick but I'm sketchy on implementation.
> 
> 
> The nearest I can see would be
> 
> import md5
> 
> m=md5.new()
> contents = open(self.file_name,"rb").read()
> check=md5.update(contents)
> 
> However this does not appear to be actually returning the checksum.
> 
> Does anyone have insight into where I am going wrong?

After calling update(), you need to call digest().  Update() only updates 
the internal state of the md5 state machine; digest() returns the hash.  
Also, for the code above, it's m.update(), not md5.update().  Update() is a 
method of an md5 instance object, not the md5 module itself.

Lastly, the md5 algorithm is known to be weak.  If you're doing md5 to 
maintain compatability with some pre-existing implementation, that's one 
thing.  But, if you're starting something new from scratch, I would suggest 
using SHA-1 instead (see the sha module).  SHA-1 is much stronger 
cryptographically than md5.  The Python API is virtually identical, so it's 
no added work to switch to the stronger algorithm.



More information about the Python-list mailing list