Skip to content

Instantly share code, notes, and snippets.

@kofemann
Last active October 15, 2021 07:57
Show Gist options
  • Save kofemann/2303046 to your computer and use it in GitHub Desktop.
Save kofemann/2303046 to your computer and use it in GitHub Desktop.
A script to calculate adler32 checksum of given files
#!/usr/bin/env python
'''A script to calculate adler32 checksum of given files'''
BLOCKSIZE=256*1024*1024
import sys
from zlib import adler32
for fname in sys.argv[1:]:
asum = 1
with open(fname,"rb") as f:
while True:
data = f.read(BLOCKSIZE)
if not data:
break
asum = adler32(data, asum)
if asum < 0:
asum += 2**32
print(hex(asum)[2:10].zfill(8).lower(), fname)
@vishalg0wda
Copy link

Could you please explain this part?

if asum < 0:
       asum += 2**32

EDIT: Is it related to this?

@shanehh
Copy link

shanehh commented Mar 22, 2021

for python3.8

def adler32sum(filepath):
    from zlib import adler32
    BLOCKSIZE = 256*1024*1024
    asum = 1
    with open(filepath, 'rb') as f:
        while (data := f.read(BLOCKSIZE)):
            # print('read len:', len(data))
            asum = adler32(data, asum)
    return asum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment