update per issue #933

This commit is contained in:
Jeremy Long
2017-10-22 15:34:16 -04:00
parent 714b3d29b9
commit 765bfa0e1d
5 changed files with 53 additions and 91 deletions

View File

@@ -24,6 +24,8 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Includes methods to generate the MD5 and SHA1 checksum.
@@ -38,6 +40,11 @@ public final class Checksum {
*/
private static final String HEXES = "0123456789abcdef";
/**
* The logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(Checksum.class);
/**
* Private constructor for a utility class.
*/
@@ -100,6 +107,30 @@ public final class Checksum {
return getHex(b);
}
/**
* Calculates the MD5 checksum of a specified bytes.
*
* @param bytes the bytes to generate the MD5 checksum
* @return the hex representation of the MD5 hash
*/
public static String getMD5Checksum(byte[] bytes) {
MessageDigest algorithm = getMessageDigest("MD5");
final byte[] b = algorithm.digest(bytes);
return getHex(b);
}
/**
* Calculates the SHA1 checksum of a specified bytes.
*
* @param bytes the bytes to generate the MD5 checksum
* @return the hex representation of the SHA1 hash
*/
public static String getSHA1Checksum(byte[] bytes) {
MessageDigest algorithm = getMessageDigest("SHA1");
final byte[] b = algorithm.digest(bytes);
return getHex(b);
}
/**
* <p>
* Converts a byte array into a hex string.</p>
@@ -121,4 +152,20 @@ public final class Checksum {
}
return hex.toString();
}
/**
* Returns the message digest.
*
* @param algorithm the algorithm for the message digest
* @return the message digest
*/
private static MessageDigest getMessageDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage());
final String msg = String.format("Failed to obtain the {} message digest.", algorithm);
throw new IllegalStateException(msg, e);
}
}
}