Class DigestUtil

java.lang.Object
com.landawn.abacus.util.DigestUtil

public final class DigestUtil extends Object
Note: copied from Apache commons-codec: https://commons.apache.org/proper/commons-codec. Operations to simplify common MessageDigest tasks. This class is immutable and thread-safe.
Version:
$Id: DigestUtils.java 1634433 2014-10-27 01:10:47Z ggregory $
  • Method Summary

    Modifier and Type
    Method
    Description
    getDigest(String algorithm)
    Returns a MessageDigest for the given algorithm.
    Returns an MD2 MessageDigest.
    Returns an MD5 MessageDigest.
    Returns an SHA-1 digest.
    Returns an SHA-256 digest.
    Returns an SHA-384 digest.
    Returns an SHA-512 digest.
    static byte[]
    md2(byte[] data)
    Calculates the MD2 digest and returns the value as a 16 element byte[].
    static byte[]
    Calculates the MD2 digest and returns the value as a 16 element byte[].
    static byte[]
    md2(String data)
    Calculates the MD2 digest and returns the value as a 16 element byte[].
    static String
    md2Base64(byte[] data)
    Calculates the MD2 digest and returns the value as a base64 encoded string.
    static String
    Calculates the MD2 digest and returns the value as a base64 encoded string.
    static String
    Calculates the MD2 digest and returns the value as a base64 encoded string.
    static String
    md2Hex(byte[] data)
    Calculates the MD2 digest and returns the value as a 32 character hex string.
    static String
    Calculates the MD2 digest and returns the value as a 32 character hex string.
    static String
    md2Hex(String data)
    Calculates the MD2 digest and returns the value as a 32 character hex string.
    static byte[]
    md5(byte[] data)
    Calculates the MD5 digest and returns the value as a 16 element byte[].
    static byte[]
    Calculates the MD5 digest and returns the value as a 16 element byte[].
    static byte[]
    md5(String data)
    Calculates the MD5 digest and returns the value as a 16 element byte[].
    static String
    md5Base64(byte[] data)
    Calculates the MD5 digest and returns the value as a base64 encoded string.
    static String
    Calculates the MD5 digest and returns the value as a base64 encoded string.
    static String
    Calculates the MD5 digest and returns the value as a base64 encoded string.
    static String
    md5Hex(byte[] data)
    Calculates the MD5 digest and returns the value as a 32 character hex string.
    static String
    Calculates the MD5 digest and returns the value as a 32 character hex string.
    static String
    md5Hex(String data)
    Calculates the MD5 digest and returns the value as a 32 character hex string.
    static byte[]
    sha1(byte[] data)
    Calculates the SHA-1 digest and returns the value as a byte[].
    static byte[]
    Calculates the SHA-1 digest and returns the value as a byte[].
    static byte[]
    sha1(String data)
    Calculates the SHA-1 digest and returns the value as a byte[].
    static String
    sha1Base64(byte[] data)
    Calculates the SHA-1 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-1 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-1 digest and returns the value as a base64 encoded string.
    static String
    sha1Hex(byte[] data)
    Calculates the SHA-1 digest and returns the value as a hex string.
    static String
    Calculates the SHA-1 digest and returns the value as a hex string.
    static String
    Calculates the SHA-1 digest and returns the value as a hex string.
    static byte[]
    sha256(byte[] data)
    Calculates the SHA-256 digest and returns the value as a byte[].
    static byte[]
    Calculates the SHA-256 digest and returns the value as a byte[].
    static byte[]
    sha256(String data)
    Calculates the SHA-256 digest and returns the value as a byte[].
    static String
    sha256Base64(byte[] data)
    Calculates the SHA-256 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-256 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-256 digest and returns the value as a base64 encoded string.
    static String
    sha256Hex(byte[] data)
    Calculates the SHA-256 digest and returns the value as a hex string.
    static String
    Calculates the SHA-256 digest and returns the value as a hex string.
    static String
    Calculates the SHA-256 digest and returns the value as a hex string.
    static byte[]
    sha384(byte[] data)
    Calculates the SHA-384 digest and returns the value as a byte[].
    static byte[]
    Calculates the SHA-384 digest and returns the value as a byte[].
    static byte[]
    sha384(String data)
    Calculates the SHA-384 digest and returns the value as a byte[].
    static String
    sha384Base64(byte[] data)
    Calculates the SHA-384 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-384 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-384 digest and returns the value as a base64 encoded string.
    static String
    sha384Hex(byte[] data)
    Calculates the SHA-384 digest and returns the value as a hex string.
    static String
    Calculates the SHA-384 digest and returns the value as a hex string.
    static String
    Calculates the SHA-384 digest and returns the value as a hex string.
    static byte[]
    sha512(byte[] data)
    Calculates the SHA-512 digest and returns the value as a byte[].
    static byte[]
    Calculates the SHA-512 digest and returns the value as a byte[].
    static byte[]
    sha512(String data)
    Calculates the SHA-512 digest and returns the value as a byte[].
    static String
    sha512Base64(byte[] data)
    Calculates the SHA-512 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-512 digest and returns the value as a base64 encoded string.
    static String
    Calculates the SHA-512 digest and returns the value as a base64 encoded string.
    static String
    sha512Hex(byte[] data)
    Calculates the SHA-512 digest and returns the value as a hex string.
    static String
    Calculates the SHA-512 digest and returns the value as a hex string.
    static String
    Calculates the SHA-512 digest and returns the value as a hex string.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getDigest

      public static MessageDigest getDigest(String algorithm)
      Returns a MessageDigest for the given algorithm.
      Parameters:
      algorithm - the name of the algorithm requested. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard algorithm names.
      Returns:
      A digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught.
      See Also:
    • getMd2Digest

      public static MessageDigest getMd2Digest()
      Returns an MD2 MessageDigest.
      Returns:
      An MD2 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because MD2 is a built-in algorithm
      Since:
      1.7
      See Also:
      • DigestUtil.MessageDigestAlgorithms.MD2
    • getMd5Digest

      public static MessageDigest getMd5Digest()
      Returns an MD5 MessageDigest.
      Returns:
      An MD5 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because MD5 is a built-in algorithm
      See Also:
      • DigestUtil.MessageDigestAlgorithms.MD5
    • getSha1Digest

      public static MessageDigest getSha1Digest()
      Returns an SHA-1 digest.
      Returns:
      An SHA-1 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because SHA-1 is a built-in algorithm
      Since:
      1.7
      See Also:
      • DigestUtil.MessageDigestAlgorithms.SHA_1
    • getSha256Digest

      public static MessageDigest getSha256Digest()
      Returns an SHA-256 digest.

      Throws a RuntimeException on JRE versions prior to 1.4.0.

      Returns:
      An SHA-256 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because SHA-256 is a built-in algorithm
      See Also:
      • DigestUtil.MessageDigestAlgorithms.SHA_256
    • getSha384Digest

      public static MessageDigest getSha384Digest()
      Returns an SHA-384 digest.

      Throws a RuntimeException on JRE versions prior to 1.4.0.

      Returns:
      An SHA-384 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because SHA-384 is a built-in algorithm
      See Also:
      • DigestUtil.MessageDigestAlgorithms.SHA_384
    • getSha512Digest

      public static MessageDigest getSha512Digest()
      Returns an SHA-512 digest.

      Throws a RuntimeException on JRE versions prior to 1.4.0.

      Returns:
      An SHA-512 digest instance.
      Throws:
      IllegalArgumentException - when a NoSuchAlgorithmException is caught, which should never happen because SHA-512 is a built-in algorithm
      See Also:
      • DigestUtil.MessageDigestAlgorithms.SHA_512
    • 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
      invalid @link
      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
    • md2Base64

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

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

      public static String md2Base64(String data)
      Calculates the MD2 digest and returns the value as a base64 encoded 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
      invalid @link
      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
    • md5Base64

      public static String md5Base64(byte[] data)
      Calculates the MD5 digest and returns the value as a base64 encoded string.
      Parameters:
      data - Data to digest
      Returns:
      MD5 digest as a hex string
      Since:
      1.7
    • md5Base64

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

      public static String md5Base64(String data)
      Calculates the MD5 digest and returns the value as a base64 encoded string.
      Parameters:
      data - Data to digest
      Returns:
      MD5 digest as a hex string
      Since:
      1.7
    • 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
      invalid @link
      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
    • sha1Base64

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

      public static String sha1Base64(InputStream data) throws IOException
      Calculates the SHA-1 digest and returns the value as a base64 encoded 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
    • sha1Base64

      public static String sha1Base64(String data)
      Calculates the SHA-1 digest and returns the value as a base64 encoded 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
      invalid @link
      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
    • sha256Base64

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

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

      public static String sha256Base64(String data)
      Calculates the SHA-256 digest and returns the value as a base64 encoded string.
      Parameters:
      data - Data to digest
      Returns:
      SHA-256 digest as a hex string
      Since:
      1.7
    • 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
      invalid @link
      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
    • sha384Base64

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

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

      public static String sha384Base64(String data)
      Calculates the SHA-384 digest and returns the value as a base64 encoded string.
      Parameters:
      data - Data to digest
      Returns:
      SHA-384 digest as a hex string
      Since:
      1.7
    • 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
      invalid @link
      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
    • sha512Base64

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

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

      public static String sha512Base64(String data)
      Calculates the SHA-512 digest and returns the value as a base64 encoded string.
      Parameters:
      data - Data to digest
      Returns:
      SHA-512 digest as a hex string
      Since:
      1.7