Class Checksum


  • public final class Checksum
    extends java.lang.Object
    Includes methods to generate the MD5 and SHA1 checksum.
    Version:
    $Id: $Id
    Author:
    Jeremy Long
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getChecksum​(java.lang.String algorithm, byte[] bytes)
      Calculates the MD5 checksum of a specified bytes.
      static java.lang.String getChecksum​(java.lang.String algorithm, java.io.File file)
      Creates the cryptographic checksum of a given file using the specified algorithm.
      static java.lang.String getHex​(byte[] raw)
      Converts a byte array into a hex string.
      static java.lang.String getMD5Checksum​(java.io.File file)
      Calculates the MD5 checksum of a specified file.
      static java.lang.String getMD5Checksum​(java.lang.String text)
      Calculates the MD5 checksum of the specified text.
      static java.lang.String getSHA1Checksum​(java.io.File file)
      Calculates the SHA1 checksum of a specified file.
      static java.lang.String getSHA1Checksum​(java.lang.String text)
      Calculates the SHA1 checksum of the specified text.
      static java.lang.String getSHA256Checksum​(java.io.File file)
      Calculates the SH256 checksum of a specified file.
      static java.lang.String getSHA256Checksum​(java.lang.String text)
      Calculates the SHA256 checksum of the specified text.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getChecksum

        public static java.lang.String getChecksum​(java.lang.String algorithm,
                                                   java.io.File file)
                                            throws java.security.NoSuchAlgorithmException,
                                                   java.io.IOException

        Creates the cryptographic checksum of a given file using the specified algorithm.

        Parameters:
        algorithm - the algorithm to use to calculate the checksum
        file - the file to calculate the checksum for
        Returns:
        the checksum
        Throws:
        java.io.IOException - when the file does not exist
        java.security.NoSuchAlgorithmException - when an algorithm is specified that does not exist
      • getMD5Checksum

        public static java.lang.String getMD5Checksum​(java.io.File file)
                                               throws java.io.IOException,
                                                      java.security.NoSuchAlgorithmException
        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:
        java.io.IOException - when the file passed in does not exist
        java.security.NoSuchAlgorithmException - when the MD5 algorithm is not available
      • getSHA1Checksum

        public static java.lang.String getSHA1Checksum​(java.io.File file)
                                                throws java.io.IOException,
                                                       java.security.NoSuchAlgorithmException
        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:
        java.io.IOException - when the file passed in does not exist
        java.security.NoSuchAlgorithmException - when the SHA1 algorithm is not available
      • getSHA256Checksum

        public static java.lang.String getSHA256Checksum​(java.io.File file)
                                                  throws java.io.IOException,
                                                         java.security.NoSuchAlgorithmException
        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:
        java.io.IOException - when the file passed in does not exist
        java.security.NoSuchAlgorithmException - when the SHA1 algorithm is not available
      • getChecksum

        public static java.lang.String getChecksum​(java.lang.String algorithm,
                                                   byte[] bytes)
        Calculates the MD5 checksum of a specified bytes.
        Parameters:
        algorithm - the algorithm to use (md5, sha1, etc.) to calculate the message digest
        bytes - the bytes to generate the MD5 checksum
        Returns:
        the hex representation of the MD5 hash
      • getMD5Checksum

        public static java.lang.String getMD5Checksum​(java.lang.String text)
        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

        public static java.lang.String getSHA1Checksum​(java.lang.String text)
        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

        public static java.lang.String getSHA256Checksum​(java.lang.String text)
        Calculates the SHA256 checksum of the specified text.
        Parameters:
        text - the text to generate the SHA1 checksum
        Returns:
        the hex representation of the SHA1
      • getHex

        public static java.lang.String getHex​(byte[] raw)

        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