Class MD5Checksum
- java.lang.Object
-
- ca.cgjennings.apps.arkham.plugins.catalog.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 usingmatches(java.lang.String)
, it will always returntrue
.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 returnstrue
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.
-
-
-
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 toupdate( 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 isnull
java.lang.IllegalStateException
- if the checksum has been completed andreset()
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 dataoffset
- the offset into the buffer at which the data block startslength
- the size of the block of data- Throws:
java.lang.NullPointerException
- if the buffer isnull
java.lang.IllegalStateException
- if the checksum has been completed andreset()
has not been calledjava.lang.IllegalArgumentException
- if the length is negativejava.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 toreset()
. If there has been any internal failure of the checksum algorithm, this method will returnnull
.- 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 returnnull
.- 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 returnstrue
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 isnull
, this method will returntrue
.- Parameters:
checksumString
- the checksum string to compare to- Returns:
false
if and only if the comparison string is notnull
, 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 classjava.lang.Object
-
-