Title: Add a file_digest() function in hashlib
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: tarek Nosy List: christian.heimes, gregory.p.smith, python-dev, tarek
Priority: normal Keywords: patch

Created on 2021-09-09 10:03 by tarek, last changed 2021-09-09 10:56 by tarek.

Pull Requests
URL Status Linked Edit
PR 28252 open python-dev, 2021-09-09 10:04
Messages (3)
msg401457 - (view) Author: Tarek Ziadé (tarek) * (Python committer) Date: 2021-09-09 10:03
I am proposing the addition of a very simple helper to return the hash of a file.
msg401461 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-09-09 10:48
Hey Tarek, long time no see!

* the _sha256 module is optional, can be disabled and is not available in some distributions.

* I also don't like to use sha256 has default. It's slow, even slower than sha512. Any default makes it also harder to upgrade to a better, more secure default in the future.

* like a file_digest() should accept PEP 452-compatible arguments and hash name as digstmod argument, not just a callable.

* a filename argument prevents users from passing in file-like objects like BytesIO.

* 4096 bytes chunk size is very conservative. The call overhead for read() and update() may dominate the performance of the function.

* The hex argument feels weird.

In a perfect world, the hash and hmac objects should get an "update_file" method. The OpenSSL-based hashes could even release the GIL and utilize OpenSSL's BIO layer to avoid any Python overhead.
msg401462 - (view) Author: Tarek Ziadé (tarek) * (Python committer) Date: 2021-09-09 10:56
Hey Christian, I hope things are well for you!
Thanks for all the precious feedback, I'll rework the patch accordingly
Date User Action Args
2021-09-09 10:56:27tareksetmessages: + msg401462
2021-09-09 10:48:47christian.heimessetmessages: + msg401461
2021-09-09 10:16:36serhiy.storchakasetnosy: + gregory.p.smith, christian.heimes

type: enhancement
versions: + Python 3.11
2021-09-09 10:04:49python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request26673
stage: patch review
2021-09-09 10:03:17tarekcreate