Package dk.netarkivet.common.utils
Class ChecksumCalculator
- java.lang.Object
-
- dk.netarkivet.common.utils.ChecksumCalculator
-
public final class ChecksumCalculator extends 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 String
calculateMd5(byte[] msg)
Generate an MD5 for a byte array.static String
calculateMd5(File src)
Calculate MD5 for a file.static String
calculateMd5(InputStream instream)
Calculates an MD5 digest on an InputStream, throwing away the data itself.static String
calculateSha1(File src)
Calculate the SHA-1 DIGEST for a file.static String
calculateSha1(InputStream instream)
Calculates an SHA-1 digest on an InputStream, throwing away the data itself.static byte[]
digestFile(File src, String digestAlgorithm)
static byte[]
digestInputStream(InputStream instream, String algorithm)
static MessageDigest
getMessageDigest(String algorithm)
Get a MessageDigest for a specific algorithm.static String
toHex(byte[] ba)
Converts a byte array to a hexstring.
-
-
-
Field Detail
-
MD5
public static final String MD5
Defines the MD5 checksum algorithm- See Also:
- Constant Field Values
-
SHA1
public static final String SHA1
Defines the SHA1 checksum algorithm- See Also:
- Constant Field Values
-
-
Method Detail
-
calculateMd5
public static String calculateMd5(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 String calculateSha1(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 String calculateMd5(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 String calculateSha1(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 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 String toHex(byte[] ba)
Converts a byte array to a hexstring.- Parameters:
ba
- the bytearray to be converted- Returns:
- ba converted to a hexstring
-
digestInputStream
public static byte[] digestInputStream(InputStream instream, String algorithm)
-
getMessageDigest
public static MessageDigest getMessageDigest(String algorithm)
Get a MessageDigest for a specific algorithm.- Parameters:
algorithm
- a specific MessageDigest algorithm.- Returns:
- a MessageDigest for a specific algorithm
-
-