Class DigestUtils


  • public class DigestUtils
    extends java.lang.Object
    Security data manipulation utilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static char[] HEXDIGITS
      General regex pattern to extract comma separated name-value components.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DigestUtils()
      Private constructor to ensure that the class acts as a true utility class i.e.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static char[] digest​(char[] target, java.lang.String algorithm)
      Returns the digest of the target string.
      static java.lang.String digest​(java.lang.String target, java.lang.String algorithm)
      Returns the digest of the target string.
      static byte[] toHMacSha1​(java.lang.String source, byte[] secretKey)
      Converts a source string to its HMAC/SHA-1 value.
      static byte[] toHMacSha1​(java.lang.String source, java.lang.String secretKey)
      Converts a source string to its HMAC/SHA-1 value.
      static byte[] toHMacSha256​(java.lang.String source, byte[] secretKey)
      Converts a source string to its HMAC/SHA256 value.
      static byte[] toHMacSha256​(java.lang.String source, java.lang.String secretKey)
      Converts a source string to its HMAC/SHA256 value.
      static java.lang.String toHttpDigest​(java.lang.String identifier, char[] secret, java.lang.String realm)
      Return the HTTP DIGEST hashed secret.
      static java.lang.String toMd5​(java.lang.String target)
      Returns the MD5 digest of the target string.
      static java.lang.String toMd5​(java.lang.String target, java.lang.String charsetName)
      Returns the MD5 digest of target string.
      static java.lang.String toSha1​(java.lang.String target)
      Returns the SHA1 digest of the target string.
      static java.lang.String toSha1​(java.lang.String target, java.lang.String charsetName)
      Returns the SHA1 digest of target string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HEXDIGITS

        private static final char[] HEXDIGITS
        General regex pattern to extract comma separated name-value components. This pattern captures one name and value per match(), and is repeatedly applied to the input string to extract all components. Must handle both quoted and unquoted values as RFC2617 isn't consistent in this respect. Pattern is immutable and thread-safe so reuse one static instance.
    • Constructor Detail

      • DigestUtils

        private DigestUtils()
        Private constructor to ensure that the class acts as a true utility class i.e. it isn't instantiable and extensible.
    • Method Detail

      • digest

        public static char[] digest​(char[] target,
                                    java.lang.String algorithm)
        Returns the digest of the target string. Target is decoded to bytes using the US-ASCII charset. Supports MD5 and SHA-1 algorithms.
        Parameters:
        target - The string to encode.
        algorithm - The digest algorithm to use.
        Returns:
        The digest of the target string.
      • digest

        public static java.lang.String digest​(java.lang.String target,
                                              java.lang.String algorithm)
        Returns the digest of the target string. Target is decoded to bytes using the US-ASCII charset. Supports MD5 and SHA-1 algorithms.
        Parameters:
        target - The string to encode.
        algorithm - The digest algorithm to use.
        Returns:
        The digest of the target string.
      • toHMacSha1

        public static byte[] toHMacSha1​(java.lang.String source,
                                        byte[] secretKey)
        Converts a source string to its HMAC/SHA-1 value.
        Parameters:
        source - The source string to convert.
        secretKey - The secret key to use for conversion.
        Returns:
        The HMac value of the source string.
      • toHMacSha1

        public static byte[] toHMacSha1​(java.lang.String source,
                                        java.lang.String secretKey)
        Converts a source string to its HMAC/SHA-1 value.
        Parameters:
        source - The source string to convert.
        secretKey - The secret key to use for conversion.
        Returns:
        The HMac value of the source string.
      • toHMacSha256

        public static byte[] toHMacSha256​(java.lang.String source,
                                          byte[] secretKey)
        Converts a source string to its HMAC/SHA256 value.
        Parameters:
        source - The source string to convert.
        secretKey - The secret key to use for conversion.
        Returns:
        The HMac value of the source string.
      • toHMacSha256

        public static byte[] toHMacSha256​(java.lang.String source,
                                          java.lang.String secretKey)
        Converts a source string to its HMAC/SHA256 value.
        Parameters:
        source - The source string to convert.
        secretKey - The secret key to use for conversion.
        Returns:
        The HMac value of the source string.
      • toHttpDigest

        public static java.lang.String toHttpDigest​(java.lang.String identifier,
                                                    char[] secret,
                                                    java.lang.String realm)
        Return the HTTP DIGEST hashed secret. It concatenates the identifier, realm and secret, separated by a comma and digest them using MD5.
        Parameters:
        identifier - The user identifier to hash.
        secret - The user secret.
        realm - The authentication realm.
        Returns:
        A hash of the user name, realm, and password, specified as A1 in section 3.2.2.2 of RFC2617, or null if the identifier has no corresponding secret.
      • toMd5

        public static java.lang.String toMd5​(java.lang.String target)
        Returns the MD5 digest of the target string. Target is decoded to bytes using the US-ASCII charset. The returned hexadecimal String always contains 32 lowercase alphanumeric characters. For example, if target is "HelloWorld", this method returns "68e109f0f40ca72a15e05cc22786f8e6".
        Parameters:
        target - The string to encode.
        Returns:
        The MD5 digest of the target string.
      • toMd5

        public static java.lang.String toMd5​(java.lang.String target,
                                             java.lang.String charsetName)
                                      throws java.io.UnsupportedEncodingException
        Returns the MD5 digest of target string. Target is decoded to bytes using the named charset. The returned hexadecimal String always contains 32 lowercase alphanumeric characters. For example, if target is "HelloWorld", this method returns "68e109f0f40ca72a15e05cc22786f8e6".
        Parameters:
        target - The string to encode.
        charsetName - The character set.
        Returns:
        The MD5 digest of the target string.
        Throws:
        java.io.UnsupportedEncodingException
      • toSha1

        public static java.lang.String toSha1​(java.lang.String target)
        Returns the SHA1 digest of the target string. Target is decoded to bytes using the US-ASCII charset.
        Parameters:
        target - The string to encode.
        Returns:
        The MD5 digest of the target string.
      • toSha1

        public static java.lang.String toSha1​(java.lang.String target,
                                              java.lang.String charsetName)
                                       throws java.io.UnsupportedEncodingException
        Returns the SHA1 digest of target string. Target is decoded to bytes using the named charset.
        Parameters:
        target - The string to encode.
        charsetName - The character set.
        Returns:
        The SHA1 digest of the target string.
        Throws:
        java.io.UnsupportedEncodingException