Help getting the md5 module to work

Carl Banks imbosol at aerojockey.invalid
Sat Mar 20 03:30:42 CET 2004


Grumfish wrote:
> I'm trying to use the md5 module but it gives me a different result from 
> what other programs give. This is the code I have and the result from 
> hexdigest() never matches what I expect. Its probably something stupid 
> but its driving me crazy.
> 
> f = file (filename, "r")
> m = md5.new()
> data = f.read (CHUNK_SIZE)
> while data != "":
>        m.update (data)
>        data = f.read (CHUNK_SIZE)


I'll wager this is happening on Windows.  When reading data in text
mode on Windows, Python converts "\r\n" sequence to "\n".  You need to
open the file in binary mode.  Try changing the first line to:

    f = file(filename,"rb")




-- 
CARL BANKS                      http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon



More information about the Python-list mailing list