Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86707

Re: Md5 is different in Windows and Linux

From Peter Otten <__peter__@web.de>
Subject Re: Md5 is different in Windows and Linux
Date 2015-03-02 09:54 +0100
Organization None
References <CA+QVAb4D1JTnaQ5ZOaB51syt1_NePt+QeSHOxEmnhMjxr10BEQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.19.1425286466.13471.python-list@python.org> (permalink)

Show all headers | View raw


Sarvagya Pant wrote:

> Hello, I am amazed that the  md5 of a file given by python in windows is
> different than that of linux. Consider the following code:
> 
> import hashlib
> def md5_for_file(f, block_size=2**20):
>     md5 = hashlib.md5()
>     while True:
>         data = f.read(block_size)
>         if not data:
>             break
>         md5.update(data)
>     return md5.hexdigest()
> 
> f = open("somefile.txt")
> print md5_for_file(f)
> 
> When I run the program I get the checksum value:
> 2f9cc8da53ee89762a34702f745d2956
> 
> But on this site http://onlinemd5.com/ and on linux it has value
> E10D4E3847713472F51BC606852712F1.
> 
> Why is there difference in value of Checksum computed by python in windows
> and other system.?

You have to open the file in binary mode

f = open("somefile.txt", "rb")

Otherwise on Windows "\r\n" will be translated to "\n" while Linux passes 
the bytes unchanged -- and thus md5.update() will see different data and 
produce a different checksum.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Md5 is different in Windows and Linux Peter Otten <__peter__@web.de> - 2015-03-02 09:54 +0100

csiph-web