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 manyupdate
methods in a fluent style, meaningupdate
methods can be chained.update
methods 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.getInstanceXXX
methods (whereXXX
is 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 class
Digest.Feature
Features 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 aMessageDigest
instance.
-
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 thisDigest
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 aDigest
instance with the specified algorithm.static Digest
getInstance(String algorithm, String provider)
Create aDigest
instance with the specified algorithm.static Digest
getInstance(String algorithm, Provider provider)
Create aDigest
instance with the specified algorithm.static Digest
getInstanceMD2()
Create aDigest
instance that implements MD2.static Digest
getInstanceMD5()
Create aDigest
instance that implements MD5.static Digest
getInstanceSHA1()
Create aDigest
instance that implements SHA-1.static Digest
getInstanceSHA256()
Create aDigest
instance that implements SHA-256.static Digest
getInstanceSHA384()
Create aDigest
instance that implements SHA-384.static Digest
getInstanceSHA512()
Create aDigest
instance that implements SHA-512.Provider
getProvider()
Get the provider.MessageDigest
getWrappedMessageDigest()
Get the wrappedMessageDigest
instance.boolean
isEnabled(Digest.Feature feature)
Check if the specified feature is enabled.Digest
reset()
Reset the wrappedMessageDigest
instance.Digest
setEnabled(Digest.Feature feature, boolean enabled)
Enable or disable the specified feature.Digest
update(boolean input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(boolean[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(boolean[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(byte input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(byte[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(byte[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(char input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(char[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(char[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(double input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(double[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(double[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(float input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(float[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(float[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(int input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(int[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(int[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(long input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(long[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(long[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(short input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(short[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(short[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Boolean input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Boolean[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Boolean[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Character input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Character[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Character[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Iterable<?> input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Number number)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Object input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(Object... input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(String input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(String[] input)
Update the wrappedMessageDigest
object with the given input data.Digest
update(String[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
update(ByteBuffer input)
Update the wrappedMessageDigest
object with the given input data.<TNumber extends Number>
Digestupdate(TNumber[] input)
Update the wrappedMessageDigest
object with the given input data.<TNumber extends Number>
Digestupdate(TNumber[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.Digest
updateJson(String json)
Update the wrappedMessageDigest
object with the given JSON.
-
-
-
Constructor Detail
-
Digest
public Digest(MessageDigest messageDigest)
Constructor with aMessageDigest
instance.- Parameters:
messageDigest
- AMessageDigest
instance wrapped in thisDigest
instance.- 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 aDigest
instance with the specified algorithm.This method creates a
MessageDigest
instance byMessageDigest.getInstance(String)
and wraps it in aDigest
instance.- Parameters:
algorithm
- Algorithm name such as "MD5" and "SHA-1".- Returns:
- A
Digest
instance 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 aDigest
instance with the specified algorithm.This method creates a
MessageDigest
instance byMessageDigest.getInstance(String, String)
and wraps it in aDigest
instance.- Parameters:
algorithm
- Algorithm name such as "MD5" and "SHA-1".provider
- Provider name.- Returns:
- A
Digest
instance 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 aDigest
instance with the specified algorithm.This method creates a
MessageDigest
instance byMessageDigest.getInstance(String, Provider)
and wraps it in aDigest
instance.- Parameters:
algorithm
- Algorithm name such as "MD5" and "SHA-1".provider
- Provider.- Returns:
- A
Digest
instance that implements the specified algorithm. - Throws:
NoSuchAlgorithmException
- The provider does not support the specified algorithm.
-
getInstanceMD2
public static Digest getInstanceMD2()
Create aDigest
instance that implements MD2.- Returns:
- A
Digest
instance that implements MD2.
-
getInstanceMD5
public static Digest getInstanceMD5()
Create aDigest
instance that implements MD5.- Returns:
- A
Digest
instance that implements MD5.
-
getInstanceSHA1
public static Digest getInstanceSHA1()
Create aDigest
instance that implements SHA-1.- Returns:
- A
Digest
instance that implements SHA-1.
-
getInstanceSHA256
public static Digest getInstanceSHA256()
Create aDigest
instance that implements SHA-256.- Returns:
- A
Digest
instance that implements SHA-256.
-
getInstanceSHA384
public static Digest getInstanceSHA384()
Create aDigest
instance that implements SHA-384.- Returns:
- A
Digest
instance that implements SHA-384.
-
getInstanceSHA512
public static Digest getInstanceSHA512()
Create aDigest
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 wrappedMessageDigest
instance.- Returns:
- Algorithm name.
-
getDigestLength
public int getDigestLength()
Get the length of the digest in bytes.This method just calls
getDigestLength()
of the wrappedMessageDigest
instance.- Returns:
- Length of the digest in bytes.
-
getProvider
public Provider getProvider()
Get the provider.This method just calls
getProvider()
of the wrappedMessageDigest
instance.- Returns:
- Provider.
-
getWrappedMessageDigest
public MessageDigest getWrappedMessageDigest()
Get the wrappedMessageDigest
instance.- Returns:
- The
MessageDigest
instance that has been given to the constructor.
-
clone
public Object clone() throws CloneNotSupportedException
Get a clone of thisDigest
instance.- Overrides:
clone
in classObject
- Returns:
- A cloned object.
- Throws:
CloneNotSupportedException
- The implementation does not supportclone
operation.
-
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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
- Thedigest
method of theMessageDigest
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 throwsEncoderException
, aRuntimeException
wrapping theEncoderException
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. Ifnull
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
isnull
.RuntimeException
- If the encoder throwsEncoderException
, aRuntimeException
wrapping theEncoderException
is thrown.
-
reset
public Digest reset()
Reset the wrappedMessageDigest
instance.- Returns:
this
object.
-
update
public Digest update(byte input)
Update the wrappedMessageDigest
object with the given input data.This method calls
update(byte)
method of the wrappedMessageDigest
instance.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(byte[] input)
Update the wrappedMessageDigest
object with the given input data.This method calls
update(byte[])
method of the wrappedMessageDigest
instance.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(byte[] input, int offset, int length)
Update the wrappedMessageDigest
object with the given input data.This method calls
update(byte[], int, int)
method of the wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method calls
update(ByteBuffer)
method of the wrappedMessageDigest
instance.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(Boolean input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.
-
update
public Digest update(boolean[] input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method calls
update
((byte)((input >> 8) & 0xff))
andupdate
(((byte)(input >> 0) & 0xff)
.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(char[] input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method calls
update
((byte)((input >> 8) & 0xff))
andupdate
(((byte)(input >> 0) & 0xff)
.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(short[] input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method converts the given
float
value to aint
byFloat.floatToRawIntBits(float)
and then passes it toupdate(int)
.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(float[] input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method converts the given
double
value to along
byDouble.doubleToRawLongBits(double)
and then passes it toupdate(long)
.- Parameters:
input
- Input data.- Returns:
this
object.
-
update
public Digest update(double[] input)
Update the wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 Number> Digest update(TNumber[] input)
Update the wrappedMessageDigest
object with the given input data.This method is an alias of
update
(input, 0, input.length)
.- Type Parameters:
TNumber
- The subclass of theNumber
class.- Parameters:
input
- Input data. If null is given, update is not performed.- Returns:
this
object.
-
update
public <TNumber extends Number> Digest update(TNumber[] input, int offset, int length)
Update the wrappedMessageDigest
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 theNumber
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
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 wrappedMessageDigest
object with the given input data.This method calls
update(Object)
for each element.- Parameters:
input
- Input data. Ifnull
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 wrappedMessageDigest
object with the given input data.This method calls
update(Object)
for each element.- Parameters:
input
- Input data. Ifnull
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 wrappedMessageDigest
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. Ifnull
is given, update is not performed.- Returns:
this
object.
-
updateJson
public Digest updateJson(String json) throws IOException
Update the wrappedMessageDigest
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 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_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.
-
-