Package com.globalmentor.security
Class MessageDigests.Algorithm
java.lang.Object
com.globalmentor.security.MessageDigests.Algorithm
- Enclosing class:
- MessageDigests
Encapsulation of a standard
MessageDigest
algorithm, which may or may not be implemented by any particular Java platform.- Author:
- Garret Wilson
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionchecksum
(byte input) Computes a lowercase hex checksum string using this algorithm for the given input byte.checksum
(byte[] input) Computes a lowercase hex checksum string using this algorithm for the given input bytes.checksum
(byte[] input, int offset, int length) Computes a lowercase hex checksum string using this algorithm for the given input bytes.checksum
(char[] characters) Computes a lowercase hex checksum string using this algorithm for the given characters using the UTF-8 charset.checksum
(InputStream inputStream) Computes a lowercase hex checksum string using this algorithm for the contents of the given input stream.checksum
(CharSequence charSequence) Computes a lowercase hex checksum string using this algorithm for the given character sequence using the UTF-8 charset.checksum
(ByteBuffer byteBuffer) Computes a lowercase hex checksum string using this algorithm for the given input byte buffer.Computes a lowercase hex checksum string using this algorithm for the contents of the given file.byte[]
digest
(byte b) Computes a digest using this algorithm for the given byte.byte[]
digest
(byte[] bytes) Computes a digest using this algorithm for the specified array of bytes.byte[]
digest
(byte[] bytes, int offset, int length) Computes a digest using this algorithm for the specified array of bytes, starting at the specified offset.byte[]
digest
(char[] characters) Computes a digest using this algorithm for the given characters using the UTF-8 charset.byte[]
digest
(InputStream inputStream) Computes a digest using this algorithm for the contents of the given input stream.byte[]
digest
(CharSequence... charSequences) Computes a digest using this algorithm for the given character sequences using the UTF-8 charset.byte[]
digest
(ByteBuffer byteBuffer) Computes a digest for the specified byte buffer.byte[]
Computes a digest using this algorithm for the given characters, using the given charset.byte[]
digest
(Charset charset, CharSequence... charSequences) Computes a digest using this algorithm for the given character sequences, using the given charset.byte[]
Computes a digest using this algorithm for the contents of the given file.Returns an "empty" hash using this algorithm.boolean
Deprecated.getName()
hash
(byte b) Computes a hash using this algorithm for the given byte.hash
(byte[] bytes) Computes a hash using this algorithm for the specified array of bytes.hash
(byte[] bytes, int offset, int length) Computes a hash using this algorithm for the specified array of bytes, starting at the specified offset.hash
(char[] characters) Computes a hash using this algorithm for the given characters using the UTF-8 charset.Computes a hash using this algorithm for the given hashes.hash
(InputStream inputStream) Computes a hash using this algorithm for the contents of the given input stream.hash
(CharSequence... charSequences) Computes a hash using this algorithm for the given character sequences using the UTF-8 charset.hash
(ByteBuffer byteBuffer) Computes a hash using this algorithm for the specified byte buffer.Computes a hash using this algorithm for the given characters, using the given charset.hash
(Charset charset, CharSequence... charSequences) Computes a hash using this algorithm for the given character sequences, using the given charset.Computes a hash using this algorithm for the contents of the given file.int
hashCode()
boolean
Determines whether the given hash is "empty", that is, equivalent to hashing no contentCreates a new message digest instance for this algorithm.toString()
-
Method Details
-
getName
-
newMessageDigest
Creates a new message digest instance for this algorithm.- API Note:
- This method differs from
MessageDigest.getInstance(String)
in that it throws an unchecked exception if the algorithm is not supported. - Implementation Specification:
- This method delegates to
MessageDigest.getInstance(String)
. - Returns:
- An instance of a message digest for this algorithm.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
getInstance
Deprecated.in favor ofnewMessageDigest()
, which has a clearer and less misleading name.Creates a new message digest instance for this algorithm.- Returns:
- An instance of a message digest for this algorithm.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
digest
Computes a digest using this algorithm for the given byte.- Parameters:
b
- The byte to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest using this algorithm for the specified array of bytes, starting at the specified offset.- Parameters:
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 using this algorithm for the specified array of bytes.- Implementation Specification:
- This implementation delegates to
MessageDigest.digest(byte[])
. - Parameters:
bytes
- The array of bytes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest for the specified byte buffer.- Parameters:
byteBuffer
- The byte buffer containing the bytes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
digest
Computes a digest using this algorithm for the given character sequences using the UTF-8 charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, CharSequence...)
. - Parameters:
charSequences
- The character sequences to digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
digest
Computes a digest using this algorithm for the given character sequences, using the given charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, Charset, CharSequence...)
. - Parameters:
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.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
digest
Computes a digest using this algorithm for the given characters using the UTF-8 charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, char[])
. - Parameters:
characters
- The characters to digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
digest
Computes a digest using this algorithm for the given characters, using the given charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, Charset, char[])
. - Parameters:
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.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
digest
Computes a digest using this algorithm for the contents of the given input stream. All the remaining contents of the input stream are consumed.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, InputStream)
. - Parameters:
inputStream
- The input stream on which to perform a digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the input stream.
-
digest
Computes a digest using this algorithm for the contents of the given file.- Implementation Specification:
- This convenience method delegates to
MessageDigests.digest(MessageDigest, Path)
. - Parameters:
file
- The path to the file on which to perform a digest.- Returns:
- The array of bytes for the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the file.
-
emptyHash
Returns an "empty" hash using this algorithm. This method is equivalent to creating a message digest and not adding any content, such as callinghash(CharSequence...)
with an empty array of strings.- API Note:
- This method is preferred over calling another hash method and providing no content, not only because this method is more semantically appropriate, but also because this implementation is likely much more efficient.
- Implementation Specification:
- This method maintains a lazy cache of empty hashes for algorithms. Although these caches are never released, very few message digest algorithms are common (and typically only one or two used in a single application), wasted space is nominal.
- Returns:
- A hash for a message digest from this algorithm that has not processed any content..
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
isEmpty
Determines whether the given hash is "empty", that is, equivalent to hashing no content- Parameters:
hash
- The hash to check.- Returns:
- Whether the given hash is equal to a hash created from a new message digest which has hashed no content.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.- See Also:
-
hash
Computes a hash using this algorithm for the given byte.- Parameters:
b
- The byte to digest.- Returns:
- The resulting hash.
-
hash
Computes a hash using this algorithm for the specified array of bytes.- Parameters:
bytes
- The array of bytes to digest.- Returns:
- The array of bytes for the resulting hash value.
-
hash
Computes a hash using this algorithm for the specified array of bytes, starting at the specified offset.- Parameters:
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 using this algorithm for the specified byte buffer.- Parameters:
byteBuffer
- The byte buffer containing the bytes to digest.- Returns:
- The resulting hash.
-
hash
Computes a hash using this algorithm for the given character sequences using the UTF-8 charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, CharSequence...)
. - Parameters:
charSequences
- The character sequences to digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
hash
Computes a hash using this algorithm for the given character sequences, using the given charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, Charset, CharSequence...)
. - Parameters:
charset
- The charset to use when converting characters to bytes.charSequences
- The character sequences to digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
hash
Computes a hash using this algorithm for the given characters using the UTF-8 charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, char[])
. - Parameters:
characters
- The characters to digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
hash
Computes a hash using this algorithm for the given characters, using the given charset.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, Charset, char[])
. - Parameters:
charset
- The charset to use when converting characters to bytes.characters
- The arrays of characters to digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
hash
Computes a hash using this algorithm for the given hashes.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, Hash...)
. - Parameters:
hashes
- The hashes to digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
hash
Computes a hash using this algorithm for the contents of the given input stream. All the remaining contents of the input stream are consumed.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, InputStream)
. - Parameters:
inputStream
- The input stream on which to perform a digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the input stream.
-
hash
Computes a hash using this algorithm for the contents of the given file.- Implementation Specification:
- This convenience method delegates to
MessageDigests.hash(MessageDigest, Path)
. - Parameters:
file
- The path to the file on which to perform a digest.- Returns:
- The resulting hash.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the file.
-
checksum
Computes a lowercase hex checksum string using this algorithm 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 delegates to
MessageDigests.checksum(MessageDigest, byte)
. - Parameters:
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 using this algorithm 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 delegates to
MessageDigests.checksum(MessageDigest, byte[])
. - Parameters:
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
Computes a lowercase hex checksum string using this algorithm 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 delegates to
MessageDigests.checksum(MessageDigest, byte[], int, int)
. - Parameters:
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 using this algorithm 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 delegates to
MessageDigests.checksum(MessageDigest, ByteBuffer)
. - Parameters:
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
Computes a lowercase hex checksum string using this algorithm 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
MessageDigests.checksum(MessageDigest, CharSequence)
. - Parameters:
charSequence
- The character sequence for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.
-
checksum
Computes a lowercase hex checksum string using this algorithm 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
MessageDigests.checksum(MessageDigest, char[])
. - Parameters:
characters
- The characters for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.- See Also:
-
checksum
Computes a lowercase hex checksum string using this algorithm 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
MessageDigests.checksum(MessageDigest, InputStream)
. - Parameters:
inputStream
- The input stream for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the input stream.- See Also:
-
checksum
Computes a lowercase hex checksum string using this algorithm 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
MessageDigests.checksum(MessageDigest, Path)
. - Parameters:
file
- The path to the file for which a checksum should be created.- Returns:
- The lowercase hex checksum string of the resulting hash value.
- Throws:
RuntimeException
- if noProvider
supports aMessageDigestSpi
implementation for this algorithm.IOException
- if there is an I/O exception reading from the file.- See Also:
-
hashCode
public int hashCode() -
equals
-
toString
-
newMessageDigest()
, which has a clearer and less misleading name.