Skip to content

Instantly share code, notes, and snippets.

@xianghuzhao
Created November 2, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xianghuzhao/c1b56e7e570bdf47535d00127d91f510 to your computer and use it in GitHub Desktop.
Save xianghuzhao/c1b56e7e570bdf47535d00127d91f510 to your computer and use it in GitHub Desktop.
File checksum
#!/usr/bin/env python
import zlib
import hashlib
class Adler32CheckSum():
@staticmethod
def checksum(filename, blocksize=1048576):
value = 1
with open(filename, "rb") as f:
for block in iter(lambda: f.read(blocksize), ""):
value = zlib.adler32(block, value)
return hex(value & 0xffffffff).lower().replace('l','').replace('x','0')[-8:]
class Md5CheckSum():
@staticmethod
def checksum(filename, blocksize=1048576):
hash = hashlib.md5()
with open(filename, "rb") as f:
for block in iter(lambda: f.read(blocksize), ""):
hash.update(block)
return hash.hexdigest()
if __name__ == '__main__':
print Adler32CheckSum.checksum('/bin/ls')
print Md5CheckSum.checksum('/bin/ls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment