Class Digest

  • All Implemented Interfaces:
    Cloneable

    public class Digest
    extends Object
    implements Cloneable
    A wrapper class over MessageDigest with many update methods in a fluent style, meaning update methods can be chained.

    update methods are provided for all the primitive types and String, and their array types. In addition, updateJson(String) has been available since the version 1.2 which updates the digest with the content of the given JSON. Note that update(String) and updateJson(String) are different.

    getInstanceXXX methods (where XXX is a pre-defined algorithm name with hyphens removed) such as getInstanceSHA1() are provided. They won't throw NoSuchAlgorithmException.

     
     // Compute SHA-1 of "Hello, world.".
     // 'digest' will have "2ae01472317d1935a84797ec1983ae243fc6aa28".
     String digest = Digest.getInstanceSHA1()
                     .update("Hello, world.")
                     .digestAsString();
    
     // Compute SHA-1 of "Hello, world." and get the result as Base64.
     // 'digest' will have "KuAUcjF9GTWoR5fsGYOuJD/Gqig=".
     String digest = Digest.getInstanceSHA1()
                     .update("Hello, world.")
                     .digestAsString(new Base64());
    
     // Compute SHA-1 of two JSONs.
     // 'result1' and 'result2' will have the same value.
     String json1 = "{ \"key1\":\"value1\", \"key2\":\"value2\" }";
     String json2 = "{ \"key2\":\"value2\", \"key1\":\"value1\" }";
     String result1 = Digest.getInstanceSHA1().updateJson(json1).digestAsString();
     String result2 = Digest.getInstanceSHA1().updateJson(json2).digestAsString();
     
    Since:
    4.23
    Author:
    Takahiko Kawasaki
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Digest.Feature
      Features to control behaviors.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static String bytesToHex​(byte[] bytes)
      Convert the given byte array to a hex string.
      Object clone()
      Get a clone of this Digest instance.
      byte[] digest()
      Complete the hash computation.
      byte[] digest​(byte[] input)
      Perform the final update with the given byte array, and then complete the hash computation.
      int digest​(byte[] output, int offset, int length)
      Complete the hash computation.
      String digestAsString()
      Complete the hash computation and get the resulting hash value as a hex string.
      String digestAsString​(byte[] input)
      Perform the final update with the given byte array, and then complete the hash computation and get the resulting hash value as a hex string.
      String digestAsString​(byte[] input, org.apache.commons.codec.BinaryEncoder encoder)
      Perform the final update with the given byte array, and then complete the hash computation and get the resulting hash value as a string.
      String digestAsString​(org.apache.commons.codec.BinaryEncoder encoder)
      Complete the hash computation and get the resulting hash value as a string.
      String getAlgorithm()
      Get the algorithm name.
      int getDigestLength()
      Get the length of the digest in bytes.
      static Digest getInstance​(String algorithm)
      Create a Digest instance with the specified algorithm.
      static Digest getInstance​(String algorithm, String provider)
      Create a Digest instance with the specified algorithm.
      static Digest getInstance​(String algorithm, Provider provider)
      Create a Digest instance with the specified algorithm.
      static Digest getInstanceMD2()
      Create a Digest instance that implements MD2.
      static Digest getInstanceMD5()
      Create a Digest instance that implements MD5.
      static Digest getInstanceSHA1()
      Create a Digest instance that implements SHA-1.
      static Digest getInstanceSHA256()
      Create a Digest instance that implements SHA-256.
      static Digest getInstanceSHA384()
      Create a Digest instance that implements SHA-384.
      static Digest getInstanceSHA512()
      Create a Digest instance that implements SHA-512.
      Provider getProvider()
      Get the provider.
      MessageDigest getWrappedMessageDigest()
      Get the wrapped MessageDigest instance.
      boolean isEnabled​(Digest.Feature feature)
      Check if the specified feature is enabled.
      Digest reset()
      Reset the wrapped MessageDigest instance.
      Digest setEnabled​(Digest.Feature feature, boolean enabled)
      Enable or disable the specified feature.
      Digest update​(boolean input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(boolean[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(boolean[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(byte input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(byte[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(byte[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(char input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(char[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(char[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(double input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(double[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(double[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(float input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(float[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(float[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(int input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(int[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(int[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(long input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(long[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(long[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(short input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(short[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(short[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Boolean input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Boolean[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Boolean[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Character input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Character[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Character[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Iterable<?> input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Number number)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Object input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(Object... input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(String input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(String[] input)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(String[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest update​(ByteBuffer input)
      Update the wrapped MessageDigest object with the given input data.
      <TNumber extends Number>
      Digest
      update​(TNumber[] input)
      Update the wrapped MessageDigest object with the given input data.
      <TNumber extends Number>
      Digest
      update​(TNumber[] input, int offset, int length)
      Update the wrapped MessageDigest object with the given input data.
      Digest updateJson​(String json)
      Update the wrapped MessageDigest object with the given JSON.
    • Method Detail

      • getInstanceMD2

        public static Digest getInstanceMD2()
        Create a Digest instance that implements MD2.
        Returns:
        A Digest instance that implements MD2.
      • getInstanceMD5

        public static Digest getInstanceMD5()
        Create a Digest instance that implements MD5.
        Returns:
        A Digest instance that implements MD5.
      • getInstanceSHA1

        public static Digest getInstanceSHA1()
        Create a Digest instance that implements SHA-1.
        Returns:
        A Digest instance that implements SHA-1.
      • getInstanceSHA256

        public static Digest getInstanceSHA256()
        Create a Digest instance that implements SHA-256.
        Returns:
        A Digest instance that implements SHA-256.
      • getInstanceSHA384

        public static Digest getInstanceSHA384()
        Create a Digest instance that implements SHA-384.
        Returns:
        A Digest instance that implements SHA-384.
      • getInstanceSHA512

        public static Digest getInstanceSHA512()
        Create a Digest instance that implements SHA-512.
        Returns:
        A Digest instance that implements SHA-512.
      • getAlgorithm

        public String getAlgorithm()
        Get the algorithm name.

        This method just calls getAlgorithm() of the wrapped MessageDigest instance.

        Returns:
        Algorithm name.
      • getDigestLength

        public int getDigestLength()
        Get the length of the digest in bytes.

        This method just calls getDigestLength() of the wrapped MessageDigest instance.

        Returns:
        Length of the digest in bytes.
      • getProvider

        public Provider getProvider()
        Get the provider.

        This method just calls getProvider() of the wrapped MessageDigest instance.

        Returns:
        Provider.
      • getWrappedMessageDigest

        public MessageDigest getWrappedMessageDigest()
        Get the wrapped MessageDigest instance.
        Returns:
        The MessageDigest instance that has been given to the constructor.
      • digest

        public byte[] digest()
        Complete the hash computation. The digest is reset after this call is made.

        This method just calls digest() method of the wrapped MessageDigest instance.

        Returns:
        The resulting hash value.
      • digest

        public byte[] digest​(byte[] input)
        Perform the final update with the given byte array, and then complete the hash computation. The digest is reset after this call is made.

        This method just calls digest(byte[]) method of the wrapped MessageDigest instance.

        Parameters:
        input - Byte array used for the last update.
        Returns:
        The resulting hash value.
      • digest

        public int digest​(byte[] output,
                          int offset,
                          int length)
                   throws DigestException
        Complete the hash computation. The digest is reset after this call is made.

        This method just calls digest(byte[], int, int) method of the wrapped MessageDigest instance.

        Parameters:
        output - Output buffer for the computed digest.
        offset - Offset into the output buffer to begin storing the digest.
        length - Number of bytes within the output buffer allotted for the digest.
        Returns:
        The resulting hash value.
        Throws:
        DigestException - The digest method of the MessageDigest class failed.
      • digestAsString

        public String digestAsString()
        Complete the hash computation and get the resulting hash value as a hex string. The digest is reset after this call is made.

        This method calls digest() method and converts the result to a String object.

        Returns:
        The result hash value represented in a hex String.
      • digestAsString

        public String digestAsString​(byte[] input)
        Perform the final update with the given byte array, and then complete the hash computation and get the resulting hash value as a hex string. The digest is reset after this call is made.

        This method calls digest(byte[]) method and converts the result to a String object.

        Parameters:
        input - Byte array used for the last update.
        Returns:
        The result hash value represented in a hex String.
      • digestAsString

        public String digestAsString​(org.apache.commons.codec.BinaryEncoder encoder)
        Complete the hash computation and get the resulting hash value as a string. The given encoder is used to convert the digest value to a string.

        This method is an alias of digestAsString((byte[])null, encoder).

        Parameters:
        encoder - Encoder to convert a digest value to a byte array whose elements are printable characters. For example, Base64.
        Returns:
        The result hash value encoded by the encoder.
        Throws:
        RuntimeException - If the encoder throws EncoderException, a RuntimeException wrapping the EncoderException is thrown.
      • digestAsString

        public String digestAsString​(byte[] input,
                                     org.apache.commons.codec.BinaryEncoder encoder)
        Perform the final update with the given byte array, and then complete the hash computation and get the resulting hash value as a string. The given encoder is used to convert the digest value to a string.
        Parameters:
        input - Byte array used for the last update. If null is given, it is just ignored.
        encoder - Encoder to convert a digest value to a byte array whose elements are printable characters. For example, Base64.
        Returns:
        The result hash value encoded by the encoder.
        Throws:
        IllegalArgumentException - encoder is null.
        RuntimeException - If the encoder throws EncoderException, a RuntimeException wrapping the EncoderException is thrown.
      • reset

        public Digest reset()
        Reset the wrapped MessageDigest instance.
        Returns:
        this object.
      • update

        public Digest update​(byte input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(byte) method of the wrapped MessageDigest instance.

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(byte[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(byte[]) method of the wrapped MessageDigest instance.

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(byte[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(byte[], int, int) method of the wrapped MessageDigest instance.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
      • update

        public Digest update​(ByteBuffer input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(ByteBuffer) method of the wrapped MessageDigest instance.

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(Boolean input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input.booleanValue()).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(Boolean[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(Boolean[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(Boolean) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(boolean input)
        Update the wrapped MessageDigest object with the given input data.
        Parameters:
        input - Input data. true results in update((byte)1) and false results in update((byte)0).
        Returns:
        this object.
      • update

        public Digest update​(boolean[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(boolean[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(boolean) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(Character input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input.charValue()).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(Character[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(Character[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(Character) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(char input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update ((byte)((input >> 8) & 0xff)) and update(((byte)(input >> 0) & 0xff).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(char[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(char[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(char) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(short input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update ((byte)((input >> 8) & 0xff)) and update(((byte)(input >> 0) & 0xff).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(short[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(short[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(short) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(int input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(byte) for each byte of the 4 bytes from MSB to LSB (from ((input >> 24) & 0xff) to ((input >> 0) & 0xff)).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(int[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(int[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(int) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(long input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(byte) for each byte of the 8 bytes from MSB to LSB (from ((input >> 54) & 0xff) to ((input >> 0) & 0xff)).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(long[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(long[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(long) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(float input)
        Update the wrapped MessageDigest object with the given input data.

        This method converts the given float value to a int by Float.floatToRawIntBits(float) and then passes it to update(int).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(float[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(float[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(float) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(double input)
        Update the wrapped MessageDigest object with the given input data.

        This method converts the given double value to a long by Double.doubleToRawLongBits(double) and then passes it to update(long).

        Parameters:
        input - Input data.
        Returns:
        this object.
      • update

        public Digest update​(double[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(double[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(double) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(Number number)
        Update the wrapped MessageDigest object with the given input data.

        This method checks the class of the given instance and calls a corresponding update method.

        Class Executed code
        Byte update(((Byte)number).byteValue())
        Short update(((Short)number).shortValue())
        Integer update(((Integer)number).intValue())
        Long update(((Long)number).longValue())
        Float update(((Float)number).floatValue())
        Double update(((Double)number).doubleValue())
        BigInteger update(((BigInteger)number).toByteArray())
        BigDecimal update(((BigDecimal)number).toString())
        AtomicInteger update(((AtomicInteger)number).intValue())
        AtomicLong update(((AtomicLong)number).longValue())
        Others Ignored.
        Parameters:
        number - Input data. If null or none of the above, update is not performed.
        Returns:
        this object.
      • update

        public <TNumber extends NumberDigest update​(TNumber[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Type Parameters:
        TNumber - The subclass of the Number class.
        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public <TNumber extends NumberDigest update​(TNumber[] input,
                                                      int offset,
                                                      int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(Number) for each array element which is in the specified range.

        Type Parameters:
        TNumber - The subclass of the Number class.
        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(String input)
        Update the wrapped MessageDigest object with the given input data.

        This method converts the given string into bytes with the character set of UTF-8 and then passes the byte array to update(byte[]).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(String[] input)
        Update the wrapped MessageDigest object with the given input data.

        This method is an alias of update(input, 0, input.length).

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • update

        public Digest update​(String[] input,
                             int offset,
                             int length)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(String) for each array element which is in the specified range.

        Parameters:
        input - Input data.
        offset - The offset to start from in the array.
        length - The number of elements to use, starting at offset.
        Returns:
        this object.
        Throws:
        IllegalArgumentException - The range specified by the parameters is invalid.
      • update

        public Digest update​(Iterable<?> input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(Object) for each element.

        Parameters:
        input - Input data. If null is given, update is not performed. null elements are ignored. Elements of unsupported classes are ignored, too.
        Returns:
        this object.
      • update

        public Digest update​(Object... input)
        Update the wrapped MessageDigest object with the given input data.

        This method calls update(Object) for each element.

        Parameters:
        input - Input data. If null is given, update is not performed. null elements are ignored. Elements of unsupported classes are ignored, too.
        Returns:
        this object.
      • update

        public Digest update​(Object input)
        Update the wrapped MessageDigest object with the given input data.

        This method checks the class of the given object and calls a corresponding update method.

        Parameters:
        input - Input data. If null is given, update is not performed.
        Returns:
        this object.
      • updateJson

        public Digest updateJson​(String json)
                          throws IOException
        Update the wrapped MessageDigest object with the given JSON. This method updates the digest based on the content of the given JSON, and in the respect, this method is different from update(String).

        JSONs with the same content, for example, two JSONs below, generate the same digest.

         { "key1":"value1", "key2":"value2" }
         { "key1" : "value1" , "key2" : "value2" }
         

        If Digest.Feature.IGNORE_JSON_OBJECT_ENTRY_WITH_VALUE_NULL is enabled (it is disabled by default), key-value entries with value 'null' are treated as if they did not exist. Therefore, two JSONs below generate the same digest value.

         { "key1":"value1", "key2":null }
         { "key1":"value1" }
         

        If Digest.Feature.SORT_JSON_OBJECT_ENTRY_KEYS is enabled (it is enabled by default), orders of JSON object keys do not matter. Therefore, two JSONs below generate the same digest value.

         { "key1":"value1", "key2":"value2" }
         { "key2":"value2", "key1":"value1" }
         
        Parameters:
        json - JSON.
        Returns:
        this object.
        Throws:
        IOException - Failed to parse the given JSON.
      • isEnabled

        public boolean isEnabled​(Digest.Feature feature)
        Check if the specified feature is enabled.
        Parameters:
        feature - Feature to check.
        Returns:
        true if the feature is enabled. Otherwise, false.
      • setEnabled

        public Digest setEnabled​(Digest.Feature feature,
                                 boolean enabled)
        Enable or disable the specified feature.
        Parameters:
        feature - Digest.Feature to enable or disable.
        enabled - true to enable the feature. false to disable the feature.
        Returns:
        this object.
      • bytesToHex

        public static String bytesToHex​(byte[] bytes)
        Convert the given byte array to a hex string.
        Parameters:
        bytes - A byte array to convert.
        Returns:
        A hex string with 0-9 and a-f.