Package com.intsof.ai.build.integrity
Class ArtifactDigestsUtils
- java.lang.Object
-
- com.intsof.ai.build.integrity.ArtifactDigestsUtils
-
public final class ArtifactDigestsUtils extends Object
Utility methods for artifact digest computation, artifact discovery, and path validation.Unlike
HashUtilswhich supports line-ending normalization for text files, this class provides streaming-only hash computation suitable for binary artifacts (JARs, WARs, ZIPs). Binary artifacts must never be loaded into heap memory in their entirety.All methods are designed to be fast and allocation-light for use in large multi-module project builds.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classArtifactDigestsUtils.PathTraversalExceptionException thrown when a path traversal attack is detected.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringbytesToHex(byte[] bytes)Converts a byte array to a lowercase hexadecimal string using a lookup table.static StringcomputeHashStreaming(Path file, String algorithm)Computes the hash of a file using the specified algorithm using streaming only.static StringextensionForAlgorithm(String algorithm)Returns the conventional file extension for a given hash algorithm (e.g. ".sha256", ".md5").static booleanisCompromisedAlgorithm(String algorithm)Checks whether a given algorithm name is considered compromised.static voidvalidateAlgorithm(String algorithm)Validates that an algorithm is available in the current JVM's MessageDigest provider.static PathvalidateArtifactPath(Path artifactPath, Path buildDirectory)Validates that an artifact path is within the project's build directory using canonical path enforcement.
-
-
-
Method Detail
-
isCompromisedAlgorithm
public static boolean isCompromisedAlgorithm(String algorithm)
Checks whether a given algorithm name is considered compromised.- Parameters:
algorithm- the algorithm name (e.g. "MD5", "SHA-1")- Returns:
- true if the algorithm is compromised and requires explicit opt-in
-
validateAlgorithm
public static void validateAlgorithm(String algorithm) throws NoSuchAlgorithmException
Validates that an algorithm is available in the current JVM's MessageDigest provider.- Parameters:
algorithm- the JCA algorithm name (e.g. "SHA-256", "MD5")- Throws:
NoSuchAlgorithmException- if the algorithm is not available
-
extensionForAlgorithm
public static String extensionForAlgorithm(String algorithm)
Returns the conventional file extension for a given hash algorithm (e.g. ".sha256", ".md5").- Parameters:
algorithm- the JCA algorithm name (e.g. "SHA-256", "MD5")- Returns:
- the extension string including the leading dot (e.g. ".sha256")
-
computeHashStreaming
public static String computeHashStreaming(Path file, String algorithm) throws IOException, NoSuchAlgorithmException
Computes the hash of a file using the specified algorithm using streaming only. This method never loads the entire file into memory, making it safe for binary artifacts of any size.Security note: This method does not perform line-ending normalization. For binary artifacts (JAR, WAR, ZIP), this is the correct behavior.
- Parameters:
file- the file to hashalgorithm- the JCA algorithm name (e.g. "SHA-256", "MD5")- Returns:
- the lowercase hex-encoded hash string
- Throws:
IOException- if the file cannot be readNoSuchAlgorithmException- if the algorithm is not available
-
validateArtifactPath
public static Path validateArtifactPath(Path artifactPath, Path buildDirectory) throws IOException
Validates that an artifact path is within the project's build directory using canonical path enforcement. This prevents path traversal attacks where an artifact symlink points outside the intended directory.Implementation:
- Resolve the artifact path using
NOFOLLOW_LINKSto avoid following symlinks during resolution - If the path is a symlink, inspect the link target using
Files.readSymbolicLink(java.nio.file.Path) - Verify the canonical path is a descendant of
buildDirectory
- Parameters:
artifactPath- the path to the artifactbuildDirectory- the project's build directory (e.g.target/)- Returns:
- the validated canonical path
- Throws:
ArtifactDigestsUtils.PathTraversalException- if the path escapes the build directoryIOException- if an I/O error occurs during path resolution
- Resolve the artifact path using
-
bytesToHex
public static String bytesToHex(byte[] bytes)
Converts a byte array to a lowercase hexadecimal string using a lookup table. This is significantly faster thanString.format("%02x")per byte.- Parameters:
bytes- the bytes to encode- Returns:
- the hex-encoded string
-
-