Skip to content

Commit 9510d20

Browse files
committed
rebase
Signed-off-by: laurentsimon <laurentsimon@google.com>
1 parent eb13ae9 commit 9510d20

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

sigstore/_utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@
2525

2626
from cryptography.hazmat.primitives import serialization
2727
from cryptography.hazmat.primitives.asymmetric import ec, rsa
28-
<<<<<<< HEAD
29-
from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
3028
from cryptography.x509 import (
3129
Certificate,
3230
ExtensionNotFound,
3331
Version,
3432
load_der_x509_certificate,
3533
)
36-
=======
37-
from cryptography.x509 import Certificate, ExtensionNotFound, Version
38-
>>>>>>> f67b019 (Change API)
3934
from cryptography.x509.oid import ExtendedKeyUsageOID, ExtensionOID
4035
from sigstore_protobuf_specs.dev.sigstore.common.v1 import HashAlgorithm
4136

sigstore/hashes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from cryptography.hazmat.primitives import hashes
1616
from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
1717
from pydantic import BaseModel
18+
import rekor_types
1819
from sigstore_protobuf_specs.dev.sigstore.common.v1 import HashAlgorithm
1920

2021

@@ -33,11 +34,13 @@ class Hashed(BaseModel):
3334
The digest representing the hash value.
3435
"""
3536

36-
def as_prehashed(self) -> Prehashed:
37-
return Prehashed(self.hazmat_algorithm())
37+
def as_hashedrekord_algorithm(self) -> rekor_types.hashedrekord.Algorithm:
38+
if self.algorithm == HashAlgorithm.SHA2_256:
39+
return rekor_types.hashedrekord.Algorithm.SHA256
40+
raise ValueError(f"unknown hash algorithm: {self.algorithm}")
3841

39-
def hazmat_algorithm(self) -> hashes.HashAlgorithm:
42+
def as_prehashed(self) -> Prehashed:
4043
if self.algorithm == HashAlgorithm.SHA2_256:
41-
return hashes.SHA256()
42-
# Add more hashes here.
44+
return Prehashed(hashes.SHA256())
4345
raise ValueError(f"unknown hash algorithm: {self.algorithm}")
46+

sigstore/sign.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
VerificationMaterial,
5757
)
5858
from sigstore_protobuf_specs.dev.sigstore.common.v1 import (
59-
HashAlgorithm,
6059
HashOutput,
6160
LogId,
6261
MessageSignature,
@@ -72,8 +71,8 @@
7271
)
7372
from sigstore_protobuf_specs.io.intoto import Envelope
7473

75-
from sigstore._internal import dsse
7674
from sigstore import hashes as sigstore_hashes
75+
from sigstore._internal import dsse
7776
from sigstore._internal.fulcio import (
7877
ExpiredCertificate,
7978
FulcioCertificateSigningResponse,
@@ -82,7 +81,7 @@
8281
from sigstore._internal.rekor.client import RekorClient
8382
from sigstore._internal.sct import verify_sct
8483
from sigstore._internal.trustroot import TrustedRoot
85-
from sigstore._utils import PEMCert, get_digest, sha256_streaming
84+
from sigstore._utils import PEMCert, get_digest
8685
from sigstore.oidc import ExpiredIdentity, IdentityToken
8786
from sigstore.transparency import LogEntry
8887

@@ -176,7 +175,7 @@ def _signing_cert(
176175

177176
def sign(
178177
self,
179-
input_: IO[bytes] | Statement,
178+
input_: IO[bytes] | Statement | sigstore_hashes.Hashed,
180179
) -> Bundle:
181180
"""Public API for signing blobs"""
182181
private_key = self._private_key
@@ -219,16 +218,16 @@ def sign(
219218
),
220219
)
221220
else:
222-
input_digest = sha256_streaming(input_)
221+
hashed_input = get_digest(input_)
223222

224223
artifact_signature = private_key.sign(
225-
input_digest, ec.ECDSA(Prehashed(hashes.SHA256()))
224+
hashed_input.digest, ec.ECDSA(hashed_input.as_prehashed())
226225
)
227226

228227
content = MessageSignature(
229228
message_digest=HashOutput(
230-
algorithm=HashAlgorithm.SHA2_256,
231-
digest=input_digest,
229+
algorithm=hashed_input.algorithm,
230+
digest=hashed_input.digest.hex(),
232231
),
233232
signature=artifact_signature,
234233
)
@@ -244,8 +243,8 @@ def sign(
244243
),
245244
data=rekor_types.hashedrekord.Data(
246245
hash=rekor_types.hashedrekord.Hash(
247-
algorithm=rekor_types.hashedrekord.Algorithm.SHA256,
248-
value=input_digest.hex(),
246+
algorithm=hashed_input.as_hashedrekord_algorithm(),
247+
value=hashed_input.digest.hex(),
249248
)
250249
),
251250
),

0 commit comments

Comments
 (0)