Package com.globalmentor.security
Class MessageDigests
java.lang.Object
com.globalmentor.security.MessageDigests
Utility methods for working with message digests.
- Author:
- Garret Wilson
- API Note:
- The standard algorithms enumerated by this class identify known algorithms with standard names. Not all of them are guaranteed to be implemented on any particular Java platform.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Encapsulation of a standardMessageDigest
algorithm, which may or may not be implemented by any particular Java platform. -
Field Summary
Modifier and TypeFieldDescriptionstatic final MessageDigests.Algorithm
The MD2 message digest algorithm as defined in RFC 1319.static final MessageDigests.Algorithm
The MD5 message digest algorithm as defined in RFC 1321.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
A hash algorithms defined in FIPS PUB 180-4.static final MessageDigests.Algorithm
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 224 bit digest.static final MessageDigests.Algorithm
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 256 bit digest.static final MessageDigests.Algorithm
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 384 bit digest.static final MessageDigests.Algorithm
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 512 bit digest. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
checksum
(MessageDigest messageDigest, byte input) Computes a lowercase hex checksum string for the given input byte.static String
checksum
(MessageDigest messageDigest, byte[] input) Computes a lowercase hex checksum string for the given input bytes.static String
checksum
(MessageDigest messageDigest, byte[] input, int offset, int length) Computes a lowercase hex checksum string for the given input bytes.static String
checksum
(MessageDigest messageDigest, char[] characters) Computes a lowercase hex checksum string for the given characters using the UTF-8 charset.static String
checksum
(MessageDigest messageDigest, InputStream inputStream) Computes a lowercase hex checksum string for the contents of the given input stream.static String
checksum
(MessageDigest messageDigest, CharSequence charSequence) Computes a lowercase hex checksum string for the given character sequence using the UTF-8 charset.static String
checksum
(MessageDigest messageDigest, ByteBuffer byteBuffer) Computes a lowercase hex checksum string for the given input byte buffer.static String
checksum
(MessageDigest messageDigest, Path file) Computes a lowercase hex checksum string for the contents of the given file.static byte[]
digest
(MessageDigest messageDigest, byte b) Computes a digest for the given byte.static byte[]
digest
(MessageDigest messageDigest, byte[] bytes, int offset, int length) Computes a digest for the specified array of bytes, starting at the specified offset.static byte[]
digest
(MessageDigest messageDigest, char[] characters) Computes a digest for the given characters using the UTF-8 charset.static byte[]
digest
(MessageDigest messageDigest, Hash... hashes) Computes a digest for the given hashes.static byte[]
digest
(MessageDigest messageDigest, InputStream inputStream) Computes a digest from the contents of the given input stream.static byte[]
digest
(MessageDigest messageDigest, CharSequence... charSequences) Computes a digest for the given character sequences using the UTF-8 charset.static byte[]
digest
(MessageDigest messageDigest, ByteBuffer byteBuffer) Computes a digest for the specified byte buffer.static byte[]
digest
(MessageDigest messageDigest, Charset charset, char[] characters) Computes a digest for the given characters, using the given charset.static byte[]
digest
(MessageDigest messageDigest, Charset charset, CharSequence... charSequences) Computes a digest for the given character sequences, using the given charset.static byte[]
digest
(MessageDigest messageDigest, Path file) Computes a digest from the contents of the given file.static Hash
hash
(MessageDigest messageDigest, byte b) Computes a hash for the given byte.static Hash
hash
(MessageDigest messageDigest, byte[] bytes) Computes a hash for the specified array of bytes.static Hash
hash
(MessageDigest messageDigest, byte[] bytes, int offset, int length) Computes a hash for the specified array of bytes, starting at the specified offset.static Hash
hash
(MessageDigest messageDigest, char[] characters) Computes a hash for the given characters using the UTF-8 charset.static Hash
hash
(MessageDigest messageDigest, Hash... hashes) Computes a hash for the given hashes.static Hash
hash
(MessageDigest messageDigest, InputStream inputStream) Computes a hash from the contents of the given input stream.static Hash
hash
(MessageDigest messageDigest, CharSequence... charSequences) Computes a hash for the given character sequences using the UTF-8 charset.static Hash
hash
(MessageDigest messageDigest, ByteBuffer byteBuffer) Computes a hash for the specified byte buffer.static Hash
hash
(MessageDigest messageDigest, Charset charset, char[] characters) Computes a hash for the given characters, using the given charset.static Hash
hash
(MessageDigest messageDigest, Charset charset, CharSequence... charSequences) Computes a hash for the given character sequences, using the given charset.static Hash
hash
(MessageDigest messageDigest, Path file) Computes a hash from the contents of the given file.static MessageDigest
update
(MessageDigest messageDigest, byte b) Updates a digest from the given byte.static MessageDigest
update
(MessageDigest messageDigest, byte[] bytes) Updates a digest for the specified array of bytes.static MessageDigest
update
(MessageDigest messageDigest, byte[] bytes, int offset, int length) Updates a digest for the specified array of bytes, starting at the specified offset.static MessageDigest
update
(MessageDigest messageDigest, char[] characters) Updates a digest with the given characters using the UTF-8 charset.static MessageDigest
update
(MessageDigest messageDigest, Hash... hashes) Updates a digest from the given hashesstatic MessageDigest
update
(MessageDigest messageDigest, InputStream inputStream) Updates a digest with the contents of the given input stream.static MessageDigest
update
(MessageDigest messageDigest, CharSequence... charSequences) Updates a digest from the given character sequences using the UTF-8 charset.static MessageDigest
update
(MessageDigest messageDigest, ByteBuffer byteBuffer) Updates a digest for the specified byte buffer.static MessageDigest
update
(MessageDigest messageDigest, Charset charset, char[] characters) Updates a digest with the given characters, using the given charset.static MessageDigest
update
(MessageDigest messageDigest, Charset charset, CharSequence charSequence) Updates a digest with the given character sequence, using the given charset.static MessageDigest
update
(MessageDigest messageDigest, Charset charset, CharSequence... charSequences) Updates a digest from given character sequences, using the given charset.static MessageDigest
update
(MessageDigest messageDigest, Path file) Updates a digest with the contents of the given file.
-
Field Details
-
MD2
The MD2 message digest algorithm as defined in RFC 1319. -
MD5
The MD5 message digest algorithm as defined in RFC 1321. Guaranteed to be supported by every Java platform implementation as of Java 7. -
SHA_1
A hash algorithms defined in FIPS PUB 180-4. Guaranteed to be supported by every Java platform implementation as of Java 7. -
SHA_224
A hash algorithms defined in FIPS PUB 180-4. -
SHA_256
A hash algorithms defined in FIPS PUB 180-4. Guaranteed to be supported by every Java platform implementation as of Java 7. -
SHA_384
A hash algorithms defined in FIPS PUB 180-4. -
SHA_512_224
A hash algorithms defined in FIPS PUB 180-4. -
SHA_512_256
A hash algorithms defined in FIPS PUB 180-4. -
SHA3_224
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 224 bit digest. -
SHA3_256
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 256 bit digest. -
SHA3_384
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 384 bit digest. -
SHA3_512
Permutation-based hash and extendable-output functions as defined in FIPS PUB 202 producing a 512 bit digest.
-
-
Constructor Details
-
MessageDigests
public MessageDigests()
-
-
Method Details
-
digest
Computes a digest for the given byte.- Parameters:
messageDigest
- The implementation of a message digest algorithm.b
- The byte to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull byte[] bytes, int offset, int length) Computes a digest for the specified array of bytes, starting at the specified offset.- API Note:
- This class provides no separate method to calculate a digest on an array of bytes with no offset and length given, returning the digest bytes,
because the JDK already provides this functionality in
MessageDigest.digest(byte[])
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.bytes
- The array of bytes to digest.offset
- The offset to start from in the array of bytes.length
- The number of bytes to use, starting at the offset.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest for the specified byte buffer.- Parameters:
messageDigest
- The implementation of a message digest algorithm.byteBuffer
- The byte buffer containing the bytes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull CharSequence... charSequences) Computes a digest for the given character sequences using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charSequences
- The character sequences to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull CharSequence... charSequences) Computes a digest for the given character sequences, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.charSequences
- The character sequences to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest for the given characters using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.characters
- The characters to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull char[] characters) Computes a digest for the given characters, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.characters
- The arrays of characters to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest for the given hashes.- Parameters:
messageDigest
- The implementation of a message digest algorithm.hashes
- The hashes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull InputStream inputStream) throws IOException Computes a digest from the contents of the given input stream. All the remaining contents of the input stream are consumed.- Parameters:
messageDigest
- The implementation of a message digest algorithm.inputStream
- The input stream on which to perform a digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
IOException
- if there is an I/O exception reading from the input stream.
-
digest
public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull Path file) throws IOException Computes a digest from the contents of the given file.- Parameters:
messageDigest
- The implementation of a message digest algorithm.file
- The path of the file on which to perform a digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
IOException
- if there is an I/O exception reading from the file.
-
hash
Computes a hash for the given byte.- Parameters:
messageDigest
- The implementation of a message digest algorithm.b
- The byte to digest.- Returns:
- The resulting hash.
-
hash
Computes a hash for the specified array of bytes.- Parameters:
messageDigest
- The implementation of a message digest algorithm.bytes
- The array of bytes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull byte[] bytes, int offset, int length) Computes a hash for the specified array of bytes, starting at the specified offset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.bytes
- The array of bytes to digest.offset
- The offset to start from in the array of bytes.length
- The number of bytes to use, starting at the offset.- Returns:
- The resulting hash.
-
hash
Computes a hash for the specified byte buffer.- Parameters:
messageDigest
- The implementation of a message digest algorithm.byteBuffer
- The byte buffer containing the bytes to digest.- Returns:
- The resulting hash.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull CharSequence... charSequences) Computes a hash for the given character sequences using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charSequences
- The character sequences to digest.- Returns:
- The resulting hash.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull CharSequence... charSequences) Computes a hash for the given character sequences, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.charSequences
- The character sequences to digest.- Returns:
- The resulting hash.
-
hash
Computes a hash for the given characters using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.characters
- The characters to digest.- Returns:
- The resulting hash.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull char[] characters) Computes a hash for the given characters, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.characters
- The arrays of characters to digest.- Returns:
- The resulting hash.
-
hash
Computes a hash for the given hashes.- Parameters:
messageDigest
- The implementation of a message digest algorithm.hashes
- The hashes to digest.- Returns:
- The resulting hash.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull InputStream inputStream) throws IOException Computes a hash from the contents of the given input stream. All the remaining contents of the input stream are consumed.- Implementation Specification:
- This implementation delegates to
digest(MessageDigest, InputStream)
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.inputStream
- The input stream on which to perform a digest.- Returns:
- The resulting hash.
- Throws:
IOException
- if there is an I/O exception reading from the input stream.
-
hash
public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull Path file) throws IOException Computes a hash from the contents of the given file.- Parameters:
messageDigest
- The implementation of a message digest algorithm.file
- The path of the file on which to perform a digest.- Returns:
- The resulting hash.
- Throws:
IOException
- if there is an I/O exception reading from the file.
-
update
Updates a digest from the given byte.- Parameters:
messageDigest
- The implementation of a message digest algorithm.b
- The byte to digest.- Returns:
- The message digest.
- See Also:
-
update
Updates a digest for the specified array of bytes.- Parameters:
messageDigest
- The implementation of a message digest algorithm.bytes
- The array of bytes to digest.- Returns:
- The message digest.
- See Also:
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull byte[] bytes, int offset, int length) Updates a digest for the specified array of bytes, starting at the specified offset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.bytes
- The array of bytes to digest.offset
- The offset to start from in the array of bytes.length
- The number of bytes to use, starting at the offset.- Returns:
- The message digest.
- See Also:
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull ByteBuffer byteBuffer) Updates a digest for the specified byte buffer.- Parameters:
messageDigest
- The implementation of a message digest algorithm.byteBuffer
- The byte buffer containing the bytes to digest.- Returns:
- The message digest.
- See Also:
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull CharSequence... charSequences) Updates a digest from the given character sequences using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charSequences
- The character sequences to digest.- Returns:
- The message digest.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull CharSequence... charSequences) Updates a digest from given character sequences, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.charSequences
- The character sequences to digest.- Returns:
- The message digest.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull char[] characters) Updates a digest with the given characters using the UTF-8 charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.characters
- The characters to digest.- Returns:
- The message digest.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull CharSequence charSequence) Updates a digest with the given character sequence, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.charSequence
- The character sequence to digest.- Returns:
- The message digest.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull Charset charset, @Nonnull char[] characters) Updates a digest with the given characters, using the given charset.- Parameters:
messageDigest
- The implementation of a message digest algorithm.charset
- The charset to use when converting characters to bytes.characters
- The arrays of characters to digest.- Returns:
- The message digest.
-
update
Updates a digest from the given hashes- Parameters:
messageDigest
- The implementation of a message digest algorithm.hashes
- The hashes to digest.- Returns:
- The message digest.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull InputStream inputStream) throws IOException Updates a digest with the contents of the given input stream. All the remaining contents of the input stream are consumed.- Parameters:
messageDigest
- The implementation of a message digest algorithm.inputStream
- The input stream on which to perform a digest.- Returns:
- The message digest.
- Throws:
IOException
- if there is an I/O exception reading from the input stream.
-
update
public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull Path file) throws IOException Updates a digest with the contents of the given file.- Parameters:
messageDigest
- The implementation of a message digest algorithm.file
- The path of the file on which to perform a digest.- Returns:
- The message digest.
- Throws:
IOException
- if there is an I/O exception reading from the file.
-
checksum
Computes a lowercase hex checksum string for the given input byte.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation calls
digest(MessageDigest, byte)
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.input
- The byte for which a digest and then a checksum string should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
-
checksum
Computes a lowercase hex checksum string for the given input bytes.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation calls
MessageDigest.digest(byte[])
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.input
- The sequence of bytes for which a digest and then a checksum string should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- See Also:
-
checksum
public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull byte[] input, int offset, int length) Computes a lowercase hex checksum string for the given input bytes.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation calls
digest(MessageDigest, byte[], int, int)
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.input
- The array of bytes for which a digest and then a checksum string should be created.offset
- The offset to start from in the array of bytes.length
- The number of bytes to use, starting at the offset.- Returns:
- The lowercase hex checksum string of the resulting hash value.
-
checksum
Computes a lowercase hex checksum string for the given input byte buffer.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation calls
digest(MessageDigest, ByteBuffer)
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.byteBuffer
- The byte buffer containing the bytes for which a digest and then a checksum string should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
-
checksum
public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull CharSequence charSequence) Computes a lowercase hex checksum string for the given character sequence using the UTF-8 charset.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation delegates to
digest(MessageDigest, CharSequence...)
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.charSequence
- The character sequence for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
-
checksum
Computes a lowercase hex checksum string for the given characters using the UTF-8 charset.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation delegates to
digest(MessageDigest, char[])
. - Parameters:
messageDigest
- The implementation of a message digest algorithm.characters
- The characters for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- See Also:
-
checksum
public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull InputStream inputStream) throws IOException Computes a lowercase hex checksum string for the contents of the given input stream. All the remaining contents of the input stream are consumed.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation delegates to
digest(MessageDigest, InputStream)
- Parameters:
messageDigest
- The implementation of a message digest algorithm.inputStream
- The input stream on which to perform a digest.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
IOException
- if there is an I/O exception reading from the input stream.
-
checksum
public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull Path file) throws IOException Computes a lowercase hex checksum string for the contents of the given file.- API Note:
- This method considers a checksum to be a string version of a message digest, as the former is often used in the context of file contents verification.
- Implementation Specification:
- This implementation delegates to
digest(MessageDigest, Path)
- Parameters:
messageDigest
- The implementation of a message digest algorithm.file
- The path to the file on which to perform a digest.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
IOException
- if there is an I/O exception reading from the file.
-