checksum works in unix but not win32 os

G

GujuBoy

i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())
totbytes = len(filedata)
result = 0
def rotate_right(c):
if c&1: return (c>>1)|0x8000
else: return c>>1
for ch in filedata:
result = (rotate_right(result)+ch) & 0xFFFF
return result

Unix File = 8683
Win32 = 23662

Please let me know why its acting different in WIN32, also it only does
this on SOME files...out of 50 i tested only 5 it failed on.
 
T

Trent Mick

[GujuBoy wrote]
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())
totbytes = len(filedata)
result = 0
def rotate_right(c):
if c&1: return (c>>1)|0x8000
else: return c>>1
for ch in filedata:
result = (rotate_right(result)+ch) & 0xFFFF
return result

Unix File = 8683
Win32 = 23662

Please let me know why its acting different in WIN32, also it only does
this on SOME files...out of 50 i tested only 5 it failed on.

By "failed" you mean the checksum is different? The file itself (if it
is a text file) might very well have different content: EOL characters
are different on Windows (CRLF) and Unix (LF). If a text file is opened
in textmode (the default), then the CRLF's will get translated to LF's,
however, if opened in binary mode (by having a 'b' in the mode option to
open()/file()) then this translation is not done.

Also, any reason you are not just using the hexdigest() method in the
md5 module?

Cheers,
Trent
 
P

Peter Hansen

GujuBoy said:
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())

Did you make sure you opened this "fileobj" using
"rb" instead of the default "r"? Look for the
open() call that created it and put "rb" in... otherwise
CRLF combinations are converted to LF on input...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top