Class HashUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of theHMAC-MD5MAC algorithm.static final StringThe name of theHMAC-SHA1MAC algorithm.static final StringThe name of theHMAC-SHA256MAC algorithm.static final StringThe name of theHMAC-SHA512MAC algorithm.static final StringThe name of theMD5hash algorithm.static final StringThe name of theSHA-1hash algorithm.static final StringThe name of theSHA-256hash algorithm.static final StringThe name of theSHA-512hash algorithm. -
Method Summary
Modifier and TypeMethodDescriptionstatic MessageDigestReturns aMessageDigestobject that implements the specified algorithm.static MacgetHmacMD5(byte[] key) Returns a newMacobject that implements the HMAC-MD5 algorithm using the specified key.static MacgetHmacSHA1(byte[] key) Returns a newMacobject that implements the HMAC-SHA1 algorithm using the specified key.static MacgetHmacSHA256(byte[] key) Returns a newMacobject that implements the HMAC-SHA256 algorithm using the specified key.static MacgetHmacSHA512(byte[] key) Returns a newMacobject that implements the HMAC-SHA512 algorithm using the specified key.static MacReturns aMacobject that implements the specified algorithm using the specified key.static MessageDigestgetMD5()Returns aMessageDigestobject that implements the MD5 algorithm.static MessageDigestgetSHA1()Returns aMessageDigestobject that implements the SHA-1 algorithm.static MessageDigestReturns aMessageDigestobject that implements the SHA-256 algorithm.static MessageDigestReturns aMessageDigestobject that implements the SHA-512 algorithm.static byte[]hash(MessageDigest alg, byte[] bytes) Calculates the hash of the specified bytes and returns it.static byte[]hash(MessageDigest alg, byte[]... bytes) Calculates the hash of the specified bytes and returns it.static byte[]hash(MessageDigest alg, InputStream input) Reads data from the input stream and hashes it.static byte[]hash(MessageDigest alg, Path path) Reads the content from the specified file and hashes it.static byte[]hashUTF8(MessageDigest alg, char[] chars) UTF8-encodes the specifiedchararray and hashes it.static byte[]hashUTF8(MessageDigest alg, String str) UTF8-encodes the specifiedStringand hashes it.static voidupdateUTF8(MessageDigest alg, char[] chars) UTF8-encodes the specifiedchararray and passes it to the specifiedMessageDigest.static voidupdateUTF8(MessageDigest alg, char[] chars, int offset, int length) UTF8-encodes the specifiedchararray and passes it to the specifiedMessageDigest.static voidupdateUTF8(MessageDigest alg, String str) UTF8-encodes the specifiedStringand passes it to the specifiedMessageDigest.
-
Field Details
-
MD5
The name of theMD5hash algorithm.All Java platforms before Java 14 are required to support this algorithm.
- See Also:
-
SHA1
The name of theSHA-1hash algorithm.All Java platforms are required to support this algorithm.
- See Also:
-
SHA256
The name of theSHA-256hash algorithm.All Java platforms are required to support this algorithm.
- See Also:
-
SHA512
The name of theSHA-512hash algorithm.The Java platform is not required to support this algorithm.
- See Also:
-
HMAC_MD5
The name of theHMAC-MD5MAC algorithm.All Java platforms before Java 14 are required to support this algorithm.
- Since:
- 1.3
- See Also:
-
HMAC_SHA1
The name of theHMAC-SHA1MAC algorithm.All Java platforms are required to support this algorithm.
- Since:
- 1.3
- See Also:
-
HMAC_SHA256
The name of theHMAC-SHA256MAC algorithm.All Java platforms are required to support this algorithm.
- Since:
- 1.3
- See Also:
-
HMAC_SHA512
The name of theHMAC-SHA512MAC algorithm.The Java platform is not required to support this algorithm.
- Since:
- 1.3
- See Also:
-
-
Method Details
-
get
Returns aMessageDigestobject that implements the specified algorithm.- Parameters:
algorithm- The algorithm name.- Returns:
- The
MessageDigestobject. - Throws:
IllegalArgumentException- If the algorithm is unknown.- See Also:
-
getMac
Returns aMacobject that implements the specified algorithm using the specified key.- Parameters:
algorithm- The algorithm name.key- The key.- Returns:
- The
Macobject. - Throws:
IllegalArgumentException- If the algorithm is unknown.- See Also:
-
getMD5
Returns aMessageDigestobject that implements the MD5 algorithm.- Returns:
- The
MessageDigestobject. - Throws:
IllegalArgumentException- If the MD5 algorithm is not supported by this runtime. All Java platforms before Java 14 are required to support this algorithm.- Since:
- 1.1
- See Also:
-
getSHA1
Returns aMessageDigestobject that implements the SHA-1 algorithm.- Returns:
- The
MessageDigestobject. - Since:
- 1.1
- See Also:
-
getSHA256
Returns aMessageDigestobject that implements the SHA-256 algorithm.- Returns:
- The
MessageDigestobject. - Since:
- 1.1
- See Also:
-
getSHA512
Returns aMessageDigestobject that implements the SHA-512 algorithm.- Returns:
- The
MessageDigestobject. - Throws:
IllegalArgumentException- If the SHA-512 algorithm is not supported by this runtime.- Since:
- 1.1
- See Also:
-
getHmacMD5
Returns a newMacobject that implements the HMAC-MD5 algorithm using the specified key.- Parameters:
key- The key.- Returns:
- The
Macobject. - Throws:
IllegalArgumentException- If the HMAC-MD5 algorithm is not supported by this runtime.- Since:
- 1.3
- See Also:
-
getHmacSHA1
-
getHmacSHA256
-
getHmacSHA512
Returns a newMacobject that implements the HMAC-SHA512 algorithm using the specified key.- Parameters:
key- The key.- Returns:
- The
Macobject. - Throws:
IllegalArgumentException- If the HMAC-SHA512 algorithm is not supported by this runtime.- Since:
- 1.3
- See Also:
-
hash
Calculates the hash of the specified bytes and returns it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash.bytes- The bytes that should be hashed.- Returns:
- The hash. Not
null.
-
hash
Calculates the hash of the specified bytes and returns it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash.bytes- Multiple byte arrays that are handled as a single large one. Must not benull, but the inner arrays may benull. Inner arrays that arenullare ignored.- Returns:
- The hash. Not
null.
-
hash
Reads data from the input stream and hashes it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash.input- The input stream from which the data should be read.- Returns:
- The hash. Not
null. - Throws:
IOException- If an I/O error occurs.
-
hash
Reads the content from the specified file and hashes it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash.path- A path to the file that should be read.- Returns:
- The hash of the file's content. Not
null. - Throws:
IOException- If an I/O error occurs.
-
updateUTF8
UTF8-encodes the specifiedStringand passes it to the specifiedMessageDigest.This method behaves like
alg.update(str.getBytes(StandardCharsets.UTF_8)), but requires only constant memory for large strings.- Parameters:
alg- TheMessageDigestthat should be updated.str- The string that should be encoded.- Since:
- 1.3
-
hashUTF8
UTF8-encodes the specifiedStringand hashes it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash. Must not benull.str- The string that should be encoded. Must not benull.- Returns:
- The hash. Not
null. - Since:
- 1.3
-
updateUTF8
UTF8-encodes the specifiedchararray and passes it to the specifiedMessageDigest.This method behaves like
alg.update(new String(chars, offset, length).getBytes(StandardCharsets.UTF_8))
but requires only constant memory for large strings and overwrites additional copies of thechararray.- Parameters:
alg- TheMessageDigestthat should be updated. Must not benull.chars- Thechararray that should be encoded. Must not benull.offset- The first index of thechararray that should be encoded. Must not be negative. Must fulfilloffset+length <= chars.length.length- The amount of characters of thechararray that should be encoded, starting atoffset. Must not be negative.- Since:
- 1.4
-
updateUTF8
UTF8-encodes the specifiedchararray and passes it to the specifiedMessageDigest.This method behaves like
alg.update(new String(chars).getBytes(StandardCharsets.UTF_8)), but requires only constant memory for large strings and overwrites additional copies of thechararray.- Parameters:
alg- TheMessageDigestthat should be updated. Must not benull.chars- Thechararray that should be encoded. Must not benull.- Since:
- 1.4
-
hashUTF8
UTF8-encodes the specifiedchararray and hashes it.- Parameters:
alg- TheMessageDigestthat should be used to calculate the hash. Must not benull.chars- Thechararray that should be encoded. Must not benull.- Returns:
- The hash. Not
null. - Since:
- 1.4
-