python / python/cpython

`hashlib` add Protocol for hash-objects

未关闭
#101,106 8 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-typing type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

hashlib 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) this should look something like

@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, type[Hash]] objects.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 hashlib 文档和所引用的 PEP 688 Buffer Protocol 开始,将提议的 Hash 接口与现有的哈希对象进行比较。确定是否应添加可在运行时检查的协议,以及 algorithms_guaranteed 和 algorithms_available 是否应从集合改为映射。完成的标准是:已接受的 API 得到规范说明并完成实现,同时为受支持的哈希对象提供适当的覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cryptography
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。