Last active
August 29, 2017 10:02
-
-
Save maretekent/cf0820fffea30dd440cdc3e22b52229e to your computer and use it in GitHub Desktop.
Hashing the files to enable to ability to checksum
This file contains hidden or 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
import hashlib | |
filename = '/path/to/file/file1.txt'; | |
def file_hash(filename): | |
h = hashlib.sha256() | |
with open(filename, 'rb', buffering=0) as f: | |
for b in iter(lambda : f.read(128*1024), b''): | |
h.update(b) | |
return h.hexdigest() | |
print file_hash(filename) |
This file contains hidden or 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
import zlib | |
def adler32sum(filename, blocksize=65536): | |
checksum = zlib.adler32("") | |
with open(filename, "rb") as f: | |
for block in iter(lambda: f.read(blocksize), b""): | |
checksum = zlib.adler32(block, checksum) | |
return checksum & 0xffffffff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import hashlib
BLOCKSIZE = 65536
hasher = hashlib.md5()
with open('anotherfile.txt', 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
print(hasher.hexdigest())
import hashlib
BLOCKSIZE = 65536
hasher = hashlib.sha1()
with open('anotherfile.txt', 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
print(hasher.hexdigest())