Class SecretKeyUtil

java.lang.Object
cloud.opencode.base.crypto.key.SecretKeyUtil

public final class SecretKeyUtil extends Object
Secret key utility class - Utility methods for symmetric key operations 对称密钥工具类 - 对称密钥操作的实用方法

Features | 主要功能:

  • Secret key creation and conversion - 密钥创建和转换

Usage Examples | 使用示例:

SecretKey key = SecretKeyUtil.fromBytes(keyBytes, "AES");

Security | 安全性:

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

Performance | 性能特性:

  • Time complexity: O(1) - 时间复杂度: O(1)
  • Space complexity: O(k) - 空间复杂度: O(k),k为密钥大小
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static SecretKey
    derive(byte[] masterKey, byte[] salt, String algorithm, int keySize)
    Derive secret key from master key using PBKDF2 使用 PBKDF2 从主密钥派生对称密钥
    static SecretKey
    deriveFromPassword(char[] password, byte[] salt, String algorithm, int keySize)
    Derive secret key from password using PBKDF2 使用 PBKDF2 从密码派生对称密钥
    static boolean
    equals(SecretKey key1, SecretKey key2)
    Check if two secret keys are equal 检查两个对称密钥是否相等
    static SecretKey
    fromBytes(byte[] keyBytes, String algorithm)
    Create secret key from byte array 从字节数组创建对称密钥
    static SecretKey
    generate(String algorithm, int keySize)
    Generate secret key with specified algorithm and key size 生成指定算法和密钥大小的对称密钥
    static byte[]
    Convert secret key to byte array 将对称密钥转换为字节数组

    Methods inherited from class Object

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

    • generate

      public static SecretKey generate(String algorithm, int keySize)
      Generate secret key with specified algorithm and key size 生成指定算法和密钥大小的对称密钥
      Parameters:
      algorithm - key algorithm (e.g., "AES", "ChaCha20")
      keySize - key size in bits
      Returns:
      generated secret key
      Throws:
      OpenKeyException - if key generation fails
    • fromBytes

      public static SecretKey fromBytes(byte[] keyBytes, String algorithm)
      Create secret key from byte array 从字节数组创建对称密钥
      Parameters:
      keyBytes - key bytes
      algorithm - key algorithm (e.g., "AES", "ChaCha20")
      Returns:
      secret key
      Throws:
      OpenKeyException - if key creation fails
    • toBytes

      public static byte[] toBytes(SecretKey key)
      Convert secret key to byte array 将对称密钥转换为字节数组
      Parameters:
      key - secret key
      Returns:
      key bytes
      Throws:
      OpenKeyException - if key is null or conversion fails
    • derive

      public static SecretKey derive(byte[] masterKey, byte[] salt, String algorithm, int keySize)
      Derive secret key from master key using PBKDF2 使用 PBKDF2 从主密钥派生对称密钥
      Parameters:
      masterKey - master key bytes (e.g., password)
      salt - salt for key derivation
      algorithm - target algorithm (e.g., "AES")
      keySize - target key size in bits
      Returns:
      derived secret key
      Throws:
      OpenKeyException - if key derivation fails
    • deriveFromPassword

      public static SecretKey deriveFromPassword(char[] password, byte[] salt, String algorithm, int keySize)
      Derive secret key from password using PBKDF2 使用 PBKDF2 从密码派生对称密钥
      Parameters:
      password - password characters
      salt - salt for key derivation
      algorithm - target algorithm (e.g., "AES")
      keySize - target key size in bits
      Returns:
      derived secret key
      Throws:
      OpenKeyException - if key derivation fails
    • equals

      public static boolean equals(SecretKey key1, SecretKey key2)
      Check if two secret keys are equal 检查两个对称密钥是否相等
      Parameters:
      key1 - first secret key
      key2 - second secret key
      Returns:
      true if keys are equal
      Throws:
      OpenKeyException - if either key is null