Package dk.netarkivet.common.utils
Class ChecksumCalculator
- java.lang.Object
-
- dk.netarkivet.common.utils.ChecksumCalculator
-
public final class ChecksumCalculator extends java.lang.Object
Calculates MD5 or SHA1 checksums on files using the built-in Java methods.
-
-
Constructor Summary
Constructors Constructor Description ChecksumCalculator()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
calculateMd5(byte[] msg)
Generate an MD5 for a byte array.static java.lang.String
calculateMd5(java.io.File src)
Calculate MD5 for a file.static java.lang.String
calculateMd5(java.io.InputStream instream)
Calculates an MD5 digest on an InputStream, throwing away the data itself.static java.lang.String
calculateSha1(java.io.File src)
Calculate the SHA-1 DIGEST for a file.static java.lang.String
calculateSha1(java.io.InputStream instream)
Calculates an SHA-1 digest on an InputStream, throwing away the data itself.static byte[]
digestFile(java.io.File src, java.lang.String digestAlgorithm)
static byte[]
digestInputStream(java.io.InputStream instream, java.lang.String algorithm)
static java.security.MessageDigest
getMessageDigest(java.lang.String algorithm)
Get a MessageDigest for a specific algorithm.static java.lang.String
toHex(byte[] ba)
Converts a byte array to a hexstring.
-
-
-
Field Detail
-
MD5
public static final java.lang.String MD5
Defines the MD5 checksum algorithm- See Also:
- Constant Field Values
-
SHA1
public static final java.lang.String SHA1
Defines the SHA1 checksum algorithm- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ChecksumCalculator
public ChecksumCalculator()
-
-
Method Detail
-
calculateMd5
public static java.lang.String calculateMd5(java.io.File src)
Calculate MD5 for a file.- Parameters:
src
- The file to calculate MD5 for.- Returns:
- The MD5 sum of a file as a 32 characters long Hex string.
-
calculateSha1
public static java.lang.String calculateSha1(java.io.File src)
Calculate the SHA-1 DIGEST for a file.- Parameters:
src
- The file to calculate SHA-1 for.- Returns:
- The SHA-1 sum of a file as a 32 characters long Hex string.
-
calculateMd5
public static java.lang.String calculateMd5(java.io.InputStream instream)
Calculates an MD5 digest on an InputStream, throwing away the data itself. Throws Alert if there is an error reading from the stream- Parameters:
instream
- AnInputStream
to calculate the MD5 digest on. The contents of the stream will be consumed by this call, but the stream will not be closed.- Returns:
- The calculated MD5 digest as a string.
-
calculateSha1
public static java.lang.String calculateSha1(java.io.InputStream instream)
Calculates an SHA-1 digest on an InputStream, throwing away the data itself. Throws Alert if there is an error reading from the stream- Parameters:
instream
- AnInputStream
to calculate the SHA-1 digest on. The contents of the stream will be consumed by this call, but the stream will not be closed.- Returns:
- The calculated SHA-1 digest as a string.
-
calculateMd5
public static java.lang.String calculateMd5(byte[] msg)
Generate an MD5 for a byte array.- Parameters:
msg
- The given bytearray- Returns:
- the MD5 for a byte array
-
toHex
public static java.lang.String toHex(byte[] ba)
Converts a byte array to a hexstring.- Parameters:
ba
- the bytearray to be converted- Returns:
- ba converted to a hexstring
-
digestFile
public static byte[] digestFile(java.io.File src, java.lang.String digestAlgorithm)
-
digestInputStream
public static byte[] digestInputStream(java.io.InputStream instream, java.lang.String algorithm)
-
getMessageDigest
public static java.security.MessageDigest getMessageDigest(java.lang.String algorithm)
Get a MessageDigest for a specific algorithm.- Parameters:
algorithm
- a specific MessageDigest algorithm.- Returns:
- a MessageDigest for a specific algorithm
-
-