Class HashUtil

java.lang.Object
de.tomatengames.util.HashUtil

public class HashUtil extends Object
Provides utilities to work with hashes.
Since:
1.0
  • Field Details

    • MD5

      public static final String MD5
      The name of the MD5 hash algorithm.

      All Java platforms before Java 14 are required to support this algorithm.

      See Also:
    • SHA1

      public static final String SHA1
      The name of the SHA-1 hash algorithm.

      All Java platforms are required to support this algorithm.

      See Also:
    • SHA256

      public static final String SHA256
      The name of the SHA-256 hash algorithm.

      All Java platforms are required to support this algorithm.

      See Also:
    • SHA512

      public static final String SHA512
      The name of the SHA-512 hash algorithm.

      The Java platform is not required to support this algorithm.

      See Also:
    • HMAC_MD5

      public static final String HMAC_MD5
      The name of the HMAC-MD5 MAC algorithm.

      All Java platforms before Java 14 are required to support this algorithm.

      Since:
      1.3
      See Also:
    • HMAC_SHA1

      public static final String HMAC_SHA1
      The name of the HMAC-SHA1 MAC algorithm.

      All Java platforms are required to support this algorithm.

      Since:
      1.3
      See Also:
    • HMAC_SHA256

      public static final String HMAC_SHA256
      The name of the HMAC-SHA256 MAC algorithm.

      All Java platforms are required to support this algorithm.

      Since:
      1.3
      See Also:
    • HMAC_SHA512

      public static final String HMAC_SHA512
      The name of the HMAC-SHA512 MAC algorithm.

      The Java platform is not required to support this algorithm.

      Since:
      1.3
      See Also:
  • Method Details

    • get

      public static MessageDigest get(String algorithm)
      Returns a MessageDigest object that implements the specified algorithm.
      Parameters:
      algorithm - The algorithm name.
      Returns:
      The MessageDigest object.
      Throws:
      IllegalArgumentException - If the algorithm is unknown.
      See Also:
    • getMac

      public static Mac getMac(String algorithm, byte[] key)
      Returns a Mac object that implements the specified algorithm using the specified key.
      Parameters:
      algorithm - The algorithm name.
      key - The key.
      Returns:
      The Mac object.
      Throws:
      IllegalArgumentException - If the algorithm is unknown.
      See Also:
    • getMD5

      public static MessageDigest getMD5()
      Returns a MessageDigest object that implements the MD5 algorithm.
      Returns:
      The MessageDigest object.
      Throws:
      IllegalArgumentException - If the MD5 algorithm is not supported by this runtime. All Java platforms before Java 14 are required to support this algorithm.
      Since:
      1.1
      See Also:
    • getSHA1

      public static MessageDigest getSHA1()
      Returns a MessageDigest object that implements the SHA-1 algorithm.
      Returns:
      The MessageDigest object.
      Since:
      1.1
      See Also:
    • getSHA256

      public static MessageDigest getSHA256()
      Returns a MessageDigest object that implements the SHA-256 algorithm.
      Returns:
      The MessageDigest object.
      Since:
      1.1
      See Also:
    • getSHA512

      public static MessageDigest getSHA512()
      Returns a MessageDigest object that implements the SHA-512 algorithm.
      Returns:
      The MessageDigest object.
      Throws:
      IllegalArgumentException - If the SHA-512 algorithm is not supported by this runtime.
      Since:
      1.1
      See Also:
    • getHmacMD5

      public static Mac getHmacMD5(byte[] key)
      Returns a new Mac object that implements the HMAC-MD5 algorithm using the specified key.
      Parameters:
      key - The key.
      Returns:
      The Mac object.
      Throws:
      IllegalArgumentException - If the HMAC-MD5 algorithm is not supported by this runtime.
      Since:
      1.3
      See Also:
    • getHmacSHA1

      public static Mac getHmacSHA1(byte[] key)
      Returns a new Mac object that implements the HMAC-SHA1 algorithm using the specified key.
      Parameters:
      key - The key.
      Returns:
      The Mac object.
      Since:
      1.3
      See Also:
    • getHmacSHA256

      public static Mac getHmacSHA256(byte[] key)
      Returns a new Mac object that implements the HMAC-SHA256 algorithm using the specified key.
      Parameters:
      key - The key.
      Returns:
      The Mac object.
      Since:
      1.3
      See Also:
    • getHmacSHA512

      public static Mac getHmacSHA512(byte[] key)
      Returns a new Mac object that implements the HMAC-SHA512 algorithm using the specified key.
      Parameters:
      key - The key.
      Returns:
      The Mac object.
      Throws:
      IllegalArgumentException - If the HMAC-SHA512 algorithm is not supported by this runtime.
      Since:
      1.3
      See Also:
    • hash

      public static byte[] hash(MessageDigest alg, byte[] bytes)
      Calculates the hash of the specified bytes and returns it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash.
      bytes - The bytes that should be hashed.
      Returns:
      The hash. Not null.
    • hash

      public static byte[] hash(MessageDigest alg, byte[]... bytes)
      Calculates the hash of the specified bytes and returns it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash.
      bytes - Multiple byte arrays that are handled as a single large one. Must not be null, but the inner arrays may be null. Inner arrays that are null are ignored.
      Returns:
      The hash. Not null.
    • hash

      public static byte[] hash(MessageDigest alg, InputStream input) throws IOException
      Reads data from the input stream and hashes it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash.
      input - The input stream from which the data should be read.
      Returns:
      The hash. Not null.
      Throws:
      IOException - If an I/O error occurs.
    • hash

      public static byte[] hash(MessageDigest alg, Path path) throws IOException
      Reads the content from the specified file and hashes it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash.
      path - A path to the file that should be read.
      Returns:
      The hash of the file's content. Not null.
      Throws:
      IOException - If an I/O error occurs.
    • updateUTF8

      public static void updateUTF8(MessageDigest alg, String str)
      UTF8-encodes the specified String and passes it to the specified MessageDigest.

      This method behaves like alg.update(str.getBytes(StandardCharsets.UTF_8)), but requires only constant memory for large strings.

      Parameters:
      alg - The MessageDigest that should be updated.
      str - The string that should be encoded.
      Since:
      1.3
    • hashUTF8

      public static byte[] hashUTF8(MessageDigest alg, String str)
      UTF8-encodes the specified String and hashes it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash. Must not be null.
      str - The string that should be encoded. Must not be null.
      Returns:
      The hash. Not null.
      Since:
      1.3
    • updateUTF8

      public static void updateUTF8(MessageDigest alg, char[] chars, int offset, int length)
      UTF8-encodes the specified char array and passes it to the specified MessageDigest.

      This method behaves like

      alg.update(new String(chars, offset, length).getBytes(StandardCharsets.UTF_8))
      
      but requires only constant memory for large strings and overwrites additional copies of the char array.
      Parameters:
      alg - The MessageDigest that should be updated. Must not be null.
      chars - The char array that should be encoded. Must not be null.
      offset - The first index of the char array that should be encoded. Must not be negative. Must fulfill offset+length <= chars.length.
      length - The amount of characters of the char array that should be encoded, starting at offset. Must not be negative.
      Since:
      1.4
    • updateUTF8

      public static void updateUTF8(MessageDigest alg, char[] chars)
      UTF8-encodes the specified char array and passes it to the specified MessageDigest.

      This method behaves like alg.update(new String(chars).getBytes(StandardCharsets.UTF_8)), but requires only constant memory for large strings and overwrites additional copies of the char array.

      Parameters:
      alg - The MessageDigest that should be updated. Must not be null.
      chars - The char array that should be encoded. Must not be null.
      Since:
      1.4
    • hashUTF8

      public static byte[] hashUTF8(MessageDigest alg, char[] chars)
      UTF8-encodes the specified char array and hashes it.
      Parameters:
      alg - The MessageDigest that should be used to calculate the hash. Must not be null.
      chars - The char array that should be encoded. Must not be null.
      Returns:
      The hash. Not null.
      Since:
      1.4