Class OpenPasswordHash

java.lang.Object
cloud.opencode.base.crypto.OpenPasswordHash

public final class OpenPasswordHash extends Object
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 Details

    • argon2

      public static OpenPasswordHash argon2()
      Create Argon2id password hasher (recommended) 创建 Argon2id 密码哈希器(推荐)
      Returns:
      OpenPasswordHash instance
    • bcrypt

      public static OpenPasswordHash bcrypt()
      Create BCrypt password hasher 创建 BCrypt 密码哈希器
      Returns:
      OpenPasswordHash instance
    • bcrypt

      public static OpenPasswordHash bcrypt(int cost)
      Create BCrypt password hasher with custom cost 创建自定义代价的 BCrypt 密码哈希器
      Parameters:
      cost - cost factor (4-31)
      Returns:
      OpenPasswordHash instance
    • scrypt

      public static OpenPasswordHash scrypt()
      Create SCrypt password hasher 创建 SCrypt 密码哈希器
      Returns:
      OpenPasswordHash instance
    • pbkdf2

      public static OpenPasswordHash pbkdf2()
      Create PBKDF2 password hasher 创建 PBKDF2 密码哈希器
      Returns:
      OpenPasswordHash instance
    • pbkdf2

      public static OpenPasswordHash pbkdf2(int iterations)
      Create PBKDF2 password hasher with custom iterations 创建自定义迭代次数的 PBKDF2 密码哈希器
      Parameters:
      iterations - number of iterations
      Returns:
      OpenPasswordHash instance
    • of

      public static OpenPasswordHash of(PasswordHashAlgorithm algorithm)
      Create password hasher by algorithm enum 根据算法枚举创建密码哈希器
      Parameters:
      algorithm - password hash algorithm
      Returns:
      OpenPasswordHash instance
    • hash

      public String hash(String password)
      Hash password 哈希密码
      Parameters:
      password - password to hash
      Returns:
      hash string (includes algorithm info and salt)
    • hash

      public String hash(char[] password)
      Hash password from char array (more secure) 从字符数组哈希密码(更安全)
      Parameters:
      password - password to hash
      Returns:
      hash string (includes algorithm info and salt)
    • verify

      public boolean verify(String password, String hash)
      Verify password against hash 验证密码与哈希
      Parameters:
      password - password to verify
      hash - stored hash
      Returns:
      true if password matches
    • verify

      public boolean verify(char[] password, String hash)
      Verify password from char array against hash 验证字符数组密码与哈希
      Parameters:
      password - password to verify
      hash - stored hash
      Returns:
      true if password matches
    • needsRehash

      public boolean needsRehash(String hash)
      Check if hash needs to be upgraded 检查哈希是否需要升级
      Parameters:
      hash - hash to check
      Returns:
      true if hash needs to be rehashed with current settings
    • defaultPolicy

      public static PasswordPolicy defaultPolicy()
      Get default password policy 获取默认密码策略
      Returns:
      default password policy
    • strongPolicy

      public static PasswordPolicy strongPolicy()
      Get strong password policy 获取强密码策略
      Returns:
      strong password policy
    • getAlgorithm

      public String getAlgorithm()
      Get algorithm name 获取算法名称
      Returns:
      algorithm name