Class Scrypt

java.lang.Object
cloud.opencode.base.crypto.kdf.Scrypt
All Implemented Interfaces:
KdfEngine

public final class Scrypt extends Object implements KdfEngine
Scrypt key derivation function implementation - Memory-hard password-based KDF Scrypt 密钥派生函数实现 - 内存困难的基于密码的 KDF

Features | 主要功能:

  • SCrypt key derivation - SCrypt 密钥派生
  • Memory-hard function - 内存密集型函数

Usage Examples | 使用示例:

Scrypt scrypt = Scrypt.create();
byte[] key = scrypt.deriveKey(password, salt, 32);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
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 Scrypt configuration Scrypt 配置构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a builder for Scrypt configuration 创建 Scrypt 配置构建器
    byte[]
    derive(byte[] inputKeyMaterial, byte[] salt, byte[] info, int length)
    Derives a key from input key material with salt and info parameters 使用盐值和信息参数从输入密钥材料派生密钥
    byte[]
    derive(byte[] inputKeyMaterial, int length)
    Derives a key from input key material with default parameters 使用默认参数从输入密钥材料派生密钥
    byte[]
    deriveKey(char[] password, byte[] salt, int keyLength)
    Derives a key from a password and salt using Scrypt 使用 Scrypt 从密码和盐值派生密钥
    Returns the algorithm name of this KDF 返回此 KDF 的算法名称
    int
    Gets the CPU/memory cost parameter 获取 CPU/内存成本参数
    int
    Gets the parallelization parameter 获取并行化参数
    int
    Gets the block size parameter 获取块大小参数
    static Scrypt
    of()
    Creates Scrypt instance with default parameters (N=32768, r=8, p=1) 创建使用默认参数的 Scrypt 实例(N=32768,r=8,p=1)
    static Scrypt
    of(int n, int r, int p)
    Creates Scrypt instance with custom parameters 创建使用自定义参数的 Scrypt 实例

    Methods inherited from class Object

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

    • of

      public static Scrypt of()
      Creates Scrypt instance with default parameters (N=32768, r=8, p=1) 创建使用默认参数的 Scrypt 实例(N=32768,r=8,p=1)
      Returns:
      new Scrypt instance
    • of

      public static Scrypt of(int n, int r, int p)
      Creates Scrypt instance with custom parameters 创建使用自定义参数的 Scrypt 实例
      Parameters:
      n - CPU/memory cost parameter (must be power of 2, greater than 1)
      r - block size parameter (must be positive)
      p - parallelization parameter (must be positive)
      Returns:
      new Scrypt instance
      Throws:
      IllegalArgumentException - if parameters are invalid
    • builder

      public static Scrypt.Builder builder()
      Creates a builder for Scrypt configuration 创建 Scrypt 配置构建器
      Returns:
      new Builder instance
    • deriveKey

      public byte[] deriveKey(char[] password, byte[] salt, int keyLength)
      Derives a key from a password and salt using Scrypt 使用 Scrypt 从密码和盐值派生密钥
      Parameters:
      password - the password as char array (will not be modified)
      salt - the salt value
      keyLength - the desired key length in bytes
      Returns:
      the derived key
      Throws:
      NullPointerException - if password or salt is null
      IllegalArgumentException - if keyLength is not positive
      OpenCryptoException - if Bouncy Castle is not available or derivation fails
    • derive

      public byte[] derive(byte[] inputKeyMaterial, byte[] salt, byte[] info, int length)
      Description copied from interface: KdfEngine
      Derives a key from input key material with salt and info parameters 使用盐值和信息参数从输入密钥材料派生密钥
      Specified by:
      derive in interface KdfEngine
      Parameters:
      inputKeyMaterial - the input key material (IKM)
      salt - the salt value (can be null or empty for some algorithms)
      info - the context and application specific information (can be null)
      length - the desired output key length in bytes
      Returns:
      the derived key
    • derive

      public byte[] derive(byte[] inputKeyMaterial, int length)
      Description copied from interface: KdfEngine
      Derives a key from input key material with default parameters 使用默认参数从输入密钥材料派生密钥
      Specified by:
      derive in interface KdfEngine
      Parameters:
      inputKeyMaterial - the input key material (IKM)
      length - the desired output key length in bytes
      Returns:
      the derived key
    • getAlgorithm

      public String getAlgorithm()
      Description copied from interface: KdfEngine
      Returns the algorithm name of this KDF 返回此 KDF 的算法名称
      Specified by:
      getAlgorithm in interface KdfEngine
      Returns:
      the algorithm name
    • getN

      public int getN()
      Gets the CPU/memory cost parameter 获取 CPU/内存成本参数
      Returns:
      the N parameter
    • getR

      public int getR()
      Gets the block size parameter 获取块大小参数
      Returns:
      the r parameter
    • getP

      public int getP()
      Gets the parallelization parameter 获取并行化参数
      Returns:
      the p parameter