Skip to content

Instantly share code, notes, and snippets.

@craigderington
Created May 6, 2019 17:52
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 craigderington/ae7783769a29384e3427fc4d0973be2b to your computer and use it in GitHub Desktop.
Save craigderington/ae7783769a29384e3427fc4d0973be2b to your computer and use it in GitHub Desktop.
Create a Hash of a File
import os
import hashlib
def sha256sum(filename):
"""
Create a file hash
:param: str (file path)
:return: str (md5 hash)
"""
h = hashlib.sha256()
b = bytearray(128*1024)
mv = memoryview(b)
with open(filename, 'rb', buffering=0) as f1:
for n in iter(lambda : f1.readinto(mv), 0):
h.update(mv[:n])
return h.hexdigest()
filehash = sha256sum(os.getcwd() + '/' + 'fourletterwords.txt')
print('File Hash: {}'.format(filehash))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment