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.

    InMemoryKeyManager is an implementation of KeyManager that stores keys in-memory using a mutable map. It provides methods to:

    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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      String generatePrivateKey(AlgorithmId algorithmId, KeyGenOptions options) Generates a private key using specified algorithmId, and stores it in the in-memory keyStore.
      Jwk getPublicKey(String keyAlias) Computes and returns a public key corresponding to the private key identified by the provided keyAlias.
      ByteArray sign(String keyAlias, ByteArray signingInput) Signs a payload using the private key identified by the provided keyAlias.
      String getDeterministicAlias(Jwk publicKey) Return the alias of publicKey, as was originally returned by generatePrivateKey.
      Jwk exportKey(String keyId) ExportKey exports the key specific by the key ID from the KeyManager.
      String importKey(Jwk jwk) ImportKey imports the key into the KeyManager and returns the key alias.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • InMemoryKeyManager

        InMemoryKeyManager()
    • 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

      • 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

      • importKey

         String importKey(Jwk jwk)

        ImportKey imports the key into the KeyManager and returns the key alias.

        Parameters:
        jwk - the Jwk representation of the key to import
        Returns:

        the key alias of the imported key