Skip to content

Instantly share code, notes, and snippets.

@maretekent
Last active August 29, 2017 10:02
Show Gist options
  • Save maretekent/cf0820fffea30dd440cdc3e22b52229e to your computer and use it in GitHub Desktop.
Save maretekent/cf0820fffea30dd440cdc3e22b52229e to your computer and use it in GitHub Desktop.
Hashing the files to enable to ability to checksum
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)
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
@maretekent
Copy link
Author

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())

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