Class MessageDigests

java.lang.Object
com.globalmentor.security.MessageDigests

public final class MessageDigests extends Object
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:
  • Field Details

  • Constructor Details

    • MessageDigests

      public MessageDigests()
  • Method Details

    • digest

      public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull byte b)
      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

      public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull ByteBuffer byteBuffer)
      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

      public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull char[] characters)
      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

      public static byte[] digest(@Nonnull MessageDigest messageDigest, @Nonnull Hash... hashes)
      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

      public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull byte b)
      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

      public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull byte[] bytes)
      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

      public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull ByteBuffer byteBuffer)
      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

      public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull char[] characters)
      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

      public static Hash hash(@Nonnull MessageDigest messageDigest, @Nonnull Hash... hashes)
      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

      public static MessageDigest update(@Nonnull MessageDigest messageDigest, byte b)
      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

      public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull byte[] bytes)
      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

      public static MessageDigest update(@Nonnull MessageDigest messageDigest, @Nonnull Hash... hashes)
      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

      public static String checksum(@Nonnull MessageDigest messageDigest, byte input)
      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

      public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull byte[] input)
      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

      public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull ByteBuffer byteBuffer)
      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

      public static String checksum(@Nonnull MessageDigest messageDigest, @Nonnull char[] characters)
      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.