Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/pypi_attestation_models/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import binascii
from base64 import b64decode, b64encode
from hashlib import sha256
from typing import TYPE_CHECKING, Annotated, Any, Literal, NewType

import rfc8785
Expand All @@ -17,6 +16,7 @@
from cryptography.hazmat.primitives import serialization
from pydantic import BaseModel
from pydantic_core import ValidationError
from sigstore._utils import _sha256_streaming
from sigstore.models import Bundle, LogEntry

if TYPE_CHECKING:
Expand Down Expand Up @@ -116,9 +116,14 @@ class AttestationPayload(BaseModel):
@classmethod
def from_dist(cls, dist: Path) -> AttestationPayload:
"""Create an `AttestationPayload` from a distribution file."""
with dist.open(mode="rb", buffering=0) as io:
# Replace this with `hashlib.file_digest()` once
# our minimum supported Python is >=3.11
digest = _sha256_streaming(io).hex()

return AttestationPayload(
distribution=dist.name,
digest=sha256(dist.read_bytes()).hexdigest(),
digest=digest,
)

def sign(self, signer: Signer) -> Attestation:
Expand Down
1 change: 1 addition & 0 deletions test/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ def test_attestation_payload(self) -> None:
expected = f'{{"digest":"{payload.digest}","distribution":"{payload.distribution}"}}'

assert bytes(payload) == bytes(expected, "utf-8")
assert json.loads(bytes(payload)) == json.loads(expected)