15
15
from __future__ import annotations
16
16
17
17
import base64
18
- import hashlib
19
18
import logging
19
+ from typing import IO
20
20
21
21
import cryptography .x509 as x509
22
22
from cryptography .hazmat .primitives import hashes , serialization
23
23
from cryptography .hazmat .primitives .asymmetric import ec
24
+ from cryptography .hazmat .primitives .asymmetric .utils import Prehashed
24
25
from cryptography .x509 .oid import NameOID
25
26
from pydantic import BaseModel
26
27
27
28
from sigstore ._internal .fulcio import FulcioClient
28
29
from sigstore ._internal .oidc import Identity
29
30
from sigstore ._internal .rekor import RekorClient , RekorEntry
30
31
from sigstore ._internal .sct import verify_sct
32
+ from sigstore ._utils import sha256_streaming
31
33
32
34
logger = logging .getLogger (__name__ )
33
35
@@ -56,11 +58,11 @@ def staging(cls) -> Signer:
56
58
57
59
def sign (
58
60
self ,
59
- input_ : bytes ,
61
+ input_ : IO [ bytes ] ,
60
62
identity_token : str ,
61
63
) -> SigningResult :
62
64
"""Public API for signing blobs"""
63
- sha256_artifact_hash = hashlib . sha256 (input_ ). hexdigest ( )
65
+ input_digest = sha256_streaming (input_ )
64
66
65
67
logger .debug ("Generating ephemeral keys..." )
66
68
private_key = ec .generate_private_key (ec .SECP384R1 ())
@@ -102,7 +104,9 @@ def sign(
102
104
logger .debug ("Successfully verified SCT..." )
103
105
104
106
# Sign artifact
105
- artifact_signature = private_key .sign (input_ , ec .ECDSA (hashes .SHA256 ()))
107
+ artifact_signature = private_key .sign (
108
+ input_digest , ec .ECDSA (Prehashed (hashes .SHA256 ()))
109
+ )
106
110
b64_artifact_signature = base64 .b64encode (artifact_signature ).decode ()
107
111
108
112
# Prepare inputs
@@ -113,7 +117,7 @@ def sign(
113
117
# Create the transparency log entry
114
118
entry = self ._rekor .log .entries .post (
115
119
b64_artifact_signature = b64_artifact_signature ,
116
- sha256_artifact_hash = sha256_artifact_hash ,
120
+ sha256_artifact_hash = input_digest . hex () ,
117
121
b64_cert = b64_cert .decode (),
118
122
)
119
123
0 commit comments