You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Encrypt and decrypt values stored in [Keyv](https://github.com/jaredwray/keyv) using the Node.js `crypto` module. Supports AES-GCM (default), AES-CCM, ChaCha20-Poly1305, AES-CBC, and any cipher available in your Node.js installation.
The encryption key. String keys are hashed with SHA-256 and truncated to the required length for the algorithm. Buffer keys are used directly and must match the expected key length.
41
+
42
+
#### options.algorithm
43
+
44
+
Type: `string`\
45
+
Default: `'aes-256-gcm'`
46
+
47
+
The cipher algorithm to use. Supports any algorithm available via Node.js `crypto.getCipherInfo()`, including:
The encoding used for the encrypted output string. Common options: `'base64'`, `'hex'`.
60
+
61
+
## Cross-Compatibility
62
+
63
+
Data encrypted with `@keyv/encrypt-node` using AES-GCM or AES-CBC can be decrypted by `@keyv/encrypt-web` (and vice versa) when using the same key and algorithm. Both packages use the same wire format:
Copy file name to clipboardExpand all lines: encryption/encrypt-node/src/index.ts
+45-3Lines changed: 45 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -14,15 +14,39 @@ const CCM_MODES = new Set(["ccm"]);
14
14
15
15
constAUTH_TAG_LENGTH=16;
16
16
17
+
/**
18
+
* Options for {@link KeyvEncryptNode}.
19
+
*/
17
20
exporttypeKeyvEncryptNodeOptions={
18
-
/** Encryption key. Strings are hashed with SHA-256 to derive a 32-byte key. Buffers are used directly. */
21
+
/** Encryption key. Strings are hashed with SHA-256 and truncated to the required length. Buffers are used directly and must match the algorithm's key length. */
19
22
key: string|Buffer;
20
-
/** Cipher algorithm to use. Default: "aes-256-gcm". */
23
+
/** Cipher algorithm to use. Any algorithm supported by Node.js `crypto.getCipherInfo()`. @defaultValue `"aes-256-gcm"` */
21
24
algorithm?: string;
22
-
/** Output encoding for the encrypted string. Default: "base64". */
25
+
/** Output encoding for the encrypted string. @defaultValue `"base64"` */
23
26
encoding?: BufferEncoding;
24
27
};
25
28
29
+
/**
30
+
* Node.js `crypto`-based encryption adapter for Keyv.
31
+
*
32
+
* Encrypts and decrypts string values using any cipher supported by the
33
+
* Node.js `crypto` module. Defaults to AES-256-GCM with authenticated
34
+
* encryption. The encrypted output is a base64 string containing the IV,
35
+
* authentication tag (for AEAD ciphers), and ciphertext.
Encrypt and decrypt values stored in [Keyv](https://github.com/jaredwray/keyv) using the Web Crypto API (`crypto.subtle`). Works in browsers, Deno, Cloudflare Workers, and Node.js 18+. No Node.js-specific dependencies.
The encryption key. String keys are hashed with SHA-256 and truncated to the required length for the algorithm. Uint8Array keys are used directly and must match the expected key length.
Data encrypted with `@keyv/encrypt-web` using AES-GCM or AES-CBC can be decrypted by `@keyv/encrypt-node` (and vice versa) when using the same key and algorithm. Both packages use the same wire format:
0 commit comments