Crypto

object Crypto

Cryptography utility object providing key generation, signature creation, and other crypto-related functionalities.

The Crypto object operates based on provided algorithms and curve types, facilitating a generic approach to handling multiple cryptographic algorithms and their respective key types. It offers convenience methods to:

Internally, it utilizes predefined mappings to pair algorithms and curve types with their respective KeyGenerator and Signer implementations, ensuring appropriate handlers are utilized for different cryptographic approaches. It also includes mappings to manage multicodec functionality, providing a mapping between byte arrays and respective key generators.

Example Usage:

val privateKey: Jwk = Crypto.generatePrivateKey(JWSAlgorithm.EdDSA, Curve.Ed25519)

Key Points:

  • Manages key generation and signing operations via predefined mappings to handle different crypto approaches.

  • Provides mechanisms to perform actions (e.g., signing, key generation) dynamically based on algorithmId AlgorithmId.

See also

for key generation functionalities.

for signing functionalities.

Functions

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

Computes a public key from the given private key, utilizing relevant KeyGenerator.

Link copied to clipboard
fun generatePrivateKey(algorithmId: AlgorithmId, options: KeyGenOptions? = null): Jwk

Generates a private key using the specified algorithmId, utilizing the appropriate KeyGenerator.

Link copied to clipboard

Retrieves the multicodec identifier associated with a given algorithmId.

Link copied to clipboard

Extracts the cryptographic curve information from a Jwk object.

Link copied to clipboard
fun getKeyGenerator(multiCodec: Int): KeyGenerator

Retrieves a KeyGenerator based on the provided multicodec identifier.

Retrieves a KeyGenerator based on the provided algorithmId. Currently, we provide key generators for keys that use ECC (see AlgorithmId enum)

Link copied to clipboard
fun getSigner(algorithmId: AlgorithmId): Signer

Retrieves a Signer based on the provided algorithmId.

Link copied to clipboard
fun getVerifier(curve: JwaCurve? = null): Signer

Retrieves a Signer to be used for verification based on the provided algorithm and curve.

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

Converts a Jwk public key into its byte array representation.

Link copied to clipboard
fun sign(privateKey: Jwk, payload: ByteArray, options: SignOptions? = null): ByteArray

Signs a payload using a private key.

Link copied to clipboard
fun verify(publicKey: Jwk, signedPayload: ByteArray, signature: ByteArray)

Verifies a signature against a signed payload using a public key.