Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DigestUtils: avoid throwing on invalid digest function name #20574

Closed
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

/** Utility methods to work with {@link Digest}. */
public class DigestUtil {
Expand All @@ -43,13 +45,20 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) {
this.digestFunction = getDigestFunctionFromHashFunction(hashFn);
}

private static final Set<String> digestHashFunctionNames;
christianscott marked this conversation as resolved.
Show resolved Hide resolved
static {
digestHashFunctionNames = new HashSet<>();
for (var value : DigestFunction.Value.values()) {
digestHashFunctionNames.add(value.name());
}
}

private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) {
for (String name : hashFn.getNames()) {
try {
return DigestFunction.Value.valueOf(name);
} catch (IllegalArgumentException e) {
// continue.
if (!digestHashFunctionNames.contains(name)) {
continue;
}
return DigestFunction.Value.valueOf(name);
christianscott marked this conversation as resolved.
Show resolved Hide resolved
}
return DigestFunction.Value.UNKNOWN;
}
Expand Down