Package org.cryptacular.util
Class HashUtil
java.lang.Object
org.cryptacular.util.HashUtil
Utility class for computing cryptographic hashes.
- Author:
- Middleware Services
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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[]
Computes the iterated hash of the given data using the given algorithm.static byte[]
Computes the hash of the given data using the given algorithm.static byte[]
Produces the SHA-1 hash of the given data.static byte[]
Produces the SHA-256 hash of the given data.static byte[]
Produces the SHA-3 hash of the given data.static byte[]
Produces the SHA-512 hash of the given data.
-
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 arebyte[]
,CharSequence
,InputStream
, andResource
. Character data is processed in theUTF-8
character set; if another character set is desired, the caller should convert tobyte[]
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 arebyte[]
,CharSequence
,InputStream
, andResource
. Character data is processed in theUTF-8
character set; if another character set is desired, the caller should convert tobyte[]
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
Produces the SHA-1 hash of the given data.- Parameters:
data
- Data to hash. Seehash(Digest, Object...)
for supported inputs.- Returns:
- 20-byte array containing hash output.
- See Also:
-
sha256
Produces the SHA-256 hash of the given data.- Parameters:
data
- Data to hash. Seehash(Digest, Object...)
for supported inputs.- Returns:
- 32-byte array containing hash output.
- See Also:
-
sha512
Produces the SHA-512 hash of the given data.- Parameters:
data
- Data to hash. Seehash(Digest, Object...)
for supported inputs.- Returns:
- 64-byte array containing hash output.
- See Also:
-
sha3
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. Seehash(Digest, Object...)
for supported inputs.- Returns:
- Byte array of size
bitLength
containing hash output. - See Also:
-