Class BCryptHashBean

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

public class BCryptHashBean extends Object implements HashBean<String>
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
Author:
Middleware Services
  • 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.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    boolean
    compare(String hash, Object... data)
    Compares a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy with the computed hash from the given password.
    hash(Object... data)
    Compute a bcrypt hash of the form $2n$cost$xxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy given a salt and a password.
    void
    setCost(int costFactor)
    Sets the bcrypt cost factor.
    void
    Sets the bcrypt version.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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].
  • Method Details

    • setCost

      public void setCost(int costFactor)
      Sets the bcrypt cost factor.
      Parameters:
      costFactor - BCrypt cost in the range [4, 31].
    • setVersion

      public void setVersion(String ver)
      Sets the bcrypt version.
      Parameters:
      ver - Bcrypt version, e.g. "2b"
    • 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<String>
      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(String 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<String>
      Parameters:
      data - A 1-element array containing password.
      hash - Known hash value.
      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.