Package org.duracloud.common.util
Class ChecksumUtil
- java.lang.Object
-
- org.duracloud.common.util.ChecksumUtil
-
public class ChecksumUtil extends Object
A utility class for generating checksums. Beware this class it not threadsafe.- Author:
- Daniel Bernstein
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classChecksumUtil.AlgorithmThis class encapsulates the valid values for checksum algorithms.
-
Constructor Summary
Constructors Constructor Description ChecksumUtil(ChecksumUtil.Algorithm alg)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringchecksumBytesToString(byte[] digestBytes)Converts a message digest byte array into a String based on the hex values appearing in the array.static StringconvertToBase64Encoding(String hexEncodedChecksum)Converts a hex-encoded checksum (like that produced by the getChecksum and generateChecksum methods of this class) to a base64-encoded checksumStringgenerateChecksum(File file)StringgenerateChecksum(InputStream inStream)This method generates checksum of content in arg stream.StringgenerateChecksum(String string)This method generates the checksum of a String and returns a hex-encoded String as the checksum valueStringgenerateChecksumBase64(String string)This method generates the checksum of a String and returns a base64-encoded String as the checksum valuestatic StringgetChecksum(DigestInputStream digestStream)Determines the checksum value of a DigestInputStream's underlying stream after the stream has been read.static byte[]getChecksumBytes(DigestInputStream digestStream)Determines the checksum value of a DigestInputStream's underlying stream after the stream has been read.static byte[]hexStringToByteArray(String s)static DigestInputStreamwrapStream(InputStream inStream, ChecksumUtil.Algorithm algorithm)Wraps an InputStream with a DigestInputStream in order to compute a checksum as the stream is being read.
-
-
-
Constructor Detail
-
ChecksumUtil
public ChecksumUtil(ChecksumUtil.Algorithm alg)
-
-
Method Detail
-
generateChecksum
public String generateChecksum(File file) throws IOException
- Throws:
IOException
-
generateChecksum
public String generateChecksum(InputStream inStream)
This method generates checksum of content in arg stream. Warning: this method is NOT threadsafe.- Parameters:
inStream- Content used as target of checksum.- Returns:
- string representation of the generated checksum.
-
generateChecksum
public String generateChecksum(String string)
This method generates the checksum of a String and returns a hex-encoded String as the checksum value- Parameters:
string- Content used as target of checksum.- Returns:
- string representation of the generated checksum using hex encoding
-
generateChecksumBase64
public String generateChecksumBase64(String string)
This method generates the checksum of a String and returns a base64-encoded String as the checksum value- Parameters:
string- Content used as target of checksum.- Returns:
- string representation of the generated checksum using base64 encoding
-
wrapStream
public static DigestInputStream wrapStream(InputStream inStream, ChecksumUtil.Algorithm algorithm)
Wraps an InputStream with a DigestInputStream in order to compute a checksum as the stream is being read.- Parameters:
inStream- The stream to wrapalgorithm- The algorithm used to compute the digest- Returns:
- The original stream wrapped as a DigestInputStream
-
getChecksum
public static String getChecksum(DigestInputStream digestStream)
Determines the checksum value of a DigestInputStream's underlying stream after the stream has been read.- Parameters:
digestStream-- Returns:
- The checksum value of the stream's contents
-
getChecksumBytes
public static byte[] getChecksumBytes(DigestInputStream digestStream)
Determines the checksum value of a DigestInputStream's underlying stream after the stream has been read.- Parameters:
digestStream-- Returns:
- The checksum value of the stream's contents
-
checksumBytesToString
public static String checksumBytesToString(byte[] digestBytes)
Converts a message digest byte array into a String based on the hex values appearing in the array.
-
hexStringToByteArray
public static byte[] hexStringToByteArray(String s)
-
convertToBase64Encoding
public static String convertToBase64Encoding(String hexEncodedChecksum)
Converts a hex-encoded checksum (like that produced by the getChecksum and generateChecksum methods of this class) to a base64-encoded checksum- Parameters:
hexEncodedChecksum- hex-encoded checksum- Returns:
- base64-encoded checksum
-
-