Skip to content

Instantly share code, notes, and snippets.

@sc137
Created May 15, 2019 23:22
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 sc137/19276227560e2788f1591322805a27c8 to your computer and use it in GitHub Desktop.
Save sc137/19276227560e2788f1591322805a27c8 to your computer and use it in GitHub Desktop.
check stored sha256 value
#!/usr/bin/env python3
# file_integrity_check.py
# check file integrity from a list
# use shasum -a 256 <filename> to generate initial checksum
import hashlib
from filechecksums import * # gets precomputed variables
import os.path
def filesha256(filename, check, block_size=2**20):
# first check if the file is there
if os.path.isfile(filename):
f = open(filename, "rb")
sha256 = hashlib.sha256()
while True:
data = f.read(block_size)
if not data:
break
sha256.update(data)
f.close()
result = sha256.hexdigest()
if check == result:
print('file integrity for', filename, 'confirmed')
else:
print(filename, 'SHA256 *** mismatch ***')
else:
print('file not found', filename)
filesha256('/Users/sable/code/test.py', test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment