Class Argon2Hash

java.lang.Object
cloud.opencode.base.crypto.password.Argon2Hash
All Implemented Interfaces:
PasswordHash

public final class Argon2Hash extends Object implements PasswordHash
Argon2 password hashing implementation - Modern memory-hard password hashing algorithm (requires Bouncy Castle) Argon2 密码哈希实现 - 现代内存困难密码哈希算法(需要 Bouncy Castle)

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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builder for creating customized Argon2 hashers 用于创建自定义 Argon2 哈希器的构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    static Argon2Hash
    Create Argon2d hasher with default parameters (resistant to GPU attacks) 使用默认参数创建 Argon2d 哈希器(抵抗 GPU 攻击)
    static Argon2Hash
    Create Argon2i hasher with default parameters (resistant to side-channel attacks) 使用默认参数创建 Argon2i 哈希器(抵抗旁道攻击)
    static Argon2Hash
    Create Argon2id hasher with default parameters (recommended for most use cases) 使用默认参数创建 Argon2id 哈希器(推荐用于大多数用例)
    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(String password)
    Hash a password from a string 从字符串哈希密码
    boolean
    Check if a hash needs to be rehashed with current parameters 检查哈希值是否需要使用当前参数重新哈希
    boolean
    verify(char[] password, String hash)
    Verify a password against a hash using character array 使用字符数组验证密码与哈希值
    boolean
    verify(String password, String hash)
    Verify a password against a hash using string 使用字符串验证密码与哈希值

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • argon2id

      public static Argon2Hash argon2id()
      Create Argon2id hasher with default parameters (recommended for most use cases) 使用默认参数创建 Argon2id 哈希器(推荐用于大多数用例)
      Returns:
      Argon2id password hasher
    • argon2i

      public static Argon2Hash argon2i()
      Create Argon2i hasher with default parameters (resistant to side-channel attacks) 使用默认参数创建 Argon2i 哈希器(抵抗旁道攻击)
      Returns:
      Argon2i password hasher
    • argon2d

      public static Argon2Hash argon2d()
      Create Argon2d hasher with default parameters (resistant to GPU attacks) 使用默认参数创建 Argon2d 哈希器(抵抗 GPU 攻击)
      Returns:
      Argon2d password hasher
    • builder

      public static Argon2Hash.Builder builder()
      Create a builder for custom Argon2 configuration 创建用于自定义 Argon2 配置的构建器
      Returns:
      builder instance
    • hash

      public String hash(char[] password)
      Description copied from interface: PasswordHash
      Hash a password from a character array 从字符数组哈希密码
      Specified by:
      hash in interface PasswordHash
      Parameters:
      password - the password to hash (will not be modified)
      Returns:
      the hash string (self-describing format)
    • hash

      public String hash(String password)
      Description copied from interface: PasswordHash
      Hash a password from a string 从字符串哈希密码
      Specified by:
      hash in interface PasswordHash
      Parameters:
      password - the password to hash
      Returns:
      the hash string (self-describing format)
    • verify

      public boolean verify(char[] password, String hash)
      Description copied from interface: PasswordHash
      Verify a password against a hash using character array 使用字符数组验证密码与哈希值
      Specified by:
      verify in interface PasswordHash
      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

      public boolean verify(String password, String hash)
      Description copied from interface: PasswordHash
      Verify a password against a hash using string 使用字符串验证密码与哈希值
      Specified by:
      verify in interface PasswordHash
      Parameters:
      password - the password to verify
      hash - the hash to verify against
      Returns:
      true if password matches the hash, false otherwise
    • needsRehash

      public boolean needsRehash(String hash)
      Description copied from interface: PasswordHash
      Check 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:
      needsRehash in interface PasswordHash
      Parameters:
      hash - the hash to check
      Returns:
      true if rehashing is needed, false otherwise
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: PasswordHash
      Get the algorithm name for this password hash implementation 获取此密码哈希实现的算法名称
      Specified by:
      getAlgorithm in interface PasswordHash
      Returns:
      algorithm name