From 79403afbe6f73a199af44892c90e265743f3ad7f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 29 Dec 2016 21:43:47 +0100 Subject: [PATCH] python/build/verify: prepare SHA support --- python/build/download.py | 2 +- python/build/verify.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/build/download.py b/python/build/download.py index 4e4efdac8..9cc710c8f 100644 --- a/python/build/download.py +++ b/python/build/download.py @@ -20,7 +20,7 @@ def download_and_verify(url, md5, parent_path): urllib.request.urlretrieve(url, tmp_path) if not verify_file_digest(tmp_path, md5): os.unlink(tmp_path) - raise RuntimeError("MD5 mismatch") + raise RuntimeError("Digest mismatch") os.rename(tmp_path, path) return path diff --git a/python/build/verify.py b/python/build/verify.py index 450b2aece..5082ef521 100644 --- a/python/build/verify.py +++ b/python/build/verify.py @@ -23,12 +23,16 @@ def file_digest(algorithm, path): feed_file_path(h, path) return h.hexdigest() -def file_md5(path): - """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" - - return file_digest(hashlib.md5, path) +def guess_digest_algorithm(digest): + l = len(digest) + if l == 32: + return hashlib.md5 + else: + return None def verify_file_digest(path, expected_digest): """Verify the digest of a file, and return True if the digest matches with the given expected digest.""" - return file_md5(path) == expected_digest + algorithm = guess_digest_algorithm(expected_digest) + assert(algorithm is not None) + return file_digest(algorithm, path) == expected_digest