Class BcryptHasher
- All Implemented Interfaces:
PasswordHasher
PasswordHasher.
This is the recommended password hashing implementation. It uses the bcrypt algorithm with a configurable cost parameter that controls the computational expense of hashing. Higher cost values increase resistance to brute-force attacks but take longer to compute.
The default cost is 12, which provides a good balance between security and performance. Each increment doubles the computation time.
Usage:
// Default cost (12)
PasswordHasher hasher = new BcryptHasher();
// Custom cost
PasswordHasher hasher = new BcryptHasher(14);
String hash = hasher.hash("myPassword123");
boolean valid = hasher.verify("myPassword123", hash);
Cost parameter guidelines:
- 10 — Fast, suitable for development/testing
- 12 — Default, good balance for most applications
- 14 — Higher security for sensitive applications
- 16+ — Very slow, only for extremely sensitive data
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault bcrypt cost parameter.static final intMaximum allowed cost parameter.static final intMinimum allowed cost parameter. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a BcryptHasher with the default cost of 12.BcryptHasher(int cost) Creates a BcryptHasher with the specified cost parameter. -
Method Summary
-
Field Details
-
DEFAULT_COST
public static final int DEFAULT_COSTDefault bcrypt cost parameter.- See Also:
-
MIN_COST
public static final int MIN_COSTMinimum allowed cost parameter.- See Also:
-
MAX_COST
public static final int MAX_COSTMaximum allowed cost parameter.- See Also:
-
-
Constructor Details
-
BcryptHasher
public BcryptHasher()Creates a BcryptHasher with the default cost of 12. -
BcryptHasher
public BcryptHasher(int cost) Creates a BcryptHasher with the specified cost parameter.- Parameters:
cost- the cost parameter (4–31); each increment doubles computation time- Throws:
IllegalArgumentException- if cost is outside the valid range
-
-
Method Details
-
hash
Hashes a plaintext password.Implementations must generate a cryptographically random salt and include it in the returned hash string so that
PasswordHasher.verify(String, String)can extract it for comparison.- Specified by:
hashin interfacePasswordHasher- Parameters:
password- the plaintext password to hash- Returns:
- the hashed password string (includes salt and algorithm parameters)
-
verify
Verifies a plaintext password against a previously hashed value.Implementations must use constant-time comparison to prevent timing attacks.
Uses constant-time comparison via
MessageDigest.isEqual(byte[], byte[])to prevent timing attacks.- Specified by:
verifyin interfacePasswordHasher- Parameters:
password- the plaintext password to verifyhash- the previously hashed password to compare against- Returns:
trueif the password matches the hash,falseotherwise
-
getCost
public int getCost()Returns the cost parameter used by this hasher.- Returns:
- the cost parameter
-