1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
# Class: CompactEncrypt
[💗 Help the project](https://github.com/sponsors/panva)
The CompactEncrypt class is a utility for creating Compact JWE strings.
**`example`** Usage
```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)
```
## Table of contents
### Constructors
- [constructor](jwe_compact_encrypt.CompactEncrypt.md#constructor)
### Methods
- [encrypt](jwe_compact_encrypt.CompactEncrypt.md#encrypt)
- [setContentEncryptionKey](jwe_compact_encrypt.CompactEncrypt.md#setcontentencryptionkey)
- [setInitializationVector](jwe_compact_encrypt.CompactEncrypt.md#setinitializationvector)
- [setKeyManagementParameters](jwe_compact_encrypt.CompactEncrypt.md#setkeymanagementparameters)
- [setProtectedHeader](jwe_compact_encrypt.CompactEncrypt.md#setprotectedheader)
## Constructors
### constructor
• **new CompactEncrypt**(`plaintext`)
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `plaintext` | `Uint8Array` | Binary representation of the plaintext to encrypt. |
## Methods
### encrypt
â–¸ **encrypt**(`key`, `options?`): `Promise`<`string`\>
Encrypts and resolves the value of the Compact JWE string.
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `key` | `Uint8Array` \| [`KeyLike`](../types/types.KeyLike.md) | Public Key or Secret to encrypt the JWE with. |
| `options?` | [`EncryptOptions`](../interfaces/types.EncryptOptions.md) | JWE Encryption options. |
#### Returns
`Promise`<`string`\>
___
### setContentEncryptionKey
â–¸ **setContentEncryptionKey**(`cek`): [`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
Sets a content encryption key to use, by default a random suitable one is generated for the JWE
enc" (Encryption Algorithm) Header Parameter.
**`deprecated`** You should not use this method. It is only really intended for test and vector
validation purposes.
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `cek` | `Uint8Array` | JWE Content Encryption Key. |
#### Returns
[`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
___
### setInitializationVector
â–¸ **setInitializationVector**(`iv`): [`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
Sets the JWE Initialization Vector to use for content encryption, by default a random suitable
one is generated for the JWE enc" (Encryption Algorithm) Header Parameter.
**`deprecated`** You should not use this method. It is only really intended for test and vector
validation purposes.
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `iv` | `Uint8Array` | JWE Initialization Vector. |
#### Returns
[`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
___
### setKeyManagementParameters
â–¸ **setKeyManagementParameters**(`parameters`): [`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
Sets the JWE Key Management parameters to be used when encrypting the Content Encryption Key.
You do not need to invoke this method, it is only really intended for test and vector
validation purposes.
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `parameters` | [`JWEKeyManagementHeaderParameters`](../interfaces/types.JWEKeyManagementHeaderParameters.md) | JWE Key Management parameters. |
#### Returns
[`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
___
### setProtectedHeader
â–¸ **setProtectedHeader**(`protectedHeader`): [`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
Sets the JWE Protected Header on the CompactEncrypt object.
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `protectedHeader` | [`CompactJWEHeaderParameters`](../interfaces/types.CompactJWEHeaderParameters.md) | JWE Protected Header object. |
#### Returns
[`CompactEncrypt`](jwe_compact_encrypt.CompactEncrypt.md)
|