Skip to content

Commit 02c716c

Browse files
committed
Avoid decoding and re-encoding
1 parent 662d5f1 commit 02c716c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

.github/embed-sbom.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
from pathlib import Path
1717

1818

19-
def record_entry(path: str, data: bytes) -> str:
19+
def record_entry(path: str, data: bytes) -> bytes:
2020
"""Build a RECORD line: `path,sha256=<base64url-nopad>,<size>`."""
2121
digest = base64.urlsafe_b64encode(hashlib.sha256(data).digest())
22-
return f"{path},sha256={digest.rstrip(b'=').decode()},{len(data)}"
22+
return (
23+
path.encode("utf-8")
24+
+ b",sha256="
25+
+ digest.rstrip(b"=")
26+
+ b","
27+
+ str(len(data)).encode()
28+
)
2329

2430

2531
def embed(wheel: Path, sbom: Path) -> None:
@@ -38,9 +44,9 @@ def embed(wheel: Path, sbom: Path) -> None:
3844
sbom_path = f"{dist_info}/sboms/{sbom.name}"
3945

4046
# Append a matching RECORD line for the SBOM (RECORD's own line has no hash).
41-
lines = contents[record_name].decode("utf-8").splitlines()
47+
lines = contents[record_name].splitlines()
4248
lines.append(record_entry(sbom_path, sbom_bytes))
43-
contents[record_name] = ("\n".join(lines) + "\n").encode("utf-8")
49+
contents[record_name] = b"\n".join(lines) + b"\n"
4450

4551
tmp = wheel.with_name(wheel.name + ".tmp")
4652
with zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zf:

0 commit comments

Comments
 (0)