Created
November 2, 2016 10:06
-
-
Save xianghuzhao/c1b56e7e570bdf47535d00127d91f510 to your computer and use it in GitHub Desktop.
File checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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