Class InMemoryKeyManager
-
- All Implemented Interfaces:
-
web5.sdk.crypto.KeyExporter,web5.sdk.crypto.KeyImporter,web5.sdk.crypto.KeyManager
public final class InMemoryKeyManager implements KeyManager, KeyExporter, KeyImporter
A class for managing cryptographic keys in-memory.
InMemoryKeyManageris an implementation of KeyManager that stores keys in-memory using a mutable map. It provides methods to:Generate private keys (generatePrivateKey)
Retrieve public keys (getPublicKey)
Sign payloads (sign)
val keyManager = InMemoryKeyManager() val keyID = keyManager.generatePrivateKey(JWSAlgorithm.EdDSA, Curve.Ed25519) val publicKey = keyManager.getPublicKey(keyID)Keys are stored in an in-memory mutable map and will be lost once the application is terminated or the object is garbage-collected.
It is suitable for testing or scenarios where persistent storage of keys is not necessary.
-
-
Constructor Summary
Constructors Constructor Description InMemoryKeyManager()
-
Method Summary
Modifier and Type Method Description StringgeneratePrivateKey(AlgorithmId algorithmId, KeyGenOptions options)Generates a private key using specified algorithmId, and stores it in the in-memory keyStore. JwkgetPublicKey(String keyAlias)Computes and returns a public key corresponding to the private key identified by the provided keyAlias. ByteArraysign(String keyAlias, ByteArray signingInput)Signs a payload using the private key identified by the provided keyAlias. StringgetDeterministicAlias(Jwk publicKey)Return the alias of publicKey, as was originally returned by generatePrivateKey. JwkexportKey(String keyId)ExportKey exports the key specific by the key ID from the KeyManager. StringimportKey(Jwk jwk)ImportKey imports the key into the KeyManager and returns the key alias. -
-
Method Detail
-
generatePrivateKey
String generatePrivateKey(AlgorithmId algorithmId, KeyGenOptions options)
Generates a private key using specified algorithmId, and stores it in the in-memory keyStore.
- Parameters:
algorithmId- The algorithmId AlgorithmId.options- Options for key generation, may include specific parameters relevant to the algorithm.- Returns:
The key ID of the generated private key.
-
getPublicKey
Jwk getPublicKey(String keyAlias)
Computes and returns a public key corresponding to the private key identified by the provided keyAlias.
- Parameters:
keyAlias- The alias (key ID) of the private key stored in the keyStore.- Returns:
The computed public key as a Jwk object.
-
sign
ByteArray sign(String keyAlias, ByteArray signingInput)
Signs a payload using the private key identified by the provided keyAlias.
The implementation of this method is not yet provided and invoking it will throw a NotImplementedError.
- Parameters:
keyAlias- The alias (key ID) of the private key stored in the keyStore.signingInput- The data to be signed.- Returns:
The signature in JWS R+S format
-
getDeterministicAlias
String getDeterministicAlias(Jwk publicKey)
Return the alias of publicKey, as was originally returned by generatePrivateKey.
- Parameters:
publicKey- A public key in Jwk (JSON Web Key) format- Returns:
The alias belonging to publicKey
-
exportKey
Jwk exportKey(String keyId)
ExportKey exports the key specific by the key ID from the KeyManager.
- Parameters:
keyId- the keyId whose corresponding key to export- Returns:
the Jwk representation of the key
-
-
-
-