Created
May 6, 2019 17:52
-
-
Save craigderington/ae7783769a29384e3427fc4d0973be2b to your computer and use it in GitHub Desktop.
Create a Hash of a File
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 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