Class DigestUtilsHelper

java.lang.Object
org.zkoss.zk.ui.sys.DigestUtilsHelper

public class DigestUtilsHelper extends 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
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    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[]
    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 String
    encodeHexString(byte[] data)
    Converts an array of bytes into a String representing the hexadecimal values of each byte in order.
    static byte[]
    Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.
    getDigest(String algorithm)
    Returns a MessageDigest for the given algorithm.
    Returns an MD5 MessageDigest.
    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
    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.
    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 Details

    • DigestUtilsHelper

      public DigestUtilsHelper()
  • Method Details

    • 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
    • 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 stream to digest (will not be closed)
      Returns:
      MD5 digest as a hex string
      Throws:
      IOException
    • 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
      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(InputStream data) throws 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:
      IOException
    • getMd5Digest

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

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

      public static byte[] getBytesUtf8(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 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(String data) throws 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:
      IllegalArgumentException
    • decodeHex

      public static byte[] decodeHex(char[] data) throws 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:
      IllegalArgumentException
    • toDigit

      protected static int toDigit(char ch, int index) throws 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:
      IllegalArgumentException