Package test
Class Checksum
java.lang.Object
test.Checksum
Includes methods to generate the MD5 and SHA1 checksum.
- Version:
- $Id: $Id
- Author:
- Jeremy Long
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classFile checksums for each supported algorithm -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBuffer size for calculating checksums.private static final Map<File,Checksum.FileChecksums> Cached file checksums for each supported algorithm.private static final StringHex code characters used in getHex.private static final LoggerThe logger.private static final StringMD5 constant.private static final StringSHA1 constant.private static final StringSHA256 constant. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetChecksum(String algorithm, byte[] bytes) Calculates the MD5 checksum of a specified bytes.static StringgetChecksum(String algorithm, File file) Creates the cryptographic checksum of a given file using the specified algorithm.static StringgetHex(byte[] raw) Converts a byte array into a hex string.static StringgetMD5Checksum(File file) Calculates the MD5 checksum of a specified file.static StringgetMD5Checksum(String text) Calculates the MD5 checksum of the specified text.private static MessageDigestgetMessageDigest(String algorithm) Returns the message digest.static StringgetSHA1Checksum(File file) Calculates the SHA1 checksum of a specified file.static StringgetSHA1Checksum(String text) Calculates the SHA1 checksum of the specified text.static StringgetSHA256Checksum(File file) Calculates the SH256 checksum of a specified file.static StringgetSHA256Checksum(String text) Calculates the SHA256 checksum of the specified text.private static byte[]stringToBytes(String text) Converts the given text into bytes.
-
Field Details
-
HEXES
Hex code characters used in getHex.- See Also:
-
BUFFER_SIZE
Buffer size for calculating checksums.- See Also:
-
LOGGER
The logger. -
MD5
MD5 constant.- See Also:
-
SHA1
SHA1 constant.- See Also:
-
SHA256
SHA256 constant.- See Also:
-
CHECKSUM_CACHE
Cached file checksums for each supported algorithm.
-
-
Constructor Details
-
Checksum
private Checksum()Private constructor for a utility class.
-
-
Method Details
-
getChecksum
public static String getChecksum(String algorithm, File file) throws NoSuchAlgorithmException, IOException Creates the cryptographic checksum of a given file using the specified algorithm.
- Parameters:
algorithm- the algorithm to use to calculate the checksumfile- the file to calculate the checksum for- Returns:
- the checksum
- Throws:
IOException- when the file does not existNoSuchAlgorithmException- when an algorithm is specified that does not exist
-
getMD5Checksum
Calculates the MD5 checksum of a specified file.- Parameters:
file- the file to generate the MD5 checksum- Returns:
- the hex representation of the MD5 hash
- Throws:
IOException- when the file passed in does not existNoSuchAlgorithmException- when the MD5 algorithm is not available
-
getSHA1Checksum
Calculates the SHA1 checksum of a specified file.- Parameters:
file- the file to generate the MD5 checksum- Returns:
- the hex representation of the SHA1 hash
- Throws:
IOException- when the file passed in does not existNoSuchAlgorithmException- when the SHA1 algorithm is not available
-
getSHA256Checksum
Calculates the SH256 checksum of a specified file.- Parameters:
file- the file to generate the MD5 checksum- Returns:
- the hex representation of the SHA1 hash
- Throws:
IOException- when the file passed in does not existNoSuchAlgorithmException- when the SHA1 algorithm is not available
-
getChecksum
Calculates the MD5 checksum of a specified bytes.- Parameters:
algorithm- the algorithm to use (md5, sha1, etc.) to calculate the message digestbytes- the bytes to generate the MD5 checksum- Returns:
- the hex representation of the MD5 hash
-
getMD5Checksum
Calculates the MD5 checksum of the specified text.- Parameters:
text- the text to generate the MD5 checksum- Returns:
- the hex representation of the MD5
-
getSHA1Checksum
Calculates the SHA1 checksum of the specified text.- Parameters:
text- the text to generate the SHA1 checksum- Returns:
- the hex representation of the SHA1
-
getSHA256Checksum
Calculates the SHA256 checksum of the specified text.- Parameters:
text- the text to generate the SHA1 checksum- Returns:
- the hex representation of the SHA1
-
stringToBytes
Converts the given text into bytes.- Parameters:
text- the text to convert- Returns:
- the bytes
-
getHex
Converts a byte array into a hex string.
This method was copied from http://www.rgagnon.com/javadetails/java-0596.html
- Parameters:
raw- a byte array- Returns:
- the hex representation of the byte array
-
getMessageDigest
Returns the message digest.- Parameters:
algorithm- the algorithm for the message digest- Returns:
- the message digest
-