New GitHub issue #101106 from randolf-scholz:<br>
<hr>
<pre>
# Feature or enhancement
[hashlib](https://docs.python.org/3/library/hashlib.html) describes a Protocol for hash objects without implementing it.
The proposal is simply to create a concreate `Hash` or `HashObject` protocol that makes it explicit.
# Pitch
Using the [Buffer Protocol (PEP 688)](https://peps.python.org/pep-0688/) this should look something like
```python
@runtime_checkable
class Hash(Protocol):
name: str
def digest_size(self) -> int:
"""Return the size of the hash in bytes."""
def block_size(self) -> int:
"""Return the internal block size of the hash in bytes."""
def update(self, data: Buffer) -> None:
"""Update this hash object's state with the provided string."""
def digest(self) -> bytes:
"""Return the digest value as a string of binary data."""
def hexdigest(self) -> str:
"""Return the digest value as a string of hexadecimal digits."""
def copy(self) -> Self:
"""Return a copy ("clone") of the hash object."""
```
Having a `Protocol` class instead of prose makes it clear how exactly a hash-function needs to look like.
A second suggestion would be to change `hashlib.algorithms_guaranteed` and ` hashlib.algorithms_available` from `set[str]` to `dict[str, Hash]` objects.
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/101106">View on GitHub</a>
<p>Labels: type-feature</p>
<p>Assignee: </p>