Class DigestUtils


  • public class DigestUtils
    extends Object
    Operations to simplify common MessageDigest tasks. This class is immutable and thread-safe. However the MessageDigest instances it creates generally won't be.

    The MessageDigestAlgorithms class provides constants for standard digest algorithms that can be used with the getDigest(String) method and other methods that require the Digest algorithm name.

    Note: the class has short-hand methods for all the algorithms present as standard in Java 6. This approach requires lots of methods for each algorithm, and quickly becomes unwieldy. The following code works with all algorithms:

     import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
     ...
     byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest);
     String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
     
    Version:
    $Id: DigestUtils.java 1811344 2017-10-06 15:19:57Z ggregory $
    See Also:
    MessageDigestAlgorithms
    • Method Detail

      • digest

        public static byte[] digest​(MessageDigest messageDigest,
                                    byte[] data)
        Reads through a byte array and returns the digest for the data. Provided for symmetry with other methods.
        Parameters:
        messageDigest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Since:
        1.11
      • digest

        public static byte[] digest​(MessageDigest messageDigest,
                                    ByteBuffer data)
        Reads through a ByteBuffer and returns the digest for the data
        Parameters:
        messageDigest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Since:
        1.11
      • digest

        public static byte[] digest​(MessageDigest messageDigest,
                                    File data)
                             throws IOException
        Reads through a File and returns the digest for the data
        Parameters:
        messageDigest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11
      • digest

        public static byte[] digest​(MessageDigest messageDigest,
                                    InputStream data)
                             throws IOException
        Reads through an InputStream and returns the digest for the data
        Parameters:
        messageDigest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11 (was private)
      • md2

        public static byte[] md2​(byte[] data)
        Calculates the MD2 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest
        Returns:
        MD2 digest
        Since:
        1.7
      • md2

        public static byte[] md2​(InputStream data)
                          throws IOException
        Calculates the MD2 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest
        Returns:
        MD2 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.7
      • md2

        public static byte[] md2​(String data)
        Calculates the MD2 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        MD2 digest
        Since:
        1.7
      • md2Hex

        public static String md2Hex​(byte[] data)
        Calculates the MD2 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD2 digest as a hex string
        Since:
        1.7
      • md2Hex

        public static String md2Hex​(InputStream data)
                             throws IOException
        Calculates the MD2 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD2 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.7
      • md2Hex

        public static String md2Hex​(String data)
        Calculates the MD2 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD2 digest as a hex string
        Since:
        1.7
      • md5

        public static byte[] md5​(byte[] data)
        Calculates the MD5 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest
        Returns:
        MD5 digest
      • md5

        public static byte[] md5​(InputStream data)
                          throws IOException
        Calculates the MD5 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest
        Returns:
        MD5 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • md5

        public static byte[] md5​(String data)
        Calculates the MD5 digest and returns the value as a 16 element byte[].
        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        MD5 digest
      • md5Hex

        public static String md5Hex​(byte[] data)
        Calculates the MD5 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD5 digest as a hex string
      • md5Hex

        public static String md5Hex​(InputStream data)
                             throws IOException
        Calculates the MD5 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD5 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • md5Hex

        public static String md5Hex​(String data)
        Calculates the MD5 digest and returns the value as a 32 character hex string.
        Parameters:
        data - Data to digest
        Returns:
        MD5 digest as a hex string
      • sha

        @Deprecated
        public static byte[] sha​(byte[] data)
        Deprecated.
        (1.11) Use sha1(byte[])
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest
      • sha

        @Deprecated
        public static byte[] sha​(InputStream data)
                          throws IOException
        Deprecated.
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha

        @Deprecated
        public static byte[] sha​(String data)
        Deprecated.
        (1.11) Use sha1(String)
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest
      • sha1

        public static byte[] sha1​(byte[] data)
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest
        Since:
        1.7
      • sha1

        public static byte[] sha1​(InputStream data)
                           throws IOException
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.7
      • sha1

        public static byte[] sha1​(String data)
        Calculates the SHA-1 digest and returns the value as a byte[].
        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        SHA-1 digest
      • sha1Hex

        public static String sha1Hex​(byte[] data)
        Calculates the SHA-1 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest as a hex string
        Since:
        1.7
      • sha1Hex

        public static String sha1Hex​(InputStream data)
                              throws IOException
        Calculates the SHA-1 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.7
      • sha1Hex

        public static String sha1Hex​(String data)
        Calculates the SHA-1 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest as a hex string
        Since:
        1.7
      • sha256

        public static byte[] sha256​(byte[] data)
        Calculates the SHA-256 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest
        Since:
        1.4
      • sha256

        public static byte[] sha256​(InputStream data)
                             throws IOException
        Calculates the SHA-256 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha256

        public static byte[] sha256​(String data)
        Calculates the SHA-256 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        SHA-256 digest
        Since:
        1.4
      • sha256Hex

        public static String sha256Hex​(byte[] data)
        Calculates the SHA-256 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest as a hex string
        Since:
        1.4
      • sha256Hex

        public static String sha256Hex​(InputStream data)
                                throws IOException
        Calculates the SHA-256 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha256Hex

        public static String sha256Hex​(String data)
        Calculates the SHA-256 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest as a hex string
        Since:
        1.4
      • sha384

        public static byte[] sha384​(byte[] data)
        Calculates the SHA-384 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-384 digest
        Since:
        1.4
      • sha384

        public static byte[] sha384​(InputStream data)
                             throws IOException
        Calculates the SHA-384 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-384 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha384

        public static byte[] sha384​(String data)
        Calculates the SHA-384 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        SHA-384 digest
        Since:
        1.4
      • sha384Hex

        public static String sha384Hex​(byte[] data)
        Calculates the SHA-384 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-384 digest as a hex string
        Since:
        1.4
      • sha384Hex

        public static String sha384Hex​(InputStream data)
                                throws IOException
        Calculates the SHA-384 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-384 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha384Hex

        public static String sha384Hex​(String data)
        Calculates the SHA-384 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-384 digest as a hex string
        Since:
        1.4
      • sha512

        public static byte[] sha512​(byte[] data)
        Calculates the SHA-512 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-512 digest
        Since:
        1.4
      • sha512

        public static byte[] sha512​(InputStream data)
                             throws IOException
        Calculates the SHA-512 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-512 digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha512

        public static byte[] sha512​(String data)
        Calculates the SHA-512 digest and returns the value as a byte[].

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest; converted to bytes using StringUtils.getBytesUtf8(String)
        Returns:
        SHA-512 digest
        Since:
        1.4
      • sha512Hex

        public static String sha512Hex​(byte[] data)
        Calculates the SHA-512 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-512 digest as a hex string
        Since:
        1.4
      • sha512Hex

        public static String sha512Hex​(InputStream data)
                                throws IOException
        Calculates the SHA-512 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-512 digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.4
      • sha512Hex

        public static String sha512Hex​(String data)
        Calculates the SHA-512 digest and returns the value as a hex string.

        Throws a RuntimeException on JRE versions prior to 1.4.0.

        Parameters:
        data - Data to digest
        Returns:
        SHA-512 digest as a hex string
        Since:
        1.4
      • shaHex

        @Deprecated
        public static String shaHex​(byte[] data)
        Deprecated.
        (1.11) Use sha1Hex(byte[])
        Calculates the SHA-1 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest as a hex string
      • shaHex

        @Deprecated
        public static String shaHex​(String data)
        Deprecated.
        (1.11) Use sha1Hex(String)
        Calculates the SHA-1 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-1 digest as a hex string
      • updateDigest

        public static MessageDigest updateDigest​(MessageDigest digest,
                                                 File data)
                                          throws IOException
        Reads through a File and updates the digest for the data
        Parameters:
        digest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11
      • updateDigest

        public static MessageDigest updateDigest​(MessageDigest digest,
                                                 InputStream data)
                                          throws IOException
        Reads through an InputStream and updates the digest for the data
        Parameters:
        digest - The MessageDigest to use (e.g. MD5)
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.8
      • isAvailable

        public static boolean isAvailable​(String messageDigestAlgorithm)
        Test whether the algorithm is supported.
        Parameters:
        messageDigestAlgorithm - the algorithm name
        Returns:
        true if the algorithm can be found
        Since:
        1.11
      • getMessageDigest

        public MessageDigest getMessageDigest()
        Returns the message digest instance.
        Returns:
        the message digest instance
        Since:
        1.11
      • digest

        public byte[] digest​(byte[] data)
        Reads through a byte array and returns the digest for the data.
        Parameters:
        data - Data to digest
        Returns:
        the digest
        Since:
        1.11
      • digest

        public byte[] digest​(String data)
        Reads through a byte array and returns the digest for the data.
        Parameters:
        data - Data to digest treated as UTF-8 string
        Returns:
        the digest
        Since:
        1.11
      • digest

        public byte[] digest​(ByteBuffer data)
        Reads through a ByteBuffer and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest
        Since:
        1.11
      • digest

        public byte[] digest​(File data)
                      throws IOException
        Reads through a File and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11
      • digest

        public byte[] digest​(InputStream data)
                      throws IOException
        Reads through an InputStream and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11
      • digestAsHex

        public String digestAsHex​(byte[] data)
        Reads through a byte array and returns the digest for the data.
        Parameters:
        data - Data to digest
        Returns:
        the digest as a hex string
        Since:
        1.11
      • digestAsHex

        public String digestAsHex​(String data)
        Reads through a byte array and returns the digest for the data.
        Parameters:
        data - Data to digest treated as UTF-8 string
        Returns:
        the digest as a hex string
        Since:
        1.11
      • digestAsHex

        public String digestAsHex​(ByteBuffer data)
        Reads through a ByteBuffer and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest as a hex string
        Since:
        1.11
      • digestAsHex

        public String digestAsHex​(File data)
                           throws IOException
        Reads through a File and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11
      • digestAsHex

        public String digestAsHex​(InputStream data)
                           throws IOException
        Reads through an InputStream and returns the digest for the data
        Parameters:
        data - Data to digest
        Returns:
        the digest as a hex string
        Throws:
        IOException - On error reading from the stream
        Since:
        1.11