From fe7ab645247a7b4357b967c3610d95571caa92b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarek=20Ziad=C3=A9?= Date: Thu, 9 Sep 2021 12:00:35 +0200 Subject: [PATCH] add a simple file_digest helper --- Lib/hashlib.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 21a73f3bf6cb62..082a9fae69b988 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -65,7 +65,8 @@ algorithms_available = set(__always_supported) __all__ = __always_supported + ('new', 'algorithms_guaranteed', - 'algorithms_available', 'pbkdf2_hmac') + 'algorithms_available', 'pbkdf2_hmac', + 'file_digest') __builtin_constructor_cache = {} @@ -254,6 +255,22 @@ def prf(msg, inner=inner, outer=outer): pass +def file_digest(filepath, digest=None, chunk_size=4096, hex=True): + """Returns a digest of a file. + """ + if digest is None: + from _sha256 import sha256 + digest = sha256 + + hash = digest() + with open(filepath, "rb") as f: + for chunk in iter(lambda: f.read(chunk_size), b""): + hash.update(chunk) + if hex: + return hash.hexdigest() + return hash.digest() + + for __func_name in __always_supported: # try them all, some may not work due to the OpenSSL # version not supporting that algorithm. @@ -263,6 +280,10 @@ def prf(msg, inner=inner, outer=outer): import logging logging.exception('code for hash %s was not found.', __func_name) +if __name__ == '__main__': + import sys + + print(file_digest(sys.argv[1])) # Cleanup locals() del __always_supported, __func_name, __get_hash