From 1976ccb843c0a0187692c0056ca1874dfb4f549e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 18 Feb 2020 00:09:15 -0800 Subject: [PATCH] Let known_size checking of verify_hash be optional. --- voussoirkit/spinal.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/voussoirkit/spinal.py b/voussoirkit/spinal.py index 04519ab..b1d225d 100644 --- a/voussoirkit/spinal.py +++ b/voussoirkit/spinal.py @@ -539,7 +539,13 @@ def normalize(text): ''' return os.path.normpath(os.path.normcase(text)) -def verify_hash(path, known_size, known_hash, callback=None): +def verify_hash( + path, + known_hash, + *, + known_size=None, + callback=None, + ): ''' callback: A function that takes three parameters: @@ -563,6 +569,11 @@ def verify_hash(path, known_size, known_hash, callback=None): if callback is not None: callback(path, checked_bytes, file_size) + if known_size is not None: + file_size = os.path.getsize(path.absolute_path) + if file_size != known_size: + raise ValidationError(f'File size {file_size} != known size {known_size}.') + file_hash = hasher.hexdigest() if file_hash != known_hash: raise ValidationError(f'File hash "{file_hash}" != known hash "{known_hash}".')