Class Digest
- java.lang.Object
-
- com.authlete.common.util.digest.Digest
-
- All Implemented Interfaces:
Cloneable
public class Digest extends Object implements Cloneable
A wrapper class over MessageDigest with manyupdatemethods in a fluent style, meaningupdatemethods can be chained.updatemethods are provided for all the primitive types andString, 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 thatupdate(String)andupdateJson(String)are different.getInstanceXXXmethods (whereXXXis a pre-defined algorithm name with hyphens removed) such asgetInstanceSHA1()are provided. They won't throwNoSuchAlgorithmException.// 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(newBase64()); // 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 classDigest.FeatureFeatures to control behaviors.
-
Constructor Summary
Constructors Constructor Description Digest(String algorithm)Constructor with an algorithm name.Digest(String algorithm, String provider)Constructor with an algorithm name and a provider.Digest(String algorithm, Provider provider)Constructor with an algorithm name and a provider.Digest(MessageDigest messageDigest)Constructor with aMessageDigestinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringbytesToHex(byte[] bytes)Convert the given byte array to a hex string.Objectclone()Get a clone of thisDigestinstance.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.intdigest(byte[] output, int offset, int length)Complete the hash computation.StringdigestAsString()Complete the hash computation and get the resulting hash value as a hex string.StringdigestAsString(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.StringdigestAsString(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.StringdigestAsString(org.apache.commons.codec.BinaryEncoder encoder)Complete the hash computation and get the resulting hash value as a string.StringgetAlgorithm()Get the algorithm name.intgetDigestLength()Get the length of the digest in bytes.static DigestgetInstance(String algorithm)Create aDigestinstance with the specified algorithm.static DigestgetInstance(String algorithm, String provider)Create aDigestinstance with the specified algorithm.static DigestgetInstance(String algorithm, Provider provider)Create aDigestinstance with the specified algorithm.static DigestgetInstanceMD2()Create aDigestinstance that implements MD2.static DigestgetInstanceMD5()Create aDigestinstance that implements MD5.static DigestgetInstanceSHA1()Create aDigestinstance that implements SHA-1.static DigestgetInstanceSHA256()Create aDigestinstance that implements SHA-256.static DigestgetInstanceSHA384()Create aDigestinstance that implements SHA-384.static DigestgetInstanceSHA512()Create aDigestinstance that implements SHA-512.ProvidergetProvider()Get the provider.MessageDigestgetWrappedMessageDigest()Get the wrappedMessageDigestinstance.booleanisEnabled(Digest.Feature feature)Check if the specified feature is enabled.Digestreset()Reset the wrappedMessageDigestinstance.DigestsetEnabled(Digest.Feature feature, boolean enabled)Enable or disable the specified feature.Digestupdate(boolean input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(boolean[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(boolean[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(byte input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(byte[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(byte[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(char input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(char[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(char[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(double input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(double[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(double[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(float input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(float[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(float[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(int input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(int[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(int[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(long input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(long[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(long[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(short input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(short[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(short[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Boolean input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Boolean[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Boolean[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Character input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Character[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Character[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Iterable<?> input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Number number)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Object input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(Object... input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(String input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(String[] input)Update the wrappedMessageDigestobject with the given input data.Digestupdate(String[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.Digestupdate(ByteBuffer input)Update the wrappedMessageDigestobject with the given input data.<TNumber extends Number>
Digestupdate(TNumber[] input)Update the wrappedMessageDigestobject with the given input data.<TNumber extends Number>
Digestupdate(TNumber[] input, int offset, int length)Update the wrappedMessageDigestobject with the given input data.DigestupdateJson(String json)Update the wrappedMessageDigestobject with the given JSON.
-
-
-
Constructor Detail
-
Digest
public Digest(MessageDigest messageDigest)
Constructor with aMessageDigestinstance.- Parameters:
messageDigest- AMessageDigestinstance wrapped in thisDigestinstance.- Throws:
IllegalArgumentException- The given argument is null.
-
Digest
public Digest(String algorithm) throws NoSuchAlgorithmException
Constructor with an algorithm name.This constructor is equivalent to
this(MessageDigest.getInstance(algorithm)).- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".- Throws:
NoSuchAlgorithmException- No provider supports the specified algorithm.
-
Digest
public Digest(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Constructor with an algorithm name and a provider.This constructor is equivalent to
this(MessageDigest.getInstance(algorithm, provider)).- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".provider- Provider name.- Throws:
NoSuchAlgorithmException- The provider does not support the specified algorithm.NoSuchProviderException- The specified provider is not registered in the security provider list.
-
Digest
public Digest(String algorithm, Provider provider) throws NoSuchAlgorithmException
Constructor with an algorithm name and a provider.This constructor is equivalent to
this(MessageDigest.getInstance(algorithm, provider)).- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".provider- Provider.- Throws:
NoSuchAlgorithmException- The provider does not support the specified algorithm.
-
-
Method Detail
-
getInstance
public static Digest getInstance(String algorithm) throws NoSuchAlgorithmException
Create aDigestinstance with the specified algorithm.This method creates a
MessageDigestinstance byMessageDigest.getInstance(String)and wraps it in aDigestinstance.- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".- Returns:
- A
Digestinstance that implements the specified algorithm. - Throws:
NoSuchAlgorithmException- No provider supports the specified algorithm.
-
getInstance
public static Digest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Create aDigestinstance with the specified algorithm.This method creates a
MessageDigestinstance byMessageDigest.getInstance(String, String)and wraps it in aDigestinstance.- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".provider- Provider name.- Returns:
- A
Digestinstance that implements the specified algorithm. - Throws:
NoSuchAlgorithmException- The provider does not support the specified algorithm.NoSuchProviderException- The specified provider is not registered in the security provider list.
-
getInstance
public static Digest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Create aDigestinstance with the specified algorithm.This method creates a
MessageDigestinstance byMessageDigest.getInstance(String, Provider)and wraps it in aDigestinstance.- Parameters:
algorithm- Algorithm name such as "MD5" and "SHA-1".provider- Provider.- Returns:
- A
Digestinstance that implements the specified algorithm. - Throws:
NoSuchAlgorithmException- The provider does not support the specified algorithm.
-
getInstanceMD2
public static Digest getInstanceMD2()
Create aDigestinstance that implements MD2.- Returns:
- A
Digestinstance that implements MD2.
-
getInstanceMD5
public static Digest getInstanceMD5()
Create aDigestinstance that implements MD5.- Returns:
- A
Digestinstance that implements MD5.
-
getInstanceSHA1
public static Digest getInstanceSHA1()
Create aDigestinstance that implements SHA-1.- Returns:
- A
Digestinstance that implements SHA-1.
-
getInstanceSHA256
public static Digest getInstanceSHA256()
Create aDigestinstance that implements SHA-256.- Returns:
- A
Digestinstance that implements SHA-256.
-
getInstanceSHA384
public static Digest getInstanceSHA384()
Create aDigestinstance that implements SHA-384.- Returns:
- A
Digestinstance that implements SHA-384.
-
getInstanceSHA512
public static Digest getInstanceSHA512()
Create aDigestinstance that implements SHA-512.- Returns:
- A
Digestinstance that implements SHA-512.
-
getAlgorithm
public String getAlgorithm()
Get the algorithm name.This method just calls
getAlgorithm()of the wrappedMessageDigestinstance.- Returns:
- Algorithm name.
-
getDigestLength
public int getDigestLength()
Get the length of the digest in bytes.This method just calls
getDigestLength()of the wrappedMessageDigestinstance.- Returns:
- Length of the digest in bytes.
-
getProvider
public Provider getProvider()
Get the provider.This method just calls
getProvider()of the wrappedMessageDigestinstance.- Returns:
- Provider.
-
getWrappedMessageDigest
public MessageDigest getWrappedMessageDigest()
Get the wrappedMessageDigestinstance.- Returns:
- The
MessageDigestinstance that has been given to the constructor.
-
clone
public Object clone() throws CloneNotSupportedException
Get a clone of thisDigestinstance.- Overrides:
clonein classObject- Returns:
- A cloned object.
- Throws:
CloneNotSupportedException- The implementation does not supportcloneoperation.
-
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 wrappedMessageDigestinstance.- 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 wrappedMessageDigestinstance.- 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 DigestExceptionComplete the hash computation. The digest is reset after this call is made.This method just calls
digest(byte[], int, int)method of the wrappedMessageDigestinstance.- 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- Thedigestmethod of theMessageDigestclass 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 throwsEncoderException, aRuntimeExceptionwrapping theEncoderExceptionis 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. Ifnullis 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-encoderisnull.RuntimeException- If the encoder throwsEncoderException, aRuntimeExceptionwrapping theEncoderExceptionis thrown.
-
reset
public Digest reset()
Reset the wrappedMessageDigestinstance.- Returns:
thisobject.
-
update
public Digest update(byte input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(byte)method of the wrappedMessageDigestinstance.- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(byte[] input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(byte[])method of the wrappedMessageDigestinstance.- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(byte[] input, int offset, int length)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(byte[], int, int)method of the wrappedMessageDigestinstance.- Parameters:
input- Input data.offset- The offset to start from in the array.length- The number of elements to use, starting at offset.- Returns:
thisobject.
-
update
public Digest update(ByteBuffer input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(ByteBuffer)method of the wrappedMessageDigestinstance.- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(Boolean input)
Update the wrappedMessageDigestobject with the given input data.This method is an alias of
update(input.booleanValue()).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(Boolean[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(Boolean[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(boolean input)
Update the wrappedMessageDigestobject with the given input data.
-
update
public Digest update(boolean[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(boolean[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(Character input)
Update the wrappedMessageDigestobject with the given input data.This method is an alias of
update(input.charValue()).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(Character[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(Character[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(char input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update((byte)((input >> 8) & 0xff))andupdate(((byte)(input >> 0) & 0xff).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(char[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(char[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(short input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update((byte)((input >> 8) & 0xff))andupdate(((byte)(input >> 0) & 0xff).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(short[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(short[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(int input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(int[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(int[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(long input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(long[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(long[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(float input)
Update the wrappedMessageDigestobject with the given input data.This method converts the given
floatvalue to aintbyFloat.floatToRawIntBits(float)and then passes it toupdate(int).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(float[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(float[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(double input)
Update the wrappedMessageDigestobject with the given input data.This method converts the given
doublevalue to alongbyDouble.doubleToRawLongBits(double)and then passes it toupdate(long).- Parameters:
input- Input data.- Returns:
thisobject.
-
update
public Digest update(double[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(double[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(Number number)
Update the wrappedMessageDigestobject with the given input data.This method checks the class of the given instance and calls a corresponding
updatemethod.Class Executed code Byteupdate(((Byte)number).byteValue())Shortupdate(((Short)number).shortValue())Integerupdate(((Integer)number).intValue())Longupdate(((Long)number).longValue())Floatupdate(((Float)number).floatValue())Doubleupdate(((Double)number).doubleValue())BigIntegerupdate(((BigInteger)number).toByteArray())BigDecimalupdate(((BigDecimal)number).toString())AtomicIntegerupdate(((AtomicInteger)number).intValue())AtomicLongupdate(((AtomicLong)number).longValue())Others Ignored. - Parameters:
number- Input data. If null or none of the above, update is not performed.- Returns:
thisobject.
-
update
public <TNumber extends Number> Digest update(TNumber[] input)
Update the wrappedMessageDigestobject with the given input data.This method is an alias of
update(input, 0, input.length).- Type Parameters:
TNumber- The subclass of theNumberclass.- Parameters:
input- Input data. If null is given, update is not performed.- Returns:
thisobject.
-
update
public <TNumber extends Number> Digest update(TNumber[] input, int offset, int length)
Update the wrappedMessageDigestobject 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 theNumberclass.- Parameters:
input- Input data.offset- The offset to start from in the array.length- The number of elements to use, starting at offset.- Returns:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(String input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(String[] input)
Update the wrappedMessageDigestobject 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:
thisobject.
-
update
public Digest update(String[] input, int offset, int length)
Update the wrappedMessageDigestobject 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:
thisobject.- Throws:
IllegalArgumentException- The range specified by the parameters is invalid.
-
update
public Digest update(Iterable<?> input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(Object)for each element.- Parameters:
input- Input data. Ifnullis given, update is not performed.nullelements are ignored. Elements of unsupported classes are ignored, too.- Returns:
thisobject.
-
update
public Digest update(Object... input)
Update the wrappedMessageDigestobject with the given input data.This method calls
update(Object)for each element.- Parameters:
input- Input data. Ifnullis given, update is not performed.nullelements are ignored. Elements of unsupported classes are ignored, too.- Returns:
thisobject.
-
update
public Digest update(Object input)
Update the wrappedMessageDigestobject with the given input data.This method checks the class of the given object and calls a corresponding
updatemethod.- Parameters:
input- Input data. Ifnullis given, update is not performed.- Returns:
thisobject.
-
updateJson
public Digest updateJson(String json) throws IOException
Update the wrappedMessageDigestobject 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 fromupdate(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_NULLis 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_KEYSis 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:
thisobject.- 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:
trueif the feature is enabled. Otherwise,false.
-
setEnabled
public Digest setEnabled(Digest.Feature feature, boolean enabled)
Enable or disable the specified feature.- Parameters:
feature-Digest.Featureto enable or disable.enabled-trueto enable the feature.falseto disable the feature.- Returns:
thisobject.
-
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.
-
-