From 3b7a3703692b0244d6f79078189454d3f5a946e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:47:50 +0100 Subject: [PATCH] [3.14] gh-155419: honor the file object's current position in `hashlib.file_digest` (GH-155422) (cherry picked from commit 5f6de00ebed9cfa811f5974ea8cc4e20b86e253c) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/hashlib.py | 7 +- Lib/test/test_hashlib.py | 81 +++++++++++++------ ...-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst | 3 + 3 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 0e9bd98aa1fc31..731cd5ce0f9cde 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -203,6 +203,8 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18): *digest* must either be a hash algorithm name as a *str*, a hash constructor, or a callable that returns a hash object. """ + import io + # On Linux we could use AF_ALG sockets and sendfile() to archive zero-copy # hashing with hardware acceleration. if isinstance(digest, str): @@ -210,9 +212,10 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18): else: digestobj = digest() - if hasattr(fileobj, "getbuffer"): + if isinstance(fileobj, io.BytesIO): # io.BytesIO object, use zero-copy buffer - digestobj.update(fileobj.getbuffer()) + with fileobj.getbuffer() as buf: + digestobj.update(buf[fileobj.tell():]) return digestobj # Only binary files implement readinto(). diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index d335db8694208d..75d597b1efe2fd 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -12,6 +12,7 @@ import itertools import logging import os +import random import re import sys import sysconfig @@ -118,6 +119,30 @@ def read_vectors(hash_name): yield parts +def make_hash_objects(*digestmods, buf=b"", **kwargs): + objects = [] + for digestmod in digestmods: + try: + if callable(digestmod): + obj = digestmod(buf) + else: + obj = hashlib.new(digestmod, buf, **kwargs) + except ValueError: + continue + objects.append((digestmod, obj)) + return objects + + +class BytesIOProxy: + """A file-like object with the io.BytesIO interface, not an io.BytesIO.""" + + def __init__(self, data): + self._buffer = io.BytesIO(data) + + def __getattr__(self, name): + return getattr(self._buffer, name) + + class HashLibTestCase(unittest.TestCase): supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', 'sha224', 'SHA224', 'sha256', 'SHA256', @@ -486,7 +511,8 @@ def test_blake2_update_over_4gb(self): self.assertEqual(h.hexdigest(), "8a268e83dd30528bc0907fa2008c91de8f090a0b6e0e60a5ff0d999d8485526f") def check(self, name, data, hexdigest, shake=False, **kwargs): - length = len(hexdigest)//2 + n = len(data) + length = len(hexdigest) // 2 hexdigest = hexdigest.lower() constructors = self.constructors_to_test[name] # 2 is for hashlib.name(...) and hashlib.new(name, ...) @@ -495,43 +521,48 @@ def check(self, name, data, hexdigest, shake=False, **kwargs): m = hash_object_constructor(data, **kwargs) computed = m.hexdigest() if not shake else m.hexdigest(length) self.assertEqual( - computed, hexdigest, - "Hash algorithm %s constructed using %s returned hexdigest" - " %r for %d byte input data that should have hashed to %r." - % (name, hash_object_constructor, - computed, len(data), hexdigest)) + computed, hexdigest, + "Hash algorithm %s constructed using %s returned hexdigest" + " %r for %d byte input data that should have hashed to %r." + % (name, hash_object_constructor, computed, n, hexdigest) + ) computed = m.digest() if not shake else m.digest(length) digest = bytes.fromhex(hexdigest) self.assertEqual(computed, digest) if not shake: self.assertEqual(len(digest), m.digest_size) + def generate_sub_hexdigest(pos=-1): + if pos < 0: + pos = 0 if n == 0 else random.randrange(0, n) + pos += 1 # ensure pos is at least 1 (allowed to be out of range) + buf = data[pos:] + objects = make_hash_objects(name, *constructors, buf=buf, **kwargs) + hexdigests = {obj.hexdigest() for _, obj in objects} + self.assertEqual(len(hexdigests), 1, f"bad digests: {objects}") + return pos, hexdigests.pop() + if not shake and kwargs.get("key") is None: # skip shake and blake2 extended parameter tests self.check_file_digest(name, data, hexdigest) + for pos in sorted({-1, 1, n - 1, n, n + 1}): + with self.subTest(pos=pos): + pos, hexdigest2 = generate_sub_hexdigest(pos=pos) + self.check_file_digest(name, data, hexdigest2, pos) - def check_file_digest(self, name, data, hexdigest): + def check_file_digest(self, name, data, hexdigest, pos=0): hexdigest = hexdigest.lower() - try: - hashlib.new(name) - except ValueError: - # skip, algorithm is blocked by security policy. - return - digests = [name] - digests.extend(self.constructors_to_test[name]) - + digests = make_hash_objects(name, *self.constructors_to_test[name]) with tempfile.TemporaryFile() as f: f.write(data) - - for digest in digests: - buf = io.BytesIO(data) - buf.seek(0) - self.assertEqual( - hashlib.file_digest(buf, digest).hexdigest(), hexdigest - ) - f.seek(0) - digestobj = hashlib.file_digest(f, digest) - self.assertEqual(digestobj.hexdigest(), hexdigest) + buf = io.BytesIO(data) + buf_like = BytesIOProxy(data) + + for digest, _ in digests: + for fobj in [buf, buf_like, f]: + fobj.seek(pos) + digestobj = hashlib.file_digest(fobj, digest) + self.assertEqual(digestobj.hexdigest(), hexdigest) def check_no_unicode(self, algorithm_name): # Unicode objects are not allowed as input. diff --git a/Misc/NEWS.d/next/Library/2026-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst b/Misc/NEWS.d/next/Library/2026-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst new file mode 100644 index 00000000000000..2117f189d64871 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-09-13-23-12.gh-issue-155419.J6dSFJ.rst @@ -0,0 +1,3 @@ +Ensure that :func:`hashlib.file_digest` honors the current position of the +file object when given an :class:`io.BytesIO` object. Patch by Bénédikt +Tran.