Note: Running this API in the simulator is not supported.
Get started
Setup
- Add the following library dependency to the
dependenciessection of your package.json file.
Usage
In the source file in which you are using the Vega Crypto functionality, import the data types that you need from the Turbo Module.Crypto class is always the first step to make use of the
KeplerCrypto Turbo Module.
ECDSA signature and verification
The following example code includes ECDSA keypair generation, message signing, and verification.Step 1: Create an ECDSA key builder
Step 2: Generate a keypair
You can generate an ECDSA keypair or construct a private key. To generate an ECDSA keypair, callbuildGenerated().
buildPrivateFromDer().
Step 3: Create a signature context builder
Step 4: Sign the message and obtain the signature
Step 5: Verify the message with the signature
RSA signature and verification
The following example code includes RSA keypair generation, message signing, and verification.Step 1: Create an RSA key builder
Step 2: Generate a keypair
You can generate an RSA keypair or construct a private key. To generate an RSA keypair, callbuildGenerated().
buildPrivateFromDer().
Step 3: Create a signature context builder and sign the message
Create a signature context builder by callingbuildSigningContext() and then sign the message with the RSA-PKCS1 v1.5 scheme to get the signature.
Step 4: Verify the message
Use the signature to verify the message by callingrsaPkcs1Verifier.verify()
which uses the RSA-PKCS1 v1.5 standard.
Step 5: Sign the message and get the signature
To sign the message using RSA-PSS standard callrsaPssSigner.sign() which
returns the signature of the signed message.
rsaPssVerifier.verify() and pass the signature of the message.
HMAC signature and verification
The following example shows how to generate a secret key, export the key, sign the data, and verify it.Use an AES CTR Block Cipher to encrypt a message
The following example shows how to generate an AES-CTR key, export it, create an AES-256 CTR cipher context builder, and use it to sign a plaintext.Use an AES GCM Block Cipher to encrypt a message
The following example shows how to generate an AES-256 GCM key, export it, create an AES-256 GCM cipher context builder, and use it to sign a plaintext.Use RSA-OAEP to encrypt a message
The following example shows how to generate an RSA-OAEP key, export it, create a context builder, and use it to sign a plaintext.Create a message digest
The following code example callsgetDigestAlgorithmByName() to get the digest,
creates a digest context, encodes an example string, and gets a digest for the string.
Key derivation using PBKDF2 (Password-Based Key Derivation Function 2)
Web Crypto API shim layer
The KeplerCrypto library provides a shim layer that implements the Web Crypto API. We refer to the existing Web Crypto API MDN documentation for the API methods and parameters.Web Crypto usage example
The following example presents how to import and use the Web Crypto API shim layer.Import the API
Initialize the WebCrypto object
Optional: Expose the crypto instance on globalThis
Call to Web Crypto APIs
The next code example generates an ECDHCryptoKeyPair.
Supported Web Crypto algorithms
This library is a work in progress and the Web Crypto implementation provides a subset of the whole W3C Web Crypto specification. More operations will be supported in future versions. Note: deriveKey is not supported in the Web Crypto shim layer implementation. For key derivation functionality, please use the standard Vega Crypto API, as shown in the above “Key derivation using HKDF” and “Key derivation using PBKDF2” examples.Supported key formats for Web Crypto
When importing and exporting keys with the Web Crypto API, the following formats are supported:- raw: Unformatted binary data, used for symmetric keys (AES or HMAC), or Elliptic Curve public keys.
- pkcs8: PKCS #8 format for RSA or Elliptic Curve private keys.
- spki: SubjectPublicKeyInfo format for RSA or Elliptic Curve public keys.
- jwk: JSON Web Key format (supported by all key types)
Web Crypto API Support Matrix
Algorithm Operations:- ✓ = Supported
- ✗ = Not Supported
Overview of Vega Crypto
The Vega Crypto API provides functionality that allows you to perform typical cryptographic operations such as read, generate, and manage cryptographic keys using industry-standard algorithms.Get started
The Vega Crypto API is supplied as part of the Kepler SDK.Install and setup Vega Crypto
To add the API to your project, in the root directory of your app, open the package.json file and add the following dependency.Use the Vega Crypto API in your app
In the source file in which you are using the Vega Crypto functionality, import the data types that you need from the Turbo Module.Crypto instance.
Crypto class is always the first step to make use of the
Vega Crypto Turbo Module.
Vega Crypto common use cases
The following example code is for explanatory purposes and is simplified for clarity. Most of the methods in the Vega Crypto API throw an exception when an error occurs. Wrap your calls in a try-catch block to gracefully handle any errors that occur. For example:Step 1: Create an ECDSA key builder
Step 2: Generate a keypair
You can generate an ECDSA keypair or construct a private key. To generate an ECDSA keypair, callbuildGenerated().
buildPrivateFromDer().
Step 3: Create a signature context builder
Step 4: Sign the message and obtain the signature
Step 5: Verify the message with the signature
Step 1: Create an RSA key builder
Step 2: Generate a keypair
You can generate an RSA keypair or construct a private key. To generate an RSA keypair, callbuildGenerated().
buildPrivateFromDer().
Step 3: Create a signature context builder and sign the message
Create a signature context builder by callingbuildSigningContext() and then
sign the message with the RSA-PKCS1 v1.5 scheme to get the signature.
Step 4: Verify the message
Use the signature to verify the message by callingrsaPkcs1Verifier.verify()
which uses the RSA-PKCS1 v1.5 standard.
Step 5: Sign the message and get the signature
To sign the message using RSA-PSS standard callrsaPssSigner.sign() which
returns the signature of the signed message.
rsaPssVerifier.verify() and pass the signature of
the message.
getDigestAlgorithmByName() to get the digest,
creates a digest context, encodes an example string, and gets a digest for the string.
Web Crypto API shim layer
The Vega Crypto library provides a shim layer that implements the Web Crypto API. We refer to the existing Web Crypto API MDN documentation for the API methods and parameters.Web Crypto usage example
The following example presents how to import and use the Web Crypto API shim layer:Import the API
Initialize the WebCrypto object
Optional: Expose the crypto instance on globalThis
Call to Web Crypto APIs
The next code example generates an ECDHCryptoKeyPair:
Supported Web Crypto algorithms
This library is a work in progress and the Web Crypto implementation provides a subset of the whole W3C Web Crypto specification. More operations will be supported in future versions.Supported key formats for Web Crypto
When importing and exporting keys with the Web Crypto API, the following formats are supported:- raw: Unformatted binary data, used for symmetric keys or Elliptic Curve public keys.
- pkcs8: PKCS #8 format for RSA or Elliptic Curve private keys.
- spki: SubjectPublicKeyInfo format for RSA or Elliptic Curve public keys.
- jwk: JSON Web Key format for symmetric keys or RSA and Elliptic Curve public/private keys.
Web Crypto API Support Matrix
Algorithm Operations:- ✓ = Supported
- ✗ = Unsupported
- — = Not applicable
Modules
- Crypto
- index
- turbo-modules/__mocks__/KeplerCrypto
- turbo-modules/__mocks__/KeplerCrypto
- turbo-modules/__mocks__/KeplerCrypto
- turbo-modules/KeplerCrypto
- turbo-modules/KeplerCrypto
- webcrypto/Crypto
- webcrypto/Crypto
- webcrypto/errors
- webcrypto/errors
- webcrypto/SubtleCrypto
- webcrypto/SubtleCrypto
- webcrypto/types
- webcrypto/types
- webcrypto/WebCrypto
- webcrypto/WebCrypto

