Class Pbkdf2Hash

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

public final class Pbkdf2Hash extends Object implements PasswordHash
PBKDF2 password hashing implementation - Password-Based Key Derivation Function 2 using JDK standard library PBKDF2 密码哈希实现 - 使用 JDK 标准库的基于密码的密钥派生函数 2

Features | 主要功能:

  • PBKDF2 with SHA-256/SHA-512 - PBKDF2(SHA-256/SHA-512)
  • Configurable iteration count - 可配置迭代次数

Usage Examples | 使用示例:

Pbkdf2Hash pbkdf2 = Pbkdf2Hash.sha256();
String hash = pbkdf2.hash("password");
boolean valid = pbkdf2.verify("password", hash);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(iterations) - 时间复杂度: O(iterations)(迭代次数)
  • 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 Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for creating custom Pbkdf2Hash instances 用于创建自定义 Pbkdf2Hash 实例的构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    Create a builder for custom PBKDF2 configuration 创建用于自定义 PBKDF2 配置的构建器
    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 检查哈希值是否需要使用当前参数重新哈希
    static Pbkdf2Hash
    Create PBKDF2 hasher with OWASP recommended parameters (SHA-256, 600k iterations) 使用 OWASP 推荐参数创建 PBKDF2 哈希器(SHA-256,60 万次迭代)
    static Pbkdf2Hash
    Create PBKDF2 hasher with SHA-256 and OWASP recommended parameters 使用 SHA-256 和 OWASP 推荐参数创建 PBKDF2 哈希器
    static Pbkdf2Hash
    Create PBKDF2 hasher with SHA-512 使用 SHA-512 创建 PBKDF2 哈希器
    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

    • sha256

      public static Pbkdf2Hash sha256()
      Create PBKDF2 hasher with SHA-256 and OWASP recommended parameters 使用 SHA-256 和 OWASP 推荐参数创建 PBKDF2 哈希器
      Returns:
      new Pbkdf2Hash instance
    • sha512

      public static Pbkdf2Hash sha512()
      Create PBKDF2 hasher with SHA-512 使用 SHA-512 创建 PBKDF2 哈希器
      Returns:
      new Pbkdf2Hash instance
    • owaspRecommended

      public static Pbkdf2Hash owaspRecommended()
      Create PBKDF2 hasher with OWASP recommended parameters (SHA-256, 600k iterations) 使用 OWASP 推荐参数创建 PBKDF2 哈希器(SHA-256,60 万次迭代)
      Returns:
      new Pbkdf2Hash instance
    • builder

      public static Pbkdf2Hash.Builder builder()
      Create a builder for custom PBKDF2 configuration 创建用于自定义 PBKDF2 配置的构建器
      Returns:
      new 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