Class BCryptHash
- All Implemented Interfaces:
PasswordHash
BCrypt is a password hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher. It incorporates a salt to protect against rainbow table attacks and is adaptive, meaning the iteration count can be increased to make it slower as computers get faster.
BCrypt 是由 Niels Provos 和 David Mazières 设计的密码哈希函数, 基于 Blowfish 密码。它包含盐值以防止彩虹表攻击,并且是自适应的, 这意味着可以增加迭代次数使其变慢,以应对计算机速度的提升。
BCrypt uses a work factor (cost) parameter that determines the number of iterations. The number of iterations is 2^cost. A cost of 12 means 4096 iterations.
BCrypt 使用工作因子(成本)参数来确定迭代次数。 迭代次数为 2^成本。成本为 12 表示 4096 次迭代。
Usage example:
// Create BCrypt hasher with default cost (12)
PasswordHash hasher = BCryptHash.create();
// Hash a password
String hash = hasher.hash("myPassword123");
// Verify a password
boolean valid = hasher.verify("myPassword123", hash);
// Check if rehashing is needed (cost increased)
if (hasher.needsRehash(hash)) {
String newHash = hasher.hash("myPassword123");
}
Features | 主要功能:
- BCrypt password hashing - BCrypt 密码哈希
- Configurable cost factor (4-31) - 可配置代价因子(4-31)
Usage Examples | 使用示例:
BCryptHash bcrypt = BCryptHash.create();
String hash = bcrypt.hash("password");
boolean valid = bcrypt.verify("password", hash);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(2^cost) - 时间复杂度: O(2^cost),cost为工作因子
- Space complexity: O(1) - 空间复杂度: O(1)
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creating customized BCrypt hashers 用于创建自定义 BCrypt 哈希器的构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic BCryptHash.Builderbuilder()Create a builder for custom BCrypt configuration 创建用于自定义 BCrypt 配置的构建器static BCryptHashcreate()Create BCrypt hasher with default cost factor (12) 使用默认成本因子(12)创建 BCrypt 哈希器Get the algorithm name for this password hash implementation 获取此密码哈希实现的算法名称hash(char[] password) Hash a password from a character array 从字符数组哈希密码Hash a password from a string 从字符串哈希密码booleanneedsRehash(String hash) Check if a hash needs to be rehashed with current parameters 检查哈希值是否需要使用当前参数重新哈希booleanVerify a password against a hash using character array 使用字符数组验证密码与哈希值booleanVerify a password against a hash using string 使用字符串验证密码与哈希值static BCryptHashwithCost(int cost) Create BCrypt hasher with custom cost factor 使用自定义成本因子创建 BCrypt 哈希器
-
Method Details
-
create
Create BCrypt hasher with default cost factor (12) 使用默认成本因子(12)创建 BCrypt 哈希器- Returns:
- BCrypt password hasher
-
withCost
Create BCrypt hasher with custom cost factor 使用自定义成本因子创建 BCrypt 哈希器- Parameters:
cost- the cost factor (4-31, recommended 12-14)- Returns:
- BCrypt password hasher
- Throws:
IllegalArgumentException- if cost is out of valid range
-
builder
Create a builder for custom BCrypt configuration 创建用于自定义 BCrypt 配置的构建器- Returns:
- builder instance
-
hash
Description copied from interface:PasswordHashHash a password from a character array 从字符数组哈希密码- Specified by:
hashin interfacePasswordHash- Parameters:
password- the password to hash (will not be modified)- Returns:
- the hash string (self-describing format)
-
hash
Description copied from interface:PasswordHashHash a password from a string 从字符串哈希密码- Specified by:
hashin interfacePasswordHash- Parameters:
password- the password to hash- Returns:
- the hash string (self-describing format)
-
verify
Description copied from interface:PasswordHashVerify a password against a hash using character array 使用字符数组验证密码与哈希值- Specified by:
verifyin interfacePasswordHash- Parameters:
password- the password to verify (will not be modified)hash- the hash to verify against- Returns:
- true if password matches the hash, false otherwise
-
verify
Description copied from interface:PasswordHashVerify a password against a hash using string 使用字符串验证密码与哈希值- Specified by:
verifyin interfacePasswordHash- Parameters:
password- the password to verifyhash- the hash to verify against- Returns:
- true if password matches the hash, false otherwise
-
needsRehash
Description copied from interface:PasswordHashCheck if a hash needs to be rehashed with current parameters 检查哈希值是否需要使用当前参数重新哈希Returns true if the hash was created with different parameters than the current instance, indicating the password should be rehashed on next successful authentication.
- Specified by:
needsRehashin interfacePasswordHash- Parameters:
hash- the hash to check- Returns:
- true if rehashing is needed, false otherwise
-
getAlgorithm
Description copied from interface:PasswordHashGet the algorithm name for this password hash implementation 获取此密码哈希实现的算法名称- Specified by:
getAlgorithmin interfacePasswordHash- Returns:
- algorithm name
-