Class OpenPasswordHash
java.lang.Object
cloud.opencode.base.crypto.OpenPasswordHash
Password hashing facade for secure password storage - Provides convenient API for various password hashing algorithms
密码哈希门面类 - 为各种密码哈希算法提供便捷的 API
Features | 主要功能:
- Argon2id password hashing (recommended) - Argon2id 密码哈希(推荐)
- BCrypt, SCrypt, PBKDF2 support - BCrypt、SCrypt、PBKDF2 支持
- Password verification with constant-time comparison - 使用常量时间比较的密码验证
- Hash upgrade detection - 哈希升级检测
Usage Examples | 使用示例:
// Hash a password
OpenPasswordHash hasher = OpenPasswordHash.argon2();
String hash = hasher.hash("myPassword");
// Verify a password
boolean valid = hasher.verify("myPassword", hash);
// Check if rehash is needed
boolean needsRehash = hasher.needsRehash(hash);
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(cost) - 时间复杂度: O(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:
-
Method Summary
Modifier and TypeMethodDescriptionstatic OpenPasswordHashargon2()Create Argon2id password hasher (recommended) 创建 Argon2id 密码哈希器(推荐)static OpenPasswordHashbcrypt()Create BCrypt password hasher 创建 BCrypt 密码哈希器static OpenPasswordHashbcrypt(int cost) Create BCrypt password hasher with custom cost 创建自定义代价的 BCrypt 密码哈希器static PasswordPolicyGet default password policy 获取默认密码策略Get algorithm name 获取算法名称hash(char[] password) Hash password from char array (more secure) 从字符数组哈希密码(更安全)Hash password 哈希密码booleanneedsRehash(String hash) Check if hash needs to be upgraded 检查哈希是否需要升级static OpenPasswordHashof(PasswordHashAlgorithm algorithm) Create password hasher by algorithm enum 根据算法枚举创建密码哈希器static OpenPasswordHashpbkdf2()Create PBKDF2 password hasher 创建 PBKDF2 密码哈希器static OpenPasswordHashpbkdf2(int iterations) Create PBKDF2 password hasher with custom iterations 创建自定义迭代次数的 PBKDF2 密码哈希器static OpenPasswordHashscrypt()Create SCrypt password hasher 创建 SCrypt 密码哈希器static PasswordPolicyGet strong password policy 获取强密码策略booleanVerify password from char array against hash 验证字符数组密码与哈希booleanVerify password against hash 验证密码与哈希
-
Method Details
-
argon2
Create Argon2id password hasher (recommended) 创建 Argon2id 密码哈希器(推荐)- Returns:
- OpenPasswordHash instance
-
bcrypt
Create BCrypt password hasher 创建 BCrypt 密码哈希器- Returns:
- OpenPasswordHash instance
-
bcrypt
Create BCrypt password hasher with custom cost 创建自定义代价的 BCrypt 密码哈希器- Parameters:
cost- cost factor (4-31)- Returns:
- OpenPasswordHash instance
-
scrypt
Create SCrypt password hasher 创建 SCrypt 密码哈希器- Returns:
- OpenPasswordHash instance
-
pbkdf2
Create PBKDF2 password hasher 创建 PBKDF2 密码哈希器- Returns:
- OpenPasswordHash instance
-
pbkdf2
Create PBKDF2 password hasher with custom iterations 创建自定义迭代次数的 PBKDF2 密码哈希器- Parameters:
iterations- number of iterations- Returns:
- OpenPasswordHash instance
-
of
Create password hasher by algorithm enum 根据算法枚举创建密码哈希器- Parameters:
algorithm- password hash algorithm- Returns:
- OpenPasswordHash instance
-
hash
-
hash
Hash password from char array (more secure) 从字符数组哈希密码(更安全)- Parameters:
password- password to hash- Returns:
- hash string (includes algorithm info and salt)
-
verify
-
verify
Verify password from char array against hash 验证字符数组密码与哈希- Parameters:
password- password to verifyhash- stored hash- Returns:
- true if password matches
-
needsRehash
Check if hash needs to be upgraded 检查哈希是否需要升级- Parameters:
hash- hash to check- Returns:
- true if hash needs to be rehashed with current settings
-
defaultPolicy
Get default password policy 获取默认密码策略- Returns:
- default password policy
-
strongPolicy
Get strong password policy 获取强密码策略- Returns:
- strong password policy
-
getAlgorithm
-