Class DigestUtilsHelper


  • public class DigestUtilsHelper
    extends java.lang.Object
    A helper class for class DigestUtils for md5 encoding, internal use only. The following implementation for md5 encoding is referred from https://commons.apache.org/proper/commons-codec/archives/1.8/index.html
    Since:
    8.0.2
    Author:
    wenning
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decodeHex​(char[] data)
      Converts an array of characters representing hexadecimal values into an array of bytes of those same values.
      static byte[] decodeHexString​(java.lang.String data)
      Converts a String into an array of bytes.
      static char[] encodeHex​(byte[] data)
      Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
      static char[] encodeHex​(byte[] data, boolean toLowerCase)
      Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
      protected static char[] encodeHex​(byte[] data, char[] toDigits)
      Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
      static java.lang.String encodeHexString​(byte[] data)
      Converts an array of bytes into a String representing the hexadecimal values of each byte in order.
      static byte[] getBytesUtf8​(java.lang.String string)
      Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.
      static java.security.MessageDigest getDigest​(java.lang.String algorithm)
      Returns a MessageDigest for the given algorithm.
      static java.security.MessageDigest getMd5Digest()
      Returns an MD5 MessageDigest.
      static byte[] md5​(byte[] data)
      Calculates the MD5 digest and returns the value as a 16 element byte[].
      static byte[] md5​(java.io.InputStream data)
      Calculates the MD5 digest and returns the value as a 16 element byte[].
      static byte[] md5​(java.lang.String data)
      Calculates the MD5 digest and returns the value as a 16 element byte[].
      static java.lang.String md5Hex​(java.io.InputStream data)
      Calculates the MD5 digest and returns the value as a 32 character hex string.
      static java.lang.String md5Hex​(java.lang.String data)
      Calculates the MD5 digest and returns the value as a 32 character hex string.
      protected static int toDigit​(char ch, int index)
      Converts a hexadecimal character to an integer.
      • Methods inherited from class java.lang.Object

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

      • DigestUtilsHelper

        public DigestUtilsHelper()
    • Method Detail

      • md5Hex

        public static java.lang.String md5Hex​(java.lang.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
      • md5Hex

        public static java.lang.String md5Hex​(java.io.InputStream data)
                                       throws java.io.IOException
        Calculates the MD5 digest and returns the value as a 32 character hex string.
        Parameters:
        data - data stream to digest (will not be closed)
        Returns:
        MD5 digest as a hex string
        Throws:
        java.io.IOException
      • md5

        public static byte[] md5​(java.lang.String 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​(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​(java.io.InputStream data)
                          throws java.io.IOException
        Calculates the MD5 digest and returns the value as a 16 element byte[].
        Parameters:
        data - data stream to digest (will not be closed)
        Returns:
        MD5 digest
        Throws:
        java.io.IOException
      • getMd5Digest

        public static java.security.MessageDigest getMd5Digest()
        Returns an MD5 MessageDigest.
        Returns:
        An MD5 digest instance.
      • getDigest

        public static java.security.MessageDigest getDigest​(java.lang.String algorithm)
        Returns a MessageDigest for the given algorithm.
        Parameters:
        algorithm -
        Returns:
        An MD5 digest instance.
        Throws:
        java.lang.IllegalArgumentException
      • getBytesUtf8

        public static byte[] getBytesUtf8​(java.lang.String string)
        Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.
        Parameters:
        string - the String to encode, may be null
        Returns:
        encoded bytes, or null if the input string was null
      • encodeHexString

        public static java.lang.String encodeHexString​(byte[] data)
        Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned String will be double the length of the passed array, as it takes two characters to represent any given byte.
        Parameters:
        data - a byte[] to convert to Hex characters
        Returns:
        A String containing hexadecimal characters
      • encodeHex

        public static char[] encodeHex​(byte[] data)
        Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
        Parameters:
        data - a byte[] to convert to Hex characters
        Returns:
        A char[] containing hexadecimal characters
      • encodeHex

        public static char[] encodeHex​(byte[] data,
                                       boolean toLowerCase)
        Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
        Parameters:
        data - a byte[] to convert to Hex characters
        toLowerCase - true converts to lowercase, false to uppercase
        Returns:
        A char[] containing hexadecimal characters
      • encodeHex

        protected static char[] encodeHex​(byte[] data,
                                          char[] toDigits)
        Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
        Parameters:
        data - a byte[] to convert to Hex characters
        toDigits - the output alphabet
        Returns:
        A char[] containing hexadecimal characters
      • decodeHexString

        public static byte[] decodeHexString​(java.lang.String data)
                                      throws java.lang.IllegalArgumentException
        Converts a String into an array of bytes.
        Parameters:
        data - a byte[] to convert to Hex characters
        Returns:
        A String containing hexadecimal characters
        Throws:
        java.lang.IllegalArgumentException
      • decodeHex

        public static byte[] decodeHex​(char[] data)
                                throws java.lang.IllegalArgumentException
        Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
        Parameters:
        data - An array of characters containing hexadecimal digits
        Returns:
        A byte array containing binary data decoded from the supplied char array.
        Throws:
        java.lang.IllegalArgumentException
      • toDigit

        protected static int toDigit​(char ch,
                                     int index)
                              throws java.lang.IllegalArgumentException
        Converts a hexadecimal character to an integer.
        Parameters:
        ch - A character to convert to an integer digit
        index - The index of the character in the source
        Returns:
        An integer
        Throws:
        java.lang.IllegalArgumentException