Class MessageDigestUtils


  • public final class MessageDigestUtils
    extends Object
    Thread-safety: Each digest is kept in a ThreadLocal. This class is intended for use on long-lived threads (e.g., Netty event loops). If you call it from a short-lived or unbounded thread pool, you may inadvertently retain one MessageDigest instance per thread, leading to memory leaks.
    • Method Detail

      • pooledMessageDigest

        public static MessageDigest pooledMessageDigest​(String algorithm)
        Returns a pooled MessageDigest instance for the given algorithm name. Supported: "MD5", "SHA-1", "SHA-256", "SHA-512/256" (and aliases). The returned instance is thread-local and reset before use.
        Parameters:
        algorithm - the algorithm name (e.g., "MD5", "SHA-256", "SHA-512/256")
        Returns:
        a reset MessageDigest instance for the algorithm
        Throws:
        IllegalArgumentException - if the algorithm is not supported
      • bytesToHex

        public static String bytesToHex​(byte[] bytes)
        Converts a byte array to a lower-case hexadecimal String. Locale-safe and allocation-free except for the final char[] → String copy.
        Parameters:
        bytes - the byte array to convert (must not be null)
        Returns:
        2×length lower-case hex string
        Throws:
        IllegalArgumentException - if bytes is null
      • pooledMd5MessageDigest

        public static MessageDigest pooledMd5MessageDigest()
        Returns:
        a pooled, reset MessageDigest for MD5
      • pooledSha1MessageDigest

        public static MessageDigest pooledSha1MessageDigest()
        Returns:
        a pooled, reset MessageDigest for SHA-1
      • pooledSha256MessageDigest

        public static MessageDigest pooledSha256MessageDigest()
        Returns:
        a pooled, reset MessageDigest for SHA-256
      • pooledSha512_256MessageDigest

        public static MessageDigest pooledSha512_256MessageDigest()
        Returns:
        a pooled, reset MessageDigest for SHA-512/256