KeyGenerator

interface KeyGenerator

The KeyGenerator interface provides a blueprint for implementing cryptographic key generation and conversion functionalities for various cryptographic algorithms and key types.

Cryptographic keys play a pivotal role in securing communication and data. This interface standardizes the key management operations including key generation, format conversion, and key derivation, ensuring that implementers adhere to a consistent API. In the realm of cryptographic applications, KeyGenerator enables developers to instantiate key pairs, convert keys between various formats, and derive public keys from private ones, while abstracting the specifics of the cryptographic algorithm and key type in use.

Implementers are expected to provide concrete implementations for various key management activities for specified algorithms (e.g., RSA, EC) and key types (symmetric/asymmetric), and to handle potential exceptions, especially in scenarios such as deriving public keys in symmetric key contexts.

Example Usage:

val keyGenerator: KeyGenerator = ...
val privateKey: Jwk = keyGenerator.generatePrivateKey()
val publicKey: Jwk = keyGenerator.getPublicKey(privateKey)
val privateKeyBytes: ByteArray = keyGenerator.privateKeyToBytes(privateKey)
val restoredPrivateKey: Jwk = keyGenerator.bytesToPrivateKey(privateKeyBytes)

Note:

For symmetric key generators, certain methods like getPublicKey or publicKeyToBytes may not be applicable and should throw an UnsupportedOperationException.

Inheritors

Properties

Link copied to clipboard
abstract val algorithm: Jwa

Indicates the algorithm intended to be used with the key.

Link copied to clipboard
abstract val curve: JwaCurve

The curve used for the key generation.

Link copied to clipboard
abstract val keyType: String

KeyType in String format (OKP, EC, etc.).

Functions

Link copied to clipboard
abstract fun bytesToPrivateKey(privateKeyBytes: ByteArray): Jwk

Converts a private key as bytes into a Jwk.

Link copied to clipboard
abstract fun bytesToPublicKey(publicKeyBytes: ByteArray): Jwk

Converts a public key as bytes into a Jwk. Applicable for asymmetric Key Generators only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception

Link copied to clipboard
abstract fun computePublicKey(privateKey: Jwk): Jwk

Derives a public key from the private key provided. Applicable for asymmetric Key Generators only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception

Link copied to clipboard
abstract fun generatePrivateKey(options: KeyGenOptions? = null): Jwk

Generates a private key.

Link copied to clipboard
abstract fun privateKeyToBytes(privateKey: Jwk): ByteArray

Converts a private key to bytes.

Link copied to clipboard
abstract fun publicKeyToBytes(publicKey: Jwk): ByteArray

Converts a public key to bytes. Applicable for asymmetric KeyGenerator implementations only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception