Skip to content

Commit 898fb85

Browse files
committed
Refactor: Move HashFunction mapping from AlgorithmRegistry to Hashers
Signed-off-by: Appu <appu@google.com>
1 parent 2adbbf8 commit 898fb85

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

sigstore-java/src/main/java/dev/sigstore/AlgorithmRegistry.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package dev.sigstore;
1717

18-
import static com.google.common.hash.Hashing.sha256;
19-
20-
import com.google.common.hash.HashFunction;
2118
import java.security.PublicKey;
2219
import java.security.interfaces.ECPublicKey;
2320
import java.security.interfaces.RSAPublicKey;
@@ -71,8 +68,6 @@ public enum SigningAlgorithm {
7168

7269
// ECDSA
7370
PKIX_ECDSA_P256_SHA_256(HashAlgorithm.SHA2_256);
74-
// TODO: PKIX_ECDSA_P384_SHA_384(HashAlgorithm.SHA2_384),
75-
// TODO: PKIX_ECDSA_P521_SHA_512(HashAlgorithm.SHA2_512);
7671

7772
private final HashAlgorithm hashAlgorithm;
7873

@@ -86,18 +81,14 @@ public HashAlgorithm getHashAlgorithm() {
8681
}
8782

8883
public enum HashAlgorithm {
89-
SHA2_256("SHA256", 32, sha256());
90-
// TODO: SHA2_384("SHA384", 48, sha384()),
91-
// TODO: SHA2_512("SHA512", 64, sha512());
84+
SHA2_256("SHA256", 32);
9285

9386
private final String name;
9487
private final int length;
95-
private final HashFunction hashFunction;
9688

97-
HashAlgorithm(String name, int length, HashFunction hashFunction) {
89+
HashAlgorithm(String name, int length) {
9890
this.name = name;
9991
this.length = length;
100-
this.hashFunction = hashFunction;
10192
}
10293

10394
public String toString() {
@@ -107,9 +98,5 @@ public String toString() {
10798
public int getLength() {
10899
return length;
109100
}
110-
111-
HashFunction getHashFunction() {
112-
return hashFunction;
113-
}
114101
}
115102
}

sigstore-java/src/main/java/dev/sigstore/KeylessSigner.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import dev.sigstore.bundle.ImmutableSignature;
3131
import dev.sigstore.bundle.ImmutableTimestamp;
3232
import dev.sigstore.dsse.InTotoPayload;
33+
import dev.sigstore.encryption.Hashers;
3334
import dev.sigstore.encryption.certificates.Certificates;
3435
import dev.sigstore.encryption.signers.Signer;
3536
import dev.sigstore.encryption.signers.Signers;
@@ -654,11 +655,9 @@ public Map<Path, Bundle> signFiles(List<Path> artifacts) throws KeylessSignerExc
654655
var digests = new ArrayList<byte[]>(artifacts.size());
655656
for (var artifact : artifacts) {
656657
var artifactByteSource = com.google.common.io.Files.asByteSource(artifact.toFile());
658+
var hashFunction = Hashers.from(signingAlgorithm);
657659
try {
658-
digests.add(
659-
artifactByteSource
660-
.hash(signingAlgorithm.getHashAlgorithm().getHashFunction())
661-
.asBytes());
660+
digests.add(artifactByteSource.hash(hashFunction).asBytes());
662661
} catch (IOException ex) {
663662
throw new KeylessSignerException("Failed to hash artifact " + artifact);
664663
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2026 The Sigstore Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.sigstore.encryption;
17+
18+
import com.google.common.hash.HashFunction;
19+
import com.google.common.hash.Hashing;
20+
import dev.sigstore.AlgorithmRegistry;
21+
22+
public class Hashers {
23+
public static HashFunction from(AlgorithmRegistry.HashAlgorithm hashAlgorithm) {
24+
switch (hashAlgorithm) {
25+
case SHA2_256:
26+
return Hashing.sha256();
27+
}
28+
throw new IllegalStateException();
29+
}
30+
31+
public static HashFunction from(AlgorithmRegistry.SigningAlgorithm signingAlgorithm) {
32+
return from(signingAlgorithm.getHashAlgorithm());
33+
}
34+
}

0 commit comments

Comments
 (0)