Add hash check on document read/write (#890)

* Add sha256sum function
* Add hash check to document class
* Make sha256sum test more thorough
* Handle exceptions in the sha256sum function directly
* Update test coverage
* Clarify title on dialog box
* Don't write blank lines in makeAlert
This commit is contained in:
Veronica Berglyd Olsen
2021-09-19 13:55:54 +02:00
committed by GitHub
parent d65c5e93b6
commit c852a5bda3
6 changed files with 140 additions and 29 deletions
+25 -1
View File
@@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import json
import hashlib
import logging
from datetime import datetime
@@ -411,7 +412,7 @@ def jsonEncode(data, n=0, nmax=0):
# =============================================================================================== #
# Other Functions
# File and File System Functions
# =============================================================================================== #
def makeFileNameSafe(theText):
@@ -424,6 +425,29 @@ def makeFileNameSafe(theText):
return cleanName
def sha256sum(filePath):
"""Make a shasum of a file using a buffer.
Based on: https://stackoverflow.com/a/44873382/5825851
"""
hDigest = hashlib.sha256()
bData = bytearray(65536)
mData = memoryview(bData)
try:
with open(filePath, mode="rb", buffering=0) as inFile:
for n in iter(lambda: inFile.readinto(mData), 0):
hDigest.update(mData[:n])
except Exception:
logger.error("Could not read sha256sum of: %s", filePath)
logException()
return None
return hDigest.hexdigest()
# =============================================================================================== #
# Other Functions
# =============================================================================================== #
def getGuiItem(theName):
"""Returns a QtWidget based on its objectName.
"""