Class SCryptHash

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

public final class SCryptHash extends Object implements PasswordHash
SCrypt password hashing implementation - Memory-hard password hashing using scrypt algorithm (requires Bouncy Castle) SCrypt 密码哈希实现 - 使用 scrypt 算法的内存困难密码哈希(需要 Bouncy Castle)

Features | 主要功能:

  • SCrypt password hashing - SCrypt 密码哈希
  • Memory-hard key derivation - 内存密集型密钥派生

Usage Examples | 使用示例:

SCryptHash scrypt = SCryptHash.of();
String hash = scrypt.hash("password");
boolean valid = scrypt.verify("password", hash);

Security | 安全性:

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

Performance | 性能特性:

  • Time complexity: O(N * r * p) - 时间复杂度: O(N * r * p),N/r/p为参数
  • Space complexity: O(N * r) - 空间复杂度: O(N * r)
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 SCryptHash instances 用于创建自定义 SCryptHash 实例的构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    Create a builder for custom SCrypt configuration 创建用于自定义 SCrypt 配置的构建器
    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 SCryptHash
    of()
    Create SCrypt hasher with default parameters 使用默认参数创建 SCrypt 哈希器
    static SCryptHash
    Create SCrypt hasher with OWASP recommended parameters 使用 OWASP 推荐参数创建 SCrypt 哈希器
    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

    • of

      public static SCryptHash of()
      Create SCrypt hasher with default parameters 使用默认参数创建 SCrypt 哈希器
      Returns:
      new SCryptHash instance
      Throws:
      OpenCryptoException - if Bouncy Castle is not available
    • owaspRecommended

      public static SCryptHash owaspRecommended()
      Create SCrypt hasher with OWASP recommended parameters 使用 OWASP 推荐参数创建 SCrypt 哈希器
      Returns:
      new SCryptHash instance
      Throws:
      OpenCryptoException - if Bouncy Castle is not available
    • builder

      public static SCryptHash.Builder builder()
      Create a builder for custom SCrypt configuration 创建用于自定义 SCrypt 配置的构建器
      Returns:
      new Builder instance
      Throws:
      OpenCryptoException - if Bouncy Castle is not available
    • 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