Package ca.corbett.extras
Class HashUtil
java.lang.Object
ca.corbett.extras.HashUtil
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbyteArrayToHexString(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[]Returns a digest of the contents of the given file.static StringgetHashString(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 StringgetHashString(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 StringgetHashString(HashUtil.HashType hashType, File file) Returns a printable string (hex format) of the hash of the given file contents, using the given HashType.static StringgetHashString(File file) Returns a printable string (hex format) of the hash of the given file contents.
-
Method Details
-
getHash
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
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
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
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
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
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
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
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.
-