Class X25519Engine
java.lang.Object
cloud.opencode.base.crypto.keyexchange.X25519Engine
- All Implemented Interfaces:
KeyExchangeEngine
X25519 key exchange engine (Curve25519 ECDH) - Recommended for most applications
X25519 密钥协商引擎 - 推荐用于大多数应用场景
Features | 主要功能:
- X25519 Diffie-Hellman key exchange - X25519 Diffie-Hellman 密钥交换
- 256-bit security level - 256 位安全级别
Usage Examples | 使用示例:
X25519Engine engine = X25519Engine.create();
KeyPair kp = engine.generateKeyPair();
byte[] shared = engine.computeSharedSecret(kp.getPrivate(), peerPubKey);
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]agree(PrivateKey myPrivateKey, PublicKey theirPublicKey) Performs X25519 key agreement between two parties (static method) 执行两方之间的 X25519 密钥协商(静态方法)static byte[]agreeAndDerive(PrivateKey myPrivateKey, PublicKey theirPublicKey, byte[] info, int keyLength) Performs X25519 key agreement and derives key material using HKDF (static method) 执行 X25519 密钥协商并使用 HKDF 派生密钥材料(静态方法)byte[]Computes the shared secret using the configured keys 使用配置的密钥计算共享密钥static X25519Enginecreate()Creates a new X25519 engine instance 创建新的 X25519 引擎实例byte[]deriveKey(byte[] info, int length) Derives key material from the shared secret using HKDF 使用 HKDF 从共享密钥派生密钥材料Generates a new key pair for the key exchange 为密钥交换生成新的密钥对Gets the algorithm name of this key exchange engine 获取密钥交换引擎的算法名称Gets the private key if set 获取已设置的私钥Gets the public key from the private key (if set).setPrivateKey(PrivateKey privateKey) Sets the private key for this party 设置本方的私钥setRemotePublicKey(PublicKey publicKey) Sets the remote party's public key 设置对方的公钥static X25519EngineCreates a new X25519 engine instance with a generated key pair 创建新的 X25519 引擎实例并生成密钥对
-
Method Details
-
create
Creates a new X25519 engine instance 创建新的 X25519 引擎实例- Returns:
- new X25519 engine instance
-
withGeneratedKeyPair
Creates a new X25519 engine instance with a generated key pair 创建新的 X25519 引擎实例并生成密钥对- Returns:
- new X25519 engine instance with generated key pair
-
agree
Performs X25519 key agreement between two parties (static method) 执行两方之间的 X25519 密钥协商(静态方法)- Parameters:
myPrivateKey- the local private keytheirPublicKey- the remote public key- Returns:
- the shared secret (32 bytes)
- Throws:
NullPointerException- if any key is null
-
agreeAndDerive
public static byte[] agreeAndDerive(PrivateKey myPrivateKey, PublicKey theirPublicKey, byte[] info, int keyLength) Performs X25519 key agreement and derives key material using HKDF (static method) 执行 X25519 密钥协商并使用 HKDF 派生密钥材料(静态方法)- Parameters:
myPrivateKey- the local private keytheirPublicKey- the remote public keyinfo- optional context information for key derivationkeyLength- desired key length in bytes- Returns:
- derived key material
- Throws:
NullPointerException- if any required parameter is nullIllegalArgumentException- if keyLength is invalid
-
generateKeyPair
Description copied from interface:KeyExchangeEngineGenerates a new key pair for the key exchange 为密钥交换生成新的密钥对- Specified by:
generateKeyPairin interfaceKeyExchangeEngine- Returns:
- a new key pair
-
setPrivateKey
Description copied from interface:KeyExchangeEngineSets the private key for this party 设置本方的私钥- Specified by:
setPrivateKeyin interfaceKeyExchangeEngine- Parameters:
privateKey- the private key to use- Returns:
- this engine for method chaining
-
setRemotePublicKey
Description copied from interface:KeyExchangeEngineSets the remote party's public key 设置对方的公钥- Specified by:
setRemotePublicKeyin interfaceKeyExchangeEngine- Parameters:
publicKey- the remote public key- Returns:
- this engine for method chaining
-
deriveKey
public byte[] deriveKey(byte[] info, int length) Description copied from interface:KeyExchangeEngineDerives key material from the shared secret using HKDF 使用 HKDF 从共享密钥派生密钥材料- Specified by:
deriveKeyin interfaceKeyExchangeEngine- Parameters:
info- optional context and application specific information (can be null)length- desired key length in bytes- Returns:
- derived key material
-
getAlgorithm
Description copied from interface:KeyExchangeEngineGets the algorithm name of this key exchange engine 获取密钥交换引擎的算法名称- Specified by:
getAlgorithmin interfaceKeyExchangeEngine- Returns:
- the algorithm name
-
getPrivateKey
Gets the private key if set 获取已设置的私钥- Returns:
- the private key, or null if not set
-
getPublicKey
Gets the public key from the private key (if set). 从私钥获取公钥(如果已设置)。X25519 does not support deriving a public key from an existing private key through the standard JCA API. Use
generateKeyPair()to obtain both the public and private keys together.- Returns:
- never returns normally
- Throws:
UnsupportedOperationException- always; usegenerateKeyPair()to obtain keys
-