Class SecretKeyUtil
java.lang.Object
cloud.opencode.base.crypto.key.SecretKeyUtil
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 TypeMethodDescriptionstatic SecretKeyDerive secret key from master key using PBKDF2 使用 PBKDF2 从主密钥派生对称密钥static SecretKeyderiveFromPassword(char[] password, byte[] salt, String algorithm, int keySize) Derive secret key from password using PBKDF2 使用 PBKDF2 从密码派生对称密钥static booleanCheck if two secret keys are equal 检查两个对称密钥是否相等static SecretKeyCreate secret key from byte array 从字节数组创建对称密钥static SecretKeyGenerate secret key with specified algorithm and key size 生成指定算法和密钥大小的对称密钥static byte[]Convert secret key to byte array 将对称密钥转换为字节数组
-
Method Details
-
generate
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
Create secret key from byte array 从字节数组创建对称密钥- Parameters:
keyBytes- key bytesalgorithm- key algorithm (e.g., "AES", "ChaCha20")- Returns:
- secret key
- Throws:
OpenKeyException- if key creation fails
-
toBytes
Convert secret key to byte array 将对称密钥转换为字节数组- Parameters:
key- secret key- Returns:
- key bytes
- Throws:
OpenKeyException- if key is null or conversion fails
-
derive
Derive secret key from master key using PBKDF2 使用 PBKDF2 从主密钥派生对称密钥- Parameters:
masterKey- master key bytes (e.g., password)salt- salt for key derivationalgorithm- 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 characterssalt- salt for key derivationalgorithm- target algorithm (e.g., "AES")keySize- target key size in bits- Returns:
- derived secret key
- Throws:
OpenKeyException- if key derivation fails
-
equals
Check if two secret keys are equal 检查两个对称密钥是否相等- Parameters:
key1- first secret keykey2- second secret key- Returns:
- true if keys are equal
- Throws:
OpenKeyException- if either key is null
-