Class Argon2Hash
- All Implemented Interfaces:
PasswordHash
Argon2 is a modern password hashing algorithm that won the Password Hashing Competition (PHC) in 2015. It is designed to be resistant to GPU cracking attacks and side-channel attacks. This implementation supports all three variants: Argon2d, Argon2i, and Argon2id (recommended).
Argon2 是一种现代密码哈希算法,在 2015 年赢得密码哈希竞赛(PHC)。 它被设计为抵抗 GPU 破解攻击和旁道攻击。此实现支持所有三种变体:Argon2d、Argon2i 和 Argon2id(推荐)。
Important: This implementation requires Bouncy Castle library (bcprov-jdk18on).
If Bouncy Castle is not available, operations will throw OpenCryptoException.
重要:此实现需要 Bouncy Castle 库(bcprov-jdk18on)。
如果 Bouncy Castle 不可用,操作将抛出 OpenCryptoException。
Usage example:
// Create Argon2id hasher with default parameters (recommended)
PasswordHash hasher = Argon2Hash.argon2id();
// Hash a password
String hash = hasher.hash("myPassword123");
// Verify a password
boolean valid = hasher.verify("myPassword123", hash);
// Check if rehashing is needed
if (hasher.needsRehash(hash)) {
String newHash = hasher.hash("myPassword123");
}
Features | 主要功能:
- Argon2id, Argon2i, Argon2d variants - Argon2id、Argon2i、Argon2d 变体
- Memory-hard password hashing - 内存密集型密码哈希
- Configurable time/memory/parallelism - 可配置时间/内存/并行度
Usage Examples | 使用示例:
Argon2Hash argon2 = Argon2Hash.argon2id();
String hash = argon2.hash("password");
boolean valid = argon2.verify("password", hash);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(memory * iterations) - 时间复杂度: O(memory * iterations),memory为内存参数
- Space complexity: O(memory) - 空间复杂度: O(memory)
- 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 Argon2 hashers 用于创建自定义 Argon2 哈希器的构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic Argon2Hashargon2d()Create Argon2d hasher with default parameters (resistant to GPU attacks) 使用默认参数创建 Argon2d 哈希器(抵抗 GPU 攻击)static Argon2Hashargon2i()Create Argon2i hasher with default parameters (resistant to side-channel attacks) 使用默认参数创建 Argon2i 哈希器(抵抗旁道攻击)static Argon2Hashargon2id()Create Argon2id hasher with default parameters (recommended for most use cases) 使用默认参数创建 Argon2id 哈希器(推荐用于大多数用例)static Argon2Hash.Builderbuilder()Create a builder for custom Argon2 configuration 创建用于自定义 Argon2 配置的构建器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 使用字符串验证密码与哈希值
-
Method Details
-
argon2id
Create Argon2id hasher with default parameters (recommended for most use cases) 使用默认参数创建 Argon2id 哈希器(推荐用于大多数用例)- Returns:
- Argon2id password hasher
-
argon2i
Create Argon2i hasher with default parameters (resistant to side-channel attacks) 使用默认参数创建 Argon2i 哈希器(抵抗旁道攻击)- Returns:
- Argon2i password hasher
-
argon2d
Create Argon2d hasher with default parameters (resistant to GPU attacks) 使用默认参数创建 Argon2d 哈希器(抵抗 GPU 攻击)- Returns:
- Argon2d password hasher
-
builder
Create a builder for custom Argon2 configuration 创建用于自定义 Argon2 配置的构建器- 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
-