Class EncodingHashBean

java.lang.Object
org.cryptacular.bean.AbstractHashBean
org.cryptacular.bean.EncodingHashBean
All Implemented Interfaces:
HashBean<String>

public class EncodingHashBean extends AbstractHashBean implements HashBean<String>
Computes a hash in an encoded format, e.g. hex, base64.
Author:
Middleware Services
  • Constructor Details

    • EncodingHashBean

      public EncodingHashBean()
      Creates a new instance.
    • EncodingHashBean

      public EncodingHashBean(Spec<Codec> codecSpec, Spec<org.bouncycastle.crypto.Digest> digestSpec)
      Creates a new instance that will not be salted. Delegates to EncodingHashBean(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 to EncodingHashBean(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

      public Spec<Codec> getCodecSpec()
      Returns:
      Codec specification that determines the encoding applied to the hash output bytes.
    • setCodecSpec

      public void setCodecSpec(Spec<Codec> codecSpec)
      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 to hash(Object...) includes a salt as the last parameter.
      Returns:
      whether hash data includes a salt
    • setSalted

      public void setSalted(boolean salted)
      Sets whether hash(Object...) should expect a salt as the last parameter.
      Parameters:
      salted - whether hash data includes a salt
    • hash

      Hashes the given data. If isSalted() 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 interface HashBean<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 interface HashBean<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.