InMemoryKeyManagementService
Native TrustWeave in-memory Key Management Service.
Constructor maps: passing a ConcurrentHashMap shares it with the service (the caller sees later changes). Any other MutableMap is copied once at construction; the caller's original is then a stale snapshot. BouncyCastle is used through a private provider instance and is not registered globally with java.security.Security.
This is a built-in, native TrustWeave implementation that uses Java's built-in cryptographic providers for key generation, storage, and signing. It's designed for development, testing, and scenarios where a full HSM or cloud KMS is not required or available.
Thread Safety: This implementation is fully thread-safe. All operations use thread-safe ConcurrentHashMap for key storage and are executed on Dispatchers.IO for I/O-bound cryptographic operations.
Key Storage: Keys are stored in memory only and will be lost when the service instance is destroyed. This makes it ideal for:
Development and testing
Single-process applications
Ephemeral key management scenarios
For production use, consider using a persistent KMS provider:
AWS KMS (FIPS 140-3 Level 3 validated)
Azure Key Vault
Google Cloud KMS
HashiCorp Vault
Hardware Security Modules (HSMs)
Algorithm Support:
Ed25519: Requires Java 15+ or BouncyCastle provider
secp256k1, P-256, P-384, P-521: Supported via Java's standard EC provider
Input Validation:
Key IDs are validated for length (max 256 characters) and non-blank
Signing data is validated for non-empty and size limits (max 10 MB to prevent DoS)
Duplicate key IDs are rejected during generation
Algorithm compatibility is checked before signing
Security Considerations:
Private keys are stored in memory and are accessible to the JVM process
Keys are not encrypted at rest (they're in memory)
Suitable for development/testing, not for production secrets
Consider using a hardware-backed KMS for production
Example:
// Create an in-memory KMS instance
val kms = InMemoryKeyManagementService()
// Generate a key
val result = kms.generateKey(Algorithm.Ed25519)
when (result) {
is GenerateKeyResult.Success -> {
val keyHandle = result.keyHandle
println("Key created: ${keyHandle.id}")
// Sign data
val signResult = kms.sign(keyHandle.id, "Hello, World!".toByteArray())
when (signResult) {
is SignResult.Success -> println("Signature: ${signResult.signature.toHexString()}")
is SignResult.Failure -> println("Error: ${signResult.reason}")
}
}
is GenerateKeyResult.Failure -> println("Error: ${result.reason}")
}Via Factory API:
import org.trustweave.kms.*
val kms = KeyManagementServices.create("inmemory")Constructors
Functions
Clears all keys from memory.
Gets the number of keys currently stored in memory.