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 摘要。