Class ChecksumUtil


  • public class ChecksumUtil
    extends Object
    A utility class for generating checksums. Beware this class it not threadsafe.
    Author:
    Daniel Bernstein
    • Method Detail

      • 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 wrap
        algorithm - 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