Class ChecksumCalculator


  • public final class ChecksumCalculator
    extends Object
    Calculates MD5 or SHA1 checksums on files using the built-in Java methods.
    • Constructor Detail

      • ChecksumCalculator

        public ChecksumCalculator()
    • 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 - An InputStream 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 - An InputStream 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
      • digestFile

        public static byte[] digestFile​(File src,
                                        String digestAlgorithm)
      • 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