Class KeyRotation<K>
java.lang.Object
cloud.opencode.base.crypto.rotation.KeyRotation<K>
- Type Parameters:
K- the key type - 密钥类型
- All Implemented Interfaces:
AutoCloseable
Key Rotation Manager - Automated key version management and rotation
密钥轮换管理器 - 自动化密钥版本管理和轮换
Provides automatic key rotation with version management, graceful retirement of old keys, and support for both symmetric and asymmetric keys.
提供自动密钥轮换,包括版本管理、旧密钥优雅退役,以及对称和非对称密钥的支持。
Features | 主要功能:
- Version-based key management - 基于版本的密钥管理
- Automatic rotation scheduling - 自动轮换调度
- Graceful key retirement - 密钥优雅退役
- Key derivation support - 密钥派生支持
- Audit logging - 审计日志
- Thread-safe operations - 线程安全操作
Usage Examples | 使用示例:
// Create a key rotation manager
KeyRotation<SecretKey> rotation = KeyRotation.<SecretKey>builder()
.keyId("encryption-key")
.keyGenerator(() -> KeyGenerator.generateAesKey(256))
.rotationInterval(Duration.ofDays(90))
.gracePeriod(Duration.ofDays(7))
.maxVersions(3)
.onRotation(event -> log.info("Key rotated: {}", event))
.build();
// Start automatic rotation
rotation.startAutoRotation();
// Get current key for encryption
VersionedKey<SecretKey> current = rotation.getCurrentKey();
// Decrypt with specific version
SecretKey key = rotation.getKeyByVersion(1).key();
// Manual rotation
rotation.rotate();
// Retire old keys
rotation.retireOldVersions();
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 ClassesModifier and TypeClassDescriptionstatic final classBuilder for KeyRotation.static enumStatus of a key version.static final recordEvent triggered during key rotation.static final recordA versioned key with metadata. -
Method Summary
Modifier and TypeMethodDescriptionAdds an existing key as a new version.static <K> KeyRotation.Builder<K> builder()Creates a new builder.voidclose()intRemoves retired keys permanently.static KeyRotation<SecretKey> Creates a key rotation for AES keys.static KeyRotation<KeyPair> Creates a key rotation for RSA key pairs.Gets all available versions.Gets the current (latest) key for encryption.longGets the current version number.getKeyByVersion(long version) Gets a key by its version number.getKeyId()Gets the key ID.Gets the time until next rotation.voidImports a key with a specific version.booleanChecks if a rotation is needed based on the interval.intRetires keys older than the grace period.rotate()Manually triggers a key rotation.voidStarts automatic key rotation.voidStops automatic key rotation.
-
Method Details
-
getCurrentKey
Gets the current (latest) key for encryption. 获取当前(最新)密钥用于加密。- Returns:
- the current versioned key - 当前版本化密钥
-
getKeyByVersion
Gets a key by its version number. 根据版本号获取密钥。- Parameters:
version- the version number - 版本号- Returns:
- the versioned key - 版本化密钥
- Throws:
OpenCryptoException- if version not found - 如果版本未找到
-
getCurrentVersion
public long getCurrentVersion()Gets the current version number. 获取当前版本号。- Returns:
- the current version - 当前版本
-
getAvailableVersions
-
getKeyId
-
rotate
Manually triggers a key rotation. 手动触发密钥轮换。- Returns:
- the new versioned key - 新的版本化密钥
-
addKey
Adds an existing key as a new version. 将现有密钥添加为新版本。- Parameters:
key- the key to add - 要添加的密钥- Returns:
- the versioned key - 版本化密钥
-
importKey
-
retireOldVersions
public int retireOldVersions()Retires keys older than the grace period. 退役超过宽限期的密钥。- Returns:
- count of retired keys - 退役的密钥数量
-
deleteRetiredKeys
public int deleteRetiredKeys()Removes retired keys permanently. 永久删除已退役的密钥。- Returns:
- count of deleted keys - 删除的密钥数量
-
startAutoRotation
public void startAutoRotation()Starts automatic key rotation. 启动自动密钥轮换。 -
stopAutoRotation
public void stopAutoRotation()Stops automatic key rotation. 停止自动密钥轮换。 -
isRotationNeeded
public boolean isRotationNeeded()Checks if a rotation is needed based on the interval. 根据间隔检查是否需要轮换。- Returns:
- true if rotation is needed - 如果需要轮换返回 true
-
getTimeUntilNextRotation
Gets the time until next rotation. 获取到下次轮换的时间。- Returns:
- duration until next rotation - 到下次轮换的时间
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
builder
Creates a new builder. 创建新的构建器。- Type Parameters:
K- the key type - 密钥类型- Returns:
- the builder - 构建器
-
forAes
Creates a key rotation for AES keys. 为 AES 密钥创建密钥轮换。- Parameters:
keyId- the key ID - 密钥 IDkeyBits- the key size in bits (128, 192, or 256) - 密钥位数- Returns:
- the key rotation manager - 密钥轮换管理器
-
forRsa
Creates a key rotation for RSA key pairs. 为 RSA 密钥对创建密钥轮换。- Parameters:
keyId- the key ID - 密钥 IDkeyBits- the key size in bits - 密钥位数- Returns:
- the key rotation manager - 密钥轮换管理器
-