Package org.cryptacular.bean
Class EncodingHashBean
java.lang.Object
org.cryptacular.bean.AbstractHashBean
org.cryptacular.bean.EncodingHashBean
Computes a hash in an encoded format, e.g. hex, base64.
- Author:
- Middleware Services
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance.EncodingHashBean
(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec) Creates a new instance that will not be salted.EncodingHashBean
(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec, int iterations) Creates a new instance that will not be salted.EncodingHashBean
(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec, int iterations, boolean salted) Creates a new instance by specifying all properties. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Compares a known hash value with the hash of the given data.Hashes the given data.boolean
isSalted()
Whether data provided tohash(Object...)
includes a salt as the last parameter.void
setCodecSpec
(Spec<Codec> codecSpec) Sets the codec specification that determines the encoding applied to the hash output bytes.void
setSalted
(boolean salted) Sets whetherhash(Object...)
should expect a salt as the last parameter.Methods inherited from class org.cryptacular.bean.AbstractHashBean
compareInternal, getDigestSpec, getIterations, hashInternal, setDigestSpec, setIterations
-
Constructor Details
-
EncodingHashBean
public EncodingHashBean()Creates a new instance. -
EncodingHashBean
Creates a new instance that will not be salted. Delegates toEncodingHashBean(Spec, Spec, int, boolean)
.- Parameters:
codecSpec
- Digest specification.digestSpec
- Digest specification.
-
EncodingHashBean
public EncodingHashBean(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec, int iterations) Creates a new instance that will not be salted. Delegates toEncodingHashBean(Spec, Spec, int, boolean)
.- Parameters:
codecSpec
- Digest specification.digestSpec
- Digest specification.iterations
- Number of hash rounds.
-
EncodingHashBean
public EncodingHashBean(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec, int iterations, boolean salted) Creates a new instance by specifying all properties.- Parameters:
codecSpec
- Digest specification.digestSpec
- Digest specification.iterations
- Number of hash rounds.salted
- Whether hash data will be salted.
-
-
Method Details
-
getCodecSpec
- Returns:
- Codec specification that determines the encoding applied to the hash output bytes.
-
setCodecSpec
Sets the codec specification that determines the encoding applied to the hash output bytes.- Parameters:
codecSpec
- Codec specification, e.g.CodecSpec.BASE64
,CodecSpec.HEX
.
-
isSalted
public boolean isSalted()Whether data provided tohash(Object...)
includes a salt as the last parameter.- Returns:
- whether hash data includes a salt
-
setSalted
public void setSalted(boolean salted) Sets whetherhash(Object...)
should expect a salt as the last parameter.- Parameters:
salted
- whether hash data includes a salt
-
hash
Hashes the given data. IfisSalted()
is true then the last parameter MUST be a byte array containing the salt. The salt value will be appended to the encoded hash that is returned.- Specified by:
hash
in interfaceHashBean<String>
- Parameters:
data
- Data to hash.- Returns:
- Encoded digest output, including a salt if provided.
- Throws:
CryptoException
- on hash computation errors.EncodingException
- on encoding errors.StreamException
- on stream IO errors.
-
compare
public boolean compare(String hash, Object... data) throws CryptoException, EncodingException, StreamException Compares a known hash value with the hash of the given data.- Specified by:
compare
in interfaceHashBean<String>
- Parameters:
hash
- Known encoded hash value. If the length of the hash bytes after decoding 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.data
- Data to hash.- Returns:
- True if the hashed data matches the given hash, false otherwise.
- Throws:
CryptoException
- on hash computation errors.EncodingException
- on encoding errors.StreamException
- on stream IO errors.
-