Class HashUtil

java.lang.Object
ca.corbett.extras.HashUtil

public final class HashUtil extends Object
A simple wrapper class to wrap and simplify hashing functionality a little.
Since:
2012-08-29 (originally written for ICE and then generalized much later)
Author:
scorbo2
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    byteArrayToHexString(byte[] data)
    Converts a byte array into a hex-encoded printable string.
    static byte[]
    getHash(byte[] data)
    Returns a digest of the data in the given byte array using SHA-1.
    static byte[]
    getHash(HashUtil.HashType hashType, byte[] data)
    Returns a digest of the data in the given byte array using the given HashType.
    static byte[]
    getHash(HashUtil.HashType hashType, File file)
    Returns a digest of the contents of the given file, using the given HashType.
    static byte[]
    getHash(File file)
    Returns a digest of the contents of the given file.
    static String
    getHashString(byte[] data)
    Hashes the given byte array and then returns a printable string (hex format) of the given byte array, using SHA-1 as the hash type.
    static String
    getHashString(HashUtil.HashType hashType, byte[] data)
    Hashes the given byte array using the given HashType and then returns a printable string (hex format) of the output.
    static String
    Returns a printable string (hex format) of the hash of the given file contents, using the given HashType.
    static String
    Returns a printable string (hex format) of the hash of the given file contents.

    Methods inherited from class java.lang.Object

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

    • getHash

      public static byte[] getHash(HashUtil.HashType hashType, byte[] data)
      Returns a digest of the data in the given byte array using the given HashType.
      Parameters:
      hashType - The HashType to use.
      data - A byte array of any length.
      Returns:
      A hash of the input array using the specified digest algorithm.
    • getHash

      public static byte[] getHash(byte[] data)
      Returns a digest of the data in the given byte array using SHA-1.
      Parameters:
      data - A byte array of any length.
      Returns:
      A SHA-1 hash of the input array.
    • getHash

      public static byte[] getHash(HashUtil.HashType hashType, File file) throws IOException
      Returns a digest of the contents of the given file, using the given HashType.
      Parameters:
      hashType - The HashType to use.
      file - The input file. Must exist and be readable.
      Returns:
      A hash of the file contents, in the specified digest type.
      Throws:
      IOException - If the file does not exist or is not readable.
    • getHash

      public static byte[] getHash(File file) throws IOException
      Returns a digest of the contents of the given file. File must exist and be readable. Uses SHA-1 as the default hash type.
      Parameters:
      file - The input file. Must exist and be readable.
      Returns:
      A SHA-1 hash of the file contents.
      Throws:
      IOException - If the file does not exist or is not readable.
    • getHashString

      public static String getHashString(HashUtil.HashType hashType, byte[] data)
      Hashes the given byte array using the given HashType and then returns a printable string (hex format) of the output.
      Parameters:
      hashType - The HashType to use.
      data - A byte array of any length.
      Returns:
      A hex-encoded printable string equivalent to the hash of the input.
    • getHashString

      public static String getHashString(byte[] data)
      Hashes the given byte array and then returns a printable string (hex format) of the given byte array, using SHA-1 as the hash type.
      Parameters:
      data - A byte array of any length.
      Returns:
      A hex-encoded printable string equivalent to the hash of the input.
    • getHashString

      public static String getHashString(HashUtil.HashType hashType, File file) throws IOException
      Returns a printable string (hex format) of the hash of the given file contents, using the given HashType.
      Parameters:
      hashType - The HashType to use.
      file - The input file in question. Must exist and be readable.
      Returns:
      A hex-encoded printable string of the hash of the file contents.
      Throws:
      IOException - If the file does not exist or is not readable.
    • getHashString

      public static String getHashString(File file) throws IOException
      Returns a printable string (hex format) of the hash of the given file contents.
      Parameters:
      file - The input file in question. Must exist and be readable.
      Returns:
      A hex-encoded printable string equivalent of the hash of the file contents.
      Throws:
      IOException - If the file does not exist or is not readable.
    • byteArrayToHexString

      public static String byteArrayToHexString(byte[] data)
      Converts a byte array into a hex-encoded printable string. As of swing-extras 2.4, this method simply delegates to the java.util.HexFormat class, which is available as of Java 17.
      Parameters:
      data - An input array of any length.
      Returns:
      A printable hex-encoded string equivalent.