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


Groups > comp.lang.python > #86703 > unrolled thread

Md5 is different in Windows and Linux

Started bySarvagya Pant <sarvagya.pant@gmail.com>
First post2015-03-02 12:04 +0545
Last post2015-03-03 00:45 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Md5 is different in Windows and Linux Sarvagya Pant <sarvagya.pant@gmail.com> - 2015-03-02 12:04 +0545
    Re: Md5 is different in Windows and Linux Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-02 10:24 +0000
    Re: Md5 is different in Windows and Linux sohcahtoa82@gmail.com - 2015-03-02 11:14 -0800
      Re: Md5 is different in Windows and Linux Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-03-03 00:45 +0100

#86703 — Md5 is different in Windows and Linux

FromSarvagya Pant <sarvagya.pant@gmail.com>
Date2015-03-02 12:04 +0545
SubjectMd5 is different in Windows and Linux
Message-ID<mailman.16.1425285828.13471.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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.?

-- 
*sarvagya*

[toc] | [next] | [standalone]


#86711

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-03-02 10:24 +0000
Message-ID<md1doh$68q$2@dont-email.me>
In reply to#86703
On Mon, 02 Mar 2015 12:04:46 +0545, Sarvagya Pant wrote:

> 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.?

Perhaps windows file io is padding the file to the block size? Or maybe 
the windows version has different end of lines.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#86777

Fromsohcahtoa82@gmail.com
Date2015-03-02 11:14 -0800
Message-ID<c25ca04b-817d-42fc-a0b1-32aa6478a0f0@googlegroups.com>
In reply to#86703
On Monday, March 2, 2015 at 12:43:59 AM UTC-8, 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.?
> 
> -- 
> 
> sarvagya

I don't know which is worse, the fact that you're composing your message in HTML, or the fact that you're using Comic Sans as your font.

[toc] | [prev] | [next] | [standalone]


#86796

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2015-03-03 00:45 +0100
Message-ID<md2smr$k2f$2@r01.glglgl.de>
In reply to#86777
Am 02.03.2015 20:14 schrieb sohcahtoa82@gmail.com:
> On Monday, March 2, 2015 at 12:43:59 AM UTC-8, Sarvagya Pant wrote:

>> f = open("somefile.txt")

This one is the problem. Under Windows, you have to open the file in 
binary to avoid that something "bad" happens with it.

So just do

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

and you should be fine.


> I don't know which is worse, the fact that you're composing your message in HTML, or the fact that you're using Comic Sans as your font.

#2 wouldn't be possible without #1...


Thomas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web