Class MD5Checksum


  • public final class MD5Checksum
    extends java.lang.Object
    Creates checksums for byte sequences using the MD5 algorithm, as obtained from the crypto API. In the the event that the algorithm is unavailable or throws an exception while computing the checksum, the caller will be shielded from this failure while the checksum would normally be computed. However, when the checksum is requested after such an algorithm failure, null will be returned, and if a failed checksum is compared to a known checksum using matches(java.lang.String), it will always return true.

    Security Warning: Note that this class is only intended to be used to detect issues such as possibly corrupt downloads. It must not be used when the security of a system depends on the results.

    Since:
    3.0
    Author:
    Chris Jennings
    • Constructor Summary

      Constructors 
      Constructor Description
      MD5Checksum()
      Creates a new checksum instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static MD5Checksum forFile​(java.io.File f)
      Returns a new checksum that contains a checksum value for the given file.
      byte[] getChecksum()
      Completes the checksum operation and returns the checksum bytes.
      java.lang.String getChecksumString()
      Completes the checksum and returns a string that represents the checksum value.
      boolean matches​(java.lang.String checksumString)
      Completes the checksum and returns true if this checksum matches a previously produced checksum string.
      void reset()
      Resets this checksum instance.
      java.lang.String toString()  
      void update​(byte[] bytes)
      Processes the next group of bytes to include in the checksum.
      void update​(byte[] buffer, int offset, int length)
      Processes the next block of bytes to be included in the checksum.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • MD5Checksum

        public MD5Checksum()
        Creates a new checksum instance.
    • Method Detail

      • reset

        public void reset()
        Resets this checksum instance. Resetting the checksum prepares it to compute a new checksum; the result of the previously computed checksum, if any, will be lost.
      • update

        public void update​(byte[] bytes)
        Processes the next group of bytes to include in the checksum. This is equivalent to update( bytes, 0, bytes.length ).
        Parameters:
        bytes - an array of bytes representing the next block of data in the stream
        Throws:
        java.lang.NullPointerException - if the buffer is null
        java.lang.IllegalStateException - if the checksum has been completed and reset() has not been called
        See Also:
        update(byte[], int, int)
      • update

        public void update​(byte[] buffer,
                           int offset,
                           int length)
        Processes the next block of bytes to be included in the checksum.
        Parameters:
        buffer - the buffer that holds the block of data
        offset - the offset into the buffer at which the data block starts
        length - the size of the block of data
        Throws:
        java.lang.NullPointerException - if the buffer is null
        java.lang.IllegalStateException - if the checksum has been completed and reset() has not been called
        java.lang.IllegalArgumentException - if the length is negative
        java.lang.ArrayIndexOutOfBoundsException - if the offset or length do not represent valid buffer positions
        See Also:
        getChecksum()
      • getChecksum

        public byte[] getChecksum()
        Completes the checksum operation and returns the checksum bytes. If the checksum operation has already been completed, this will return the same checksum bytes until the next call to reset(). If there has been any internal failure of the checksum algorithm, this method will return null.
        Returns:
        the checksum bytes, or null
      • getChecksumString

        public java.lang.String getChecksumString()
        Completes the checksum and returns a string that represents the checksum value. If there has been any internal failure of the checksum algorithm, this method will return null.
        Returns:
        a string of hexadecimal digits representing the checksum value
        See Also:
        getChecksum()
      • matches

        public boolean matches​(java.lang.String checksumString)
        Completes the checksum and returns true if this checksum matches a previously produced checksum string. If there has been any internal failure of the checksum algorithm, or if the comparison string is null, this method will return true.
        Parameters:
        checksumString - the checksum string to compare to
        Returns:
        false if and only if the comparison string is not null, this check has non-null checksum bytes, and this checksum's checksum string is equal to the comparison string except for differences in letter case
      • forFile

        public static MD5Checksum forFile​(java.io.File f)
                                   throws java.io.IOException
        Returns a new checksum that contains a checksum value for the given file.
        Parameters:
        f - the file to compute a checksum for
        Throws:
        java.io.IOException - if an I/O error occurs
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object