Back to Jose

Class: CompactEncrypt

docs/jwe/compact/encrypt/classes/CompactEncrypt.md

6.2.123.8 KB
Original Source

Class: CompactEncrypt

💗 Help the project

Support from the community to continue maintaining and improving this module is welcome. If you find the module useful, please consider supporting the project by becoming a sponsor.

Builds and encrypts Compact JWE strings.

This class is exported (as a named export) from the main 'jose' module entry point as well as from its subpath export 'jose/jwe/compact/encrypt'.

Example

js
const jwe = await new jose.CompactEncrypt(
  new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
)
  .setProtectedHeader({ alg: 'RSA-OAEP-256', enc: 'A256GCM' })
  .encrypt(publicKey)

console.log(jwe)

Constructors

Constructor

â–¸ new CompactEncrypt(plaintext): CompactEncrypt

Creates a Compact JWE encryptor.

Parameters

ParameterTypeDescription
plaintextUint8ArrayBinary representation of the plaintext to encrypt.

Returns

CompactEncrypt

Methods

encrypt()

â–¸ encrypt(key, options?): Promise<string>

Encrypts the plaintext as a Compact JWE.

Parameters

ParameterTypeDescription
keyKeyInputPublic key or shared secret. See Algorithm Key Requirements.
options?EncryptOptionsJWE Encryption options.

Returns

Promise<string>


setContentEncryptionKey()

â–¸ setContentEncryptionKey(cek): this

Sets a content encryption key instead of generating a random one for the JWE "enc" algorithm. May only be called once.

Parameters

ParameterTypeDescription
cekUint8ArrayJWE Content Encryption Key.

Returns

this

Deprecated

Use only for testing and vector validation.


setInitializationVector()

â–¸ setInitializationVector(iv): this

Sets the content encryption IV instead of generating a random one for the JWE "enc" algorithm. May only be called once.

Parameters

ParameterTypeDescription
ivUint8ArrayJWE Initialization Vector.

Returns

this

Deprecated

Use only for testing and vector validation.


setKeyManagementParameters()

â–¸ setKeyManagementParameters(parameters): this

Sets key management inputs such as ECDH-ES "apu"/"apv" or PBES2 "p2c". Use this method instead of header setters; the resulting parameters are added to the JOSE header. May only be called once.

Parameters

ParameterTypeDescription
parametersJWEKeyManagementHeaderParametersJWE Key Management parameters.

Returns

this


setProtectedHeader()

â–¸ setProtectedHeader(protectedHeader): this

Sets the JWE Protected Header. May only be called once.

Parameters

ParameterTypeDescription
protectedHeaderCompactJWEHeaderParametersJWE Protected Header object.

Returns

this