Class HashUtils


  • public final class HashUtils
    extends Object
    Shared utility methods for hash computation, pattern parsing, and hex encoding used by both the generate and verify mojos. All methods are designed to be fast and allocation-light for use in large multi-module project traversals.
    • Method Detail

      • resolveAlgorithm

        public static String resolveAlgorithm​(int algorithmBits)
        Resolves the SHA algorithm name from the configured bit width.
        Parameters:
        algorithmBits - the bit width (supported: 256, 384, 512)
        Returns:
        the JCA algorithm name (e.g. "SHA-256")
      • extensionForBits

        public static String extensionForBits​(int algorithmBits)
        Returns the conventional file extension for a given hash algorithm bit width (e.g. ".sha256", ".sha512").
        Parameters:
        algorithmBits - the bit width (supported: 256, 384, 512)
        Returns:
        the extension string including the leading dot
      • computeHash

        public static String computeHash​(Path file,
                                         String algorithm,
                                         boolean normalizeLineEndings)
                                  throws IOException,
                                         NoSuchAlgorithmException
        Computes the hash of a file using the specified algorithm.

        Uses a 64 KiB buffer for streaming reads, keeping heap pressure low even on large files.

        Parameters:
        file - the file to hash
        algorithm - the JCA algorithm name (e.g. "SHA-256")
        normalizeLineEndings - if true, normalizes CRLF to LF in memory before hashing
        Returns:
        the lowercase hex-encoded hash string
        Throws:
        IOException - if the file cannot be read
        NoSuchAlgorithmException - if the algorithm is not available
      • parsePatterns

        public static Set<String> parsePatterns​(String patterns)
        Parses a comma-separated pattern string into a set of trimmed, non-empty patterns.
        Parameters:
        patterns - comma-separated glob patterns (may be null)
        Returns:
        a set of individual pattern strings
      • bytesToHex

        public static String bytesToHex​(byte[] bytes)
        Converts a byte array to a lowercase hexadecimal string using a lookup table. This is significantly faster than String.format("%02x") per byte.
        Parameters:
        bytes - the bytes to encode
        Returns:
        the hex-encoded string