Class HashUtil

java.lang.Object
org.cryptacular.util.HashUtil

public final class HashUtil extends Object
Utility class for computing cryptographic hashes.
Author:
Middleware Services
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    compareHash(org.bouncycastle.crypto.Digest digest, byte[] hash, int iterations, Object... data)
    Determines whether the hash of the given input equals a known value.
    static boolean
    compareHash(org.bouncycastle.crypto.Digest digest, SaltedHash hash, int iterations, boolean saltAfterData, Object... data)
    Determines whether the salted hash of the given input equals a known hash value.
    static byte[]
    hash(org.bouncycastle.crypto.Digest digest, int iterations, Object... data)
    Computes the iterated hash of the given data using the given algorithm.
    static byte[]
    hash(org.bouncycastle.crypto.Digest digest, Object... data)
    Computes the hash of the given data using the given algorithm.
    static byte[]
    sha1(Object... data)
    Produces the SHA-1 hash of the given data.
    static byte[]
    sha256(Object... data)
    Produces the SHA-256 hash of the given data.
    static byte[]
    sha3(int bitLength, Object... data)
    Produces the SHA-3 hash of the given data.
    static byte[]
    sha512(Object... data)
    Produces the SHA-512 hash of the given data.

    Methods inherited from class java.lang.Object

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

    • hash

      public static byte[] hash(org.bouncycastle.crypto.Digest digest, Object... data) throws CryptoException, StreamException
      Computes the hash of the given data using the given algorithm. A salted hash may be produced as follows:
             // data is a byte array containing raw data to digest
             final byte[] salt = new RBGNonce(16).generate();
             final byte[] hash = HashUtil.hash(new SHA1Digest(), data, salt);
       
      Parameters:
      digest - Hash algorithm.
      data - Data to hash. Supported types are byte[], CharSequence ,InputStream, and Resource. Character data is processed in the UTF-8 character set; if another character set is desired, the caller should convert to byte[] and provide the resulting bytes.
      Returns:
      Byte array of length Digest.getDigestSize() containing hash output.
      Throws:
      CryptoException - on hash computation errors.
      StreamException - on stream IO errors.
    • hash

      public static byte[] hash(org.bouncycastle.crypto.Digest digest, int iterations, Object... data) throws CryptoException, StreamException
      Computes the iterated hash of the given data using the given algorithm. The following example demonstrates a typical usage pattern, a salted hash with 10 rounds:
             // data is a byte array containing raw data to digest
             final byte[] salt = new RBGNonce(16).generate();
             final byte[] hash = HashUtil.hash(new SHA1Digest(), 10, data, salt);
       
      Parameters:
      digest - Hash algorithm.
      iterations - Number of hash rounds. Must be positive value.
      data - Data to hash. Supported types are byte[], CharSequence ,InputStream, and Resource. Character data is processed in the UTF-8 character set; if another character set is desired, the caller should convert to byte[] and provide the resulting bytes.
      Returns:
      Byte array of length Digest.getDigestSize() containing hash output.
      Throws:
      CryptoException - on hash computation errors.
      StreamException - on stream IO errors.
    • compareHash

      public static boolean compareHash(org.bouncycastle.crypto.Digest digest, byte[] hash, int iterations, Object... data) throws CryptoException, StreamException
      Determines whether the hash of the given input equals a known value.
      Parameters:
      digest - Hash algorithm.
      hash - Hash to compare with. If the length of the array is greater than the length of the digest output, anything beyond the digest length is considered salt data that is hashed after the input data.
      iterations - Number of hash rounds.
      data - Data to hash.
      Returns:
      True if the hash of the data under the given digest is equal to the hash, false otherwise.
      Throws:
      CryptoException - on hash computation errors.
      StreamException - on stream IO errors.
    • compareHash

      public static boolean compareHash(org.bouncycastle.crypto.Digest digest, SaltedHash hash, int iterations, boolean saltAfterData, Object... data) throws CryptoException, StreamException
      Determines whether the salted hash of the given input equals a known hash value.
      Parameters:
      digest - Hash algorithm.
      hash - Salted hash data.
      iterations - Number of hash rounds.
      saltAfterData - True to apply salt after data, false to apply salt before data.
      data - Data to hash, which should NOT include the salt value.
      Returns:
      True if the hash of the data under the given digest is equal to the hash, false otherwise.
      Throws:
      CryptoException - on hash computation errors.
      StreamException - on stream IO errors.
    • sha1

      public static byte[] sha1(Object... data)
      Produces the SHA-1 hash of the given data.
      Parameters:
      data - Data to hash. See hash(Digest, Object...) for supported inputs.
      Returns:
      20-byte array containing hash output.
      See Also:
    • sha256

      public static byte[] sha256(Object... data)
      Produces the SHA-256 hash of the given data.
      Parameters:
      data - Data to hash. See hash(Digest, Object...) for supported inputs.
      Returns:
      32-byte array containing hash output.
      See Also:
    • sha512

      public static byte[] sha512(Object... data)
      Produces the SHA-512 hash of the given data.
      Parameters:
      data - Data to hash. See hash(Digest, Object...) for supported inputs.
      Returns:
      64-byte array containing hash output.
      See Also:
    • sha3

      public static byte[] sha3(int bitLength, Object... data)
      Produces the SHA-3 hash of the given data.
      Parameters:
      bitLength - One of the supported SHA-3 output bit lengths: 224, 256, 384, or 512.
      data - Data to hash. See hash(Digest, Object...) for supported inputs.
      Returns:
      Byte array of size bitLength containing hash output.
      See Also: