Class BCryptHashBean

java.lang.Object
org.cryptacular.bean.BCryptHashBean
All Implemented Interfaces:
HashBean<CharSequence>

public class BCryptHashBean extends Object implements HashBean<CharSequence>
HashBean implementation that uses the bcrypt algorithm for hashing. Hash strings of the following format are supported:
$2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy where: n is an optional bcrypt algorithm version (typically "a" or "b") 4 ≤ cost ≤ 31 x is 22 characters of encoded salt y is 31 characters of encoded hash bytes

The encoding for salt and hash bytes is a variant of base-64 encoding without padding in the following alphabet:


./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Handles encoding and decoding a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Custom base-64 alphabet.
    private final int
    BCrypt cost factor in the range [4, 31].
    private static final int
    Default cost.
    private static final String
    Default version.
    private final String
    BCrypt version used when computing hashes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance.
    BCryptHashBean(int costFactor)
    Creates a new instance that uses the given cost factor when hashing.
    BCryptHashBean(int costFactor, String version)
    Creates a new instance that uses the given cost factor when hashing.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    compare(CharSequence hash, Object... data)
    Compares a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy with the computed hash from the given password.
    private static byte[]
    decode(String input, int length)
    Decodes an input string into a byte array using the configured decoder.
    private static String
    encode(byte[] bytes, int length)
    Encodes an input byte array into a string using the configured encoder.
    hash(Object... data)
    Compute a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy given a salt and a password.
    private static byte[]
    password(String version, Object data)
    Converts an input object into a password as an array of UTF-8 bytes.
    private static byte[]
    salt(Object data)
    Converts an input object into a salt as an array of bytes.

    Methods inherited from class java.lang.Object

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

    • ALPHABET

      private static final String ALPHABET
      Custom base-64 alphabet.
      See Also:
    • DEFAULT_COST

      private static final int DEFAULT_COST
      Default cost. Value is 12.
      See Also:
    • DEFAULT_VERSION

      private static final String DEFAULT_VERSION
      Default version. Value is '2b'.
      See Also:
    • costFactor

      private final int costFactor
      BCrypt cost factor in the range [4, 31]. Default value is .
    • version

      private final String version
      BCrypt version used when computing hashes. Default value is .
  • Constructor Details

    • BCryptHashBean

      public BCryptHashBean()
      Creates a new instance.
    • BCryptHashBean

      public BCryptHashBean(int costFactor)
      Creates a new instance that uses the given cost factor when hashing.
      Parameters:
      costFactor - BCrypt cost in the range [4, 31].
    • BCryptHashBean

      public BCryptHashBean(int costFactor, String version)
      Creates a new instance that uses the given cost factor when hashing.
      Parameters:
      costFactor - BCrypt cost in the range [4, 31].
      version - Bcrypt version, e.g. "2b"
  • Method Details

    • hash

      public String hash(Object... data) throws CryptoException
      Compute a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy given a salt and a password.
      Specified by:
      hash in interface HashBean<CharSequence>
      Parameters:
      data - A 2-element array containing salt and password. The salt may be encoded per the bcrypt standard or raw bytes.
      Returns:
      An encoded bcrypt hash, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy in the specification above.
      Throws:
      CryptoException - on bcrypt algorithm errors.
    • compare

      public boolean compare(CharSequence hash, Object... data) throws CryptoException, StreamException
      Compares a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy with the computed hash from the given password. The bcrypt algorithm parameters are derived from the reference bcrypt hash string.
      Specified by:
      compare in interface HashBean<CharSequence>
      Parameters:
      hash - Known hash value.
      data - A 1-element array containing password.
      Returns:
      True if the computed hash is exactly equal to the reference hash, false otherwise.
      Throws:
      CryptoException - on bcrypt algorithm errors.
      StreamException - on stream IO errors.
    • encode

      private static String encode(byte[] bytes, int length)
      Encodes an input byte array into a string using the configured encoder.
      Parameters:
      bytes - Input bytes to encode.
      length - Number of bytes of input to encode.
      Returns:
      Input encoded as a string.
    • decode

      private static byte[] decode(String input, int length)
      Decodes an input string into a byte array using the configured decoder.
      Parameters:
      input - Input string to decode.
      length - Desired output size in bytes.
      Returns:
      Input decoded as a byte array.
    • salt

      private static byte[] salt(Object data)
      Converts an input object into a salt as an array of bytes.
      Parameters:
      data - Input salt as a byte array or encoded string.
      Returns:
      Salt as byte array.
    • password

      private static byte[] password(String version, Object data)
      Converts an input object into a password as an array of UTF-8 bytes. A null terminator is added if the supplied data does not end with one.
      Parameters:
      version - Bcrypt version, e.g. "2a".
      data - Input password.
      Returns:
      Null terminated password as UTF-8 byte array.