unjwt 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +518 -48
  3. package/dist/index.d.mts +6 -79
  4. package/dist/index.d.ts +6 -79
  5. package/dist/index.mjs +5 -141
  6. package/dist/jwe.d.mts +3 -0
  7. package/dist/jwe.d.ts +3 -0
  8. package/dist/jwe.mjs +4 -0
  9. package/dist/jwk.d.mts +2 -0
  10. package/dist/jwk.d.ts +2 -0
  11. package/dist/jwk.mjs +2 -0
  12. package/dist/jws.d.mts +3 -0
  13. package/dist/jws.d.ts +3 -0
  14. package/dist/jws.mjs +4 -0
  15. package/dist/shared/unjwt.B4e_rwol.mjs +453 -0
  16. package/dist/shared/unjwt.B9uGngq-.d.mts +154 -0
  17. package/dist/shared/unjwt.BKG_8Y9G.d.ts +189 -0
  18. package/dist/shared/unjwt.BOEZHxi_.d.ts +154 -0
  19. package/dist/shared/unjwt.Bzcov22m.mjs +167 -0
  20. package/dist/shared/unjwt.C0bBN0AC.d.mts +200 -0
  21. package/dist/shared/unjwt.C0bBN0AC.d.ts +200 -0
  22. package/dist/shared/unjwt.CJ_ohCFC.d.mts +46 -0
  23. package/dist/shared/unjwt.CjHZv3F3.mjs +57 -0
  24. package/dist/shared/unjwt.D90BbcE1.d.mts +52 -0
  25. package/dist/shared/unjwt.DKq5VF7t.mjs +1186 -0
  26. package/dist/shared/unjwt.DYFIh1YE.d.mts +100 -0
  27. package/dist/shared/unjwt.DoVzOnty.d.ts +52 -0
  28. package/dist/shared/unjwt.Dz5Oe9BG.d.mts +189 -0
  29. package/dist/shared/unjwt.mc4AEAT7.d.ts +46 -0
  30. package/dist/shared/unjwt.ppm5Y0k7.d.ts +100 -0
  31. package/dist/shared/unjwt.sux3_Z19.mjs +101 -0
  32. package/dist/utils.d.mts +2 -0
  33. package/dist/utils.d.ts +2 -0
  34. package/dist/utils.mjs +1 -0
  35. package/package.json +44 -84
  36. package/dist/crypto/node.cjs +0 -19
  37. package/dist/crypto/node.d.cts +0 -5
  38. package/dist/crypto/node.d.mts +0 -5
  39. package/dist/crypto/node.d.ts +0 -5
  40. package/dist/crypto/node.mjs +0 -11
  41. package/dist/crypto/web.cjs +0 -14
  42. package/dist/crypto/web.d.cts +0 -5
  43. package/dist/crypto/web.d.mts +0 -5
  44. package/dist/crypto/web.d.ts +0 -5
  45. package/dist/crypto/web.mjs +0 -10
  46. package/dist/index.cjs +0 -146
  47. package/dist/index.d.cts +0 -79
@@ -0,0 +1,453 @@
1
+ import { a as checkEncCryptoKey, u as uint32be, g as generateCEK, e as encryptIV, w as wrap, k as keyToJWK, b as bitLengthCEK, d as jwkTokey, i as importKey, f as generateIV, h as encrypt$1, l as unwrapKey, m as decrypt$1 } from './unjwt.DKq5VF7t.mjs';
2
+ import { e as concatUint8Arrays, t as textEncoder, f as assertCryptoKey, b as base64UrlEncode, g as isCryptoKey, a as isJWK, d as base64UrlDecode, r as randomBytes, c as textDecoder } from './unjwt.CjHZv3F3.mjs';
3
+ import { c as checkKeyLength } from './unjwt.sux3_Z19.mjs';
4
+
5
+ function lengthAndInput(input) {
6
+ return concatUint8Arrays(uint32be(input.length), input);
7
+ }
8
+ async function concatKdf(secret, bits, value) {
9
+ const iterations = Math.ceil((bits >> 3) / 32);
10
+ const res = new Uint8Array(iterations * 32);
11
+ for (let iter = 0; iter < iterations; iter++) {
12
+ const buf = new Uint8Array(4 + secret.length + value.length);
13
+ buf.set(uint32be(iter + 1));
14
+ buf.set(secret, 4);
15
+ buf.set(value, 4 + secret.length);
16
+ res.set(await digest("sha256", buf), iter * 32);
17
+ }
18
+ return res.slice(0, bits >> 3);
19
+ }
20
+ async function deriveECDHESKey(publicKey, privateKey, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) {
21
+ checkEncCryptoKey(publicKey, "ECDH");
22
+ checkEncCryptoKey(privateKey, "ECDH", "deriveBits");
23
+ const value = concatUint8Arrays(
24
+ lengthAndInput(textEncoder.encode(algorithm)),
25
+ lengthAndInput(apu),
26
+ lengthAndInput(apv),
27
+ uint32be(keyLength)
28
+ );
29
+ const length = publicKey.algorithm.name === "X25519" ? 256 : Math.ceil(
30
+ Number.parseInt(
31
+ publicKey.algorithm.namedCurve.slice(-3),
32
+ 10
33
+ ) / 8
34
+ ) << 3;
35
+ const sharedSecret = new Uint8Array(
36
+ await crypto.subtle.deriveBits(
37
+ {
38
+ name: publicKey.algorithm.name,
39
+ public: publicKey
40
+ },
41
+ privateKey,
42
+ length
43
+ )
44
+ );
45
+ return concatKdf(sharedSecret, keyLength, value);
46
+ }
47
+ function allowed(key) {
48
+ switch (key.algorithm.namedCurve) {
49
+ case "P-256":
50
+ case "P-384":
51
+ case "P-521": {
52
+ return true;
53
+ }
54
+ default: {
55
+ return key.algorithm.name === "X25519";
56
+ }
57
+ }
58
+ }
59
+ async function digest(algorithm, data) {
60
+ const subtleDigest = `SHA-${algorithm.slice(-3)}`;
61
+ return new Uint8Array(await crypto.subtle.digest(subtleDigest, data));
62
+ }
63
+
64
+ const subtleAlgorithm = (alg) => {
65
+ switch (alg) {
66
+ case "RSA-OAEP":
67
+ case "RSA-OAEP-256":
68
+ case "RSA-OAEP-384":
69
+ case "RSA-OAEP-512": {
70
+ return "RSA-OAEP";
71
+ }
72
+ default: {
73
+ throw new Error(
74
+ `alg ${alg} is not supported either by JOSE or your javascript runtime`
75
+ );
76
+ }
77
+ }
78
+ };
79
+ async function encryptRSAES(alg, key, cek) {
80
+ checkEncCryptoKey(key, alg, "encrypt");
81
+ checkKeyLength(alg, key);
82
+ return new Uint8Array(
83
+ await crypto.subtle.encrypt(subtleAlgorithm(alg), key, cek)
84
+ );
85
+ }
86
+
87
+ /**
88
+ * This is a fork of Jose library's utility functions.
89
+ * @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/encrypt_key_management.ts
90
+ * @license MIT https://github.com/panva/jose/blob/69b7960c67e05be55fa2ec31c74b987696c20c60/LICENSE.md
91
+ */
92
+ async function encryptKey(alg, enc, key, providedCek, providedParameters = {}) {
93
+ let encryptedKey;
94
+ let parameters = {};
95
+ let cek;
96
+ switch (alg) {
97
+ case "dir": {
98
+ cek = key;
99
+ break;
100
+ }
101
+ case "ECDH-ES":
102
+ case "ECDH-ES+A128KW":
103
+ case "ECDH-ES+A192KW":
104
+ case "ECDH-ES+A256KW": {
105
+ assertCryptoKey(key);
106
+ if (!allowed(key)) {
107
+ throw new Error(
108
+ "ECDH with the provided key is not allowed or not supported by your javascript runtime"
109
+ );
110
+ }
111
+ const { apu, apv } = providedParameters;
112
+ const ephemeralKey = providedParameters.epk ? await normalizeKey(providedParameters.epk, alg) : (await crypto.subtle.generateKey(
113
+ key.algorithm,
114
+ true,
115
+ ["deriveBits"]
116
+ )).privateKey;
117
+ const { x, y, crv, kty } = await keyToJWK(ephemeralKey);
118
+ const sharedSecret = await deriveECDHESKey(
119
+ key,
120
+ ephemeralKey,
121
+ alg === "ECDH-ES" ? enc : alg,
122
+ alg === "ECDH-ES" ? bitLengthCEK(enc) : Number.parseInt(alg.slice(-5, -2), 10),
123
+ apu,
124
+ apv
125
+ );
126
+ parameters = { epk: { x, y, crv, kty } };
127
+ if (apu) parameters.apu = base64UrlEncode(apu);
128
+ if (apv) parameters.apv = base64UrlEncode(apv);
129
+ if (alg === "ECDH-ES") {
130
+ cek = sharedSecret;
131
+ break;
132
+ }
133
+ cek = providedCek || generateCEK(enc);
134
+ const kwAlg = alg.slice(-6);
135
+ encryptedKey = (await wrap(kwAlg, sharedSecret, cek)).encryptedKey;
136
+ break;
137
+ }
138
+ case "RSA-OAEP":
139
+ case "RSA-OAEP-256":
140
+ case "RSA-OAEP-384":
141
+ case "RSA-OAEP-512": {
142
+ cek = providedCek || generateCEK(enc);
143
+ assertCryptoKey(key);
144
+ encryptedKey = await encryptRSAES(alg, key, cek);
145
+ break;
146
+ }
147
+ case "PBES2-HS256+A128KW":
148
+ case "PBES2-HS384+A192KW":
149
+ case "PBES2-HS512+A256KW": {
150
+ cek = providedCek || generateCEK(enc);
151
+ const { p2c, p2s } = providedParameters;
152
+ ({ encryptedKey, ...parameters } = await wrap(alg, key, cek, p2c, p2s));
153
+ break;
154
+ }
155
+ case "A128KW":
156
+ case "A192KW":
157
+ case "A256KW": {
158
+ cek = providedCek || generateCEK(enc);
159
+ encryptedKey = (await wrap(alg, key, cek)).encryptedKey;
160
+ break;
161
+ }
162
+ case "A128GCMKW":
163
+ case "A192GCMKW":
164
+ case "A256GCMKW": {
165
+ cek = providedCek || generateCEK(enc);
166
+ const { iv } = providedParameters;
167
+ ({ encryptedKey, ...parameters } = await encryptIV(alg, key, cek, iv));
168
+ break;
169
+ }
170
+ default: {
171
+ throw new Error(
172
+ 'Invalid or unsupported "alg" (JWE Algorithm) header value'
173
+ );
174
+ }
175
+ }
176
+ return { cek, encryptedKey, parameters };
177
+ }
178
+ async function normalizeKey(key, alg) {
179
+ if (key instanceof Uint8Array) {
180
+ return key;
181
+ }
182
+ if (isCryptoKey(key)) {
183
+ return key;
184
+ }
185
+ if (isJWK(key)) {
186
+ if ("k" in key && key.k) {
187
+ return base64UrlDecode(key.k, false);
188
+ }
189
+ return jwkTokey({ ...key, alg });
190
+ }
191
+ throw new Error("unreachable");
192
+ }
193
+
194
+ async function encrypt(payload, key, options = {}) {
195
+ const {
196
+ protectedHeader: additionalProtectedHeader,
197
+ cek: providedCek,
198
+ contentEncryptionIV: providedContentIV,
199
+ keyManagementIV,
200
+ p2s = randomBytes(16),
201
+ p2c = 2048,
202
+ ecdhPartyUInfo,
203
+ ecdhPartyVInfo
204
+ } = options;
205
+ let { alg, enc } = options;
206
+ if (!alg) {
207
+ if (typeof key === "string" || key instanceof Uint8Array) {
208
+ alg = "PBES2-HS256+A128KW";
209
+ } else if (isJWK(key)) {
210
+ alg = key.alg;
211
+ }
212
+ }
213
+ if (!alg) {
214
+ throw new TypeError(
215
+ 'JWE "alg" (Key Management Algorithm) must be provided in options or inferable from the key'
216
+ );
217
+ }
218
+ if (!enc) {
219
+ enc = isJWK(key) && "enc" in key ? key.enc : "A128GCM";
220
+ }
221
+ const plaintextBytes = getPlaintextBytes(payload);
222
+ const jweKeyManagementParams = {};
223
+ if (keyManagementIV) jweKeyManagementParams.iv = keyManagementIV;
224
+ if (p2s) jweKeyManagementParams.p2s = p2s;
225
+ if (p2c) jweKeyManagementParams.p2c = p2c;
226
+ if (ecdhPartyUInfo) jweKeyManagementParams.apu = ecdhPartyUInfo;
227
+ if (ecdhPartyVInfo) jweKeyManagementParams.apv = ecdhPartyVInfo;
228
+ if (additionalProtectedHeader?.epk instanceof CryptoKey) {
229
+ jweKeyManagementParams.epk = additionalProtectedHeader.epk;
230
+ }
231
+ const wrappingKeyMaterial = await importKey(key, alg);
232
+ const {
233
+ cek: finalCek,
234
+ // This is the CEK (CryptoKey | Uint8Array) to be used for content encryption
235
+ encryptedKey: jweEncryptedKey,
236
+ // This is the JWE Encrypted Key (Uint8Array | undefined)
237
+ parameters: keyManagementHeaderParams
238
+ // JWE header params from key encryption (e.g., epk, p2s, iv, tag)
239
+ } = await encryptKey(
240
+ alg,
241
+ enc,
242
+ wrappingKeyMaterial,
243
+ providedCek,
244
+ jweKeyManagementParams
245
+ );
246
+ const baseProtectedHeader = { ...additionalProtectedHeader };
247
+ delete baseProtectedHeader.alg;
248
+ delete baseProtectedHeader.enc;
249
+ const jweProtectedHeader = {
250
+ ...baseProtectedHeader,
251
+ ...keyManagementHeaderParams,
252
+ alg,
253
+ enc
254
+ };
255
+ if (jweProtectedHeader.typ === void 0 && typeof payload === "object" && !(payload instanceof Uint8Array)) {
256
+ jweProtectedHeader.typ = "JWT";
257
+ }
258
+ if (jweProtectedHeader.typ === "JWT" && typeof payload === "object" && !(payload instanceof Uint8Array)) {
259
+ jweProtectedHeader.cty ||= "json";
260
+ }
261
+ const protectedHeaderSerialized = JSON.stringify(jweProtectedHeader);
262
+ const protectedHeaderEncoded = base64UrlEncode(protectedHeaderSerialized);
263
+ const aadBytes = textEncoder.encode(protectedHeaderEncoded);
264
+ const contentIVBytes = providedContentIV ?? generateIV(enc);
265
+ const {
266
+ ciphertext: contentCiphertext,
267
+ tag: contentAuthTag,
268
+ iv: actualContentIV
269
+ } = await encrypt$1(
270
+ enc,
271
+ plaintextBytes,
272
+ finalCek,
273
+ contentIVBytes,
274
+ aadBytes
275
+ );
276
+ if (!actualContentIV) {
277
+ throw new Error("Content encryption IV was not generated or returned.");
278
+ }
279
+ if (!contentAuthTag) {
280
+ throw new Error(
281
+ "Content encryption auth tag was not generated or returned."
282
+ );
283
+ }
284
+ const encryptedKeyEncoded = jweEncryptedKey ? base64UrlEncode(jweEncryptedKey) : "";
285
+ const jweParts = [
286
+ protectedHeaderEncoded,
287
+ encryptedKeyEncoded,
288
+ base64UrlEncode(actualContentIV),
289
+ base64UrlEncode(contentCiphertext),
290
+ base64UrlEncode(contentAuthTag)
291
+ ];
292
+ return jweParts.join(".");
293
+ }
294
+ async function decrypt(jwe, key, options) {
295
+ const parts = jwe.split(".");
296
+ if (parts.length !== 5) {
297
+ throw new Error(
298
+ "Invalid JWE: Must contain five sections (RFC7516, section-3)."
299
+ );
300
+ }
301
+ const [
302
+ protectedHeaderEncoded,
303
+ encryptedKeyEncoded,
304
+ ivEncoded,
305
+ ciphertextEncoded,
306
+ authTagEncoded
307
+ ] = parts;
308
+ let protectedHeader;
309
+ try {
310
+ const protectedHeaderJson = base64UrlDecode(protectedHeaderEncoded);
311
+ protectedHeader = JSON.parse(protectedHeaderJson);
312
+ } catch (error_) {
313
+ throw new Error(
314
+ `Invalid JWE: Protected header is not valid Base64URL or JSON (${error_ instanceof Error ? error_.message : error_})`
315
+ );
316
+ }
317
+ if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg || !protectedHeader.enc) {
318
+ throw new Error(
319
+ 'Invalid JWE: Protected header must be an object with "alg" and "enc" properties.'
320
+ );
321
+ }
322
+ const alg = protectedHeader.alg;
323
+ const enc = protectedHeader.enc;
324
+ if (options?.algorithms && !options.algorithms.includes(alg)) {
325
+ throw new Error(`Key management algorithm not allowed: ${alg}`);
326
+ }
327
+ if (options?.encryptionAlgorithms && !options.encryptionAlgorithms.includes(enc)) {
328
+ throw new Error(`Content encryption algorithm not allowed: ${enc}`);
329
+ }
330
+ const resolvedKeyMaterial = typeof key === "function" ? await key(protectedHeader) : key;
331
+ const unwrappingKey = await importKey(resolvedKeyMaterial, alg);
332
+ const encryptedKeyBytes = base64UrlDecode(encryptedKeyEncoded, false);
333
+ const contentIVBytes = base64UrlDecode(ivEncoded, false);
334
+ const contentAuthTagBytes = base64UrlDecode(authTagEncoded, false);
335
+ const ciphertextBytes = base64UrlDecode(ciphertextEncoded, false);
336
+ const unwrapKeyOpts = {
337
+ iv: protectedHeader.iv,
338
+ tag: protectedHeader.tag,
339
+ p2s: protectedHeader.p2s,
340
+ p2c: protectedHeader.p2c,
341
+ epk: protectedHeader.epk,
342
+ apu: protectedHeader.apu,
343
+ apv: protectedHeader.apv,
344
+ unwrappedKeyAlgorithm: options?.unwrappedKeyAlgorithm,
345
+ keyUsage: options?.keyUsage,
346
+ extractable: options?.extractable,
347
+ returnAs: false
348
+ // Returning CEK as Uint8Array
349
+ };
350
+ const cekBytes = await unwrapKey(
351
+ alg,
352
+ encryptedKeyBytes,
353
+ unwrappingKey,
354
+ unwrapKeyOpts
355
+ );
356
+ const aadBytes = textEncoder.encode(protectedHeaderEncoded);
357
+ const plaintextBytes = await decrypt$1(
358
+ enc,
359
+ cekBytes,
360
+ ciphertextBytes,
361
+ contentIVBytes,
362
+ contentAuthTagBytes,
363
+ aadBytes
364
+ );
365
+ let payload;
366
+ if (options?.forceUint8Array) {
367
+ payload = plaintextBytes;
368
+ } else {
369
+ const decodedString = textDecoder.decode(plaintextBytes);
370
+ const cty = protectedHeader.cty?.toLowerCase();
371
+ const isJsonOutput = protectedHeader.typ === "JWT" || cty === "json" || cty === "application/json" || cty && cty.endsWith("+json");
372
+ if (isJsonOutput) {
373
+ if (decodedString.startsWith("{") && decodedString.endsWith("}") || decodedString.startsWith("[") && decodedString.endsWith("]")) {
374
+ try {
375
+ payload = JSON.parse(decodedString);
376
+ } catch {
377
+ payload = decodedString;
378
+ }
379
+ } else {
380
+ payload = decodedString;
381
+ }
382
+ } else {
383
+ payload = decodedString;
384
+ }
385
+ }
386
+ if (options?.critical && protectedHeader.crit) {
387
+ const understoodParams = /* @__PURE__ */ new Set([
388
+ ...options.critical || [],
389
+ // JWE specific standard headers:
390
+ "alg",
391
+ "enc",
392
+ "typ",
393
+ "cty",
394
+ "kid",
395
+ "jwk",
396
+ "jku",
397
+ "x5c",
398
+ "x5t",
399
+ "x5u",
400
+ "iv",
401
+ "tag",
402
+ "p2s",
403
+ "p2c",
404
+ "epk",
405
+ "apu",
406
+ "apv"
407
+ ]);
408
+ for (const critParam of protectedHeader.crit) {
409
+ if (!Object.prototype.hasOwnProperty.call(protectedHeader, critParam)) {
410
+ throw new Error(
411
+ `Critical header parameter "${critParam}" listed in "crit" but not present in the protected header.`
412
+ );
413
+ }
414
+ if (!understoodParams.has(critParam)) {
415
+ throw new Error(
416
+ `Critical header parameter not understood: ${critParam}`
417
+ );
418
+ }
419
+ }
420
+ } else if (!options?.critical && protectedHeader.crit) {
421
+ throw new Error(
422
+ `Unprocessed critical header parameters: ${protectedHeader.crit.join(", ")}`
423
+ );
424
+ }
425
+ return {
426
+ payload,
427
+ protectedHeader,
428
+ cek: cekBytes,
429
+ aad: aadBytes
430
+ };
431
+ }
432
+ function getPlaintextBytes(payload) {
433
+ if (payload instanceof Uint8Array) {
434
+ return payload;
435
+ }
436
+ if (typeof payload === "string") {
437
+ return textEncoder.encode(payload);
438
+ }
439
+ if (typeof payload === "object" && payload !== null) {
440
+ return textEncoder.encode(JSON.stringify(payload));
441
+ }
442
+ throw new TypeError(
443
+ "Plaintext must be a string, Uint8Array, or a JSON-serializable object."
444
+ );
445
+ }
446
+
447
+ const jwe = {
448
+ __proto__: null,
449
+ decrypt: decrypt,
450
+ encrypt: encrypt
451
+ };
452
+
453
+ export { decrypt as d, encrypt as e, jwe as j };
@@ -0,0 +1,154 @@
1
+ import { G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, w as JWK_PBES2, D as DeriveKeyOptions, c as DeriveKeyReturn, l as JWK_oct, m as JWK, K as KeyManagementAlgorithm, W as WrapKeyOptions, e as WrapKeyResult, U as UnwrapKeyOptions, C as CompositeKey, d as ContentEncryptionAlgorithm, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm } from './unjwt.C0bBN0AC.mjs';
2
+
3
+ /**
4
+ * Generates a cryptographic key for the specified algorithm.
5
+ *
6
+ * @param alg The JWA algorithm identifier (e.g., "HS256", "RS256", "A128GCM").
7
+ * @param options Configuration options for key generation.
8
+ * @returns A Promise resolving to the generated key (CryptoKey, CryptoKeyPair, or Uint8Array) or its JWK representation.
9
+ */
10
+ declare function generateKey<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions>(alg: TAlg, options?: TOptions): Promise<GenerateKeyReturn<TAlg, TOptions>>;
11
+ /**
12
+ * Derives a key from a password using PBKDF2 as specified by PBES2 algorithms.
13
+ *
14
+ * @param password The password to derive the key from (string or Uint8Array).
15
+ * @param alg The PBES2 algorithm identifier (e.g., "PBES2-HS256+A128KW").
16
+ * @param options Configuration options including salt and iterations.
17
+ * @returns A Promise resolving to the derived key (CryptoKey) or its JWK_oct representation.
18
+ */
19
+ declare function deriveKeyFromPassword<TAlg extends JWK_PBES2, TOptions extends DeriveKeyOptions>(password: string | Uint8Array, alg: TAlg, options: TOptions): Promise<DeriveKeyReturn<TOptions>>;
20
+ /**
21
+ * Imports a key from various formats (CryptoKey, JWK, Uint8Array).
22
+ *
23
+ * - If `key` is a CryptoKey, it's returned directly.
24
+ * - If `key` is a Uint8Array, it's returned directly.
25
+ * - If `key` is a JWK_oct (symmetric key with 'k'), the raw key bytes are returned as Uint8Array.
26
+ * - If `key` is any other JWK type (asymmetric), the `alg` parameter is required, and a CryptoKey is returned.
27
+ *
28
+ * @param key The key to import.
29
+ * @param alg The algorithm hint, required when importing asymmetric JWKs.
30
+ * @returns A Promise resolving to the imported key as CryptoKey or Uint8Array.
31
+ */
32
+ declare function importKey(key: string): Promise<Uint8Array>;
33
+ declare function importKey(key: Uint8Array): Promise<Uint8Array>;
34
+ declare function importKey(key: CryptoKey): Promise<CryptoKey>;
35
+ declare function importKey(key: JWK_oct): Promise<Uint8Array>;
36
+ declare function importKey(key: Exclude<JWK, JWK_oct>, alg: string): Promise<CryptoKey>;
37
+ declare function importKey(key: CryptoKey | JWK | Uint8Array | string, alg?: string): Promise<CryptoKey | Uint8Array>;
38
+ /**
39
+ * Exports a CryptoKey to a JWK (JSON Web Key) format.
40
+ *
41
+ * @param key The CryptoKey to export.
42
+ * @param jwk Optional partial JWK to merge with the exported key, allowing overrides.
43
+ * @returns A Promise resolving to the exported JWK.
44
+ */
45
+ declare function exportKey(key: CryptoKey, jwk?: Partial<JWK>): Promise<JWK>;
46
+ /**
47
+ * Wraps a Content Encryption Key (CEK) using the specified algorithm and wrapping key.
48
+ *
49
+ * @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
50
+ * @param keyToWrap The key to be wrapped (CEK), typically a symmetric key as Uint8Array or CryptoKey.
51
+ * @param wrappingKey The key used to wrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
52
+ * @param options Additional options required by certain algorithms (e.g., p2s, p2c for PBES2).
53
+ * @returns A Promise resolving to an object containing the wrapped key and any necessary parameters (iv, tag, epk, etc.).
54
+ */
55
+ declare function wrapKey(alg: KeyManagementAlgorithm, keyToWrap: CryptoKey | Uint8Array, wrappingKey: CryptoKey | JWK | string | Uint8Array, options?: WrapKeyOptions): Promise<WrapKeyResult>;
56
+ /**
57
+ * Unwraps a Content Encryption Key (CEK) using the specified algorithm and unwrapping key.
58
+ *
59
+ * @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
60
+ * @param wrappedKey The wrapped key (ciphertext) as Uint8Array.
61
+ * @param unwrappingKey The key used to unwrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
62
+ * @param options Additional options required by certain algorithms (e.g., iv, tag, p2s, p2c, epk).
63
+ * @returns A Promise resolving to the unwrapped key (CEK) as a CryptoKey or Uint8Array.
64
+ */
65
+ declare function unwrapKey<T extends boolean | undefined = undefined>(alg: KeyManagementAlgorithm, wrappedKey: Uint8Array, unwrappingKey: CryptoKey | JWK | string | Uint8Array, options?: UnwrapKeyOptions & {
66
+ returnAs?: T;
67
+ }): Promise<T extends false ? Uint8Array : CryptoKey>;
68
+
69
+ declare const jwk_CompositeKey: typeof CompositeKey;
70
+ declare const jwk_ContentEncryptionAlgorithm: typeof ContentEncryptionAlgorithm;
71
+ declare const jwk_DeriveKeyOptions: typeof DeriveKeyOptions;
72
+ declare const jwk_DeriveKeyReturn: typeof DeriveKeyReturn;
73
+ declare const jwk_GenerateKeyAlgorithm: typeof GenerateKeyAlgorithm;
74
+ declare const jwk_GenerateKeyOptions: typeof GenerateKeyOptions;
75
+ declare const jwk_GenerateKeyReturn: typeof GenerateKeyReturn;
76
+ declare const jwk_JWK: typeof JWK;
77
+ declare const jwk_JWKAlgorithm: typeof JWKAlgorithm;
78
+ declare const jwk_JWKParameters: typeof JWKParameters;
79
+ declare const jwk_JWK_AES_CBC_HMAC: typeof JWK_AES_CBC_HMAC;
80
+ declare const jwk_JWK_AES_GCM: typeof JWK_AES_GCM;
81
+ declare const jwk_JWK_AES_GCM_KW: typeof JWK_AES_GCM_KW;
82
+ declare const jwk_JWK_AES_KW: typeof JWK_AES_KW;
83
+ declare const jwk_JWK_Asymmetric_Algorithm: typeof JWK_Asymmetric_Algorithm;
84
+ declare const jwk_JWK_EC: typeof JWK_EC;
85
+ declare const jwk_JWK_ECDH_ES: typeof JWK_ECDH_ES;
86
+ declare const jwk_JWK_ECDSA: typeof JWK_ECDSA;
87
+ declare const jwk_JWK_EC_Private: typeof JWK_EC_Private;
88
+ declare const jwk_JWK_EC_Public: typeof JWK_EC_Public;
89
+ declare const jwk_JWK_HMAC: typeof JWK_HMAC;
90
+ declare const jwk_JWK_PBES2: typeof JWK_PBES2;
91
+ declare const jwk_JWK_RSA: typeof JWK_RSA;
92
+ declare const jwk_JWK_RSA_ENC: typeof JWK_RSA_ENC;
93
+ declare const jwk_JWK_RSA_PSS: typeof JWK_RSA_PSS;
94
+ declare const jwk_JWK_RSA_Private: typeof JWK_RSA_Private;
95
+ declare const jwk_JWK_RSA_Public: typeof JWK_RSA_Public;
96
+ declare const jwk_JWK_RSA_SIGN: typeof JWK_RSA_SIGN;
97
+ declare const jwk_JWK_Symmetric_Algorithm: typeof JWK_Symmetric_Algorithm;
98
+ declare const jwk_JWK_oct: typeof JWK_oct;
99
+ declare const jwk_KeyManagementAlgorithm: typeof KeyManagementAlgorithm;
100
+ declare const jwk_UnwrapKeyOptions: typeof UnwrapKeyOptions;
101
+ declare const jwk_WrapKeyOptions: typeof WrapKeyOptions;
102
+ declare const jwk_WrapKeyResult: typeof WrapKeyResult;
103
+ declare const jwk_deriveKeyFromPassword: typeof deriveKeyFromPassword;
104
+ declare const jwk_exportKey: typeof exportKey;
105
+ declare const jwk_generateKey: typeof generateKey;
106
+ declare const jwk_importKey: typeof importKey;
107
+ declare const jwk_unwrapKey: typeof unwrapKey;
108
+ declare const jwk_wrapKey: typeof wrapKey;
109
+ declare namespace jwk {
110
+ export {
111
+ jwk_CompositeKey as CompositeKey,
112
+ jwk_ContentEncryptionAlgorithm as ContentEncryptionAlgorithm,
113
+ jwk_DeriveKeyOptions as DeriveKeyOptions,
114
+ jwk_DeriveKeyReturn as DeriveKeyReturn,
115
+ jwk_GenerateKeyAlgorithm as GenerateKeyAlgorithm,
116
+ jwk_GenerateKeyOptions as GenerateKeyOptions,
117
+ jwk_GenerateKeyReturn as GenerateKeyReturn,
118
+ jwk_JWK as JWK,
119
+ jwk_JWKAlgorithm as JWKAlgorithm,
120
+ jwk_JWKParameters as JWKParameters,
121
+ jwk_JWK_AES_CBC_HMAC as JWK_AES_CBC_HMAC,
122
+ jwk_JWK_AES_GCM as JWK_AES_GCM,
123
+ jwk_JWK_AES_GCM_KW as JWK_AES_GCM_KW,
124
+ jwk_JWK_AES_KW as JWK_AES_KW,
125
+ jwk_JWK_Asymmetric_Algorithm as JWK_Asymmetric_Algorithm,
126
+ jwk_JWK_EC as JWK_EC,
127
+ jwk_JWK_ECDH_ES as JWK_ECDH_ES,
128
+ jwk_JWK_ECDSA as JWK_ECDSA,
129
+ jwk_JWK_EC_Private as JWK_EC_Private,
130
+ jwk_JWK_EC_Public as JWK_EC_Public,
131
+ jwk_JWK_HMAC as JWK_HMAC,
132
+ jwk_JWK_PBES2 as JWK_PBES2,
133
+ jwk_JWK_RSA as JWK_RSA,
134
+ jwk_JWK_RSA_ENC as JWK_RSA_ENC,
135
+ jwk_JWK_RSA_PSS as JWK_RSA_PSS,
136
+ jwk_JWK_RSA_Private as JWK_RSA_Private,
137
+ jwk_JWK_RSA_Public as JWK_RSA_Public,
138
+ jwk_JWK_RSA_SIGN as JWK_RSA_SIGN,
139
+ jwk_JWK_Symmetric_Algorithm as JWK_Symmetric_Algorithm,
140
+ jwk_JWK_oct as JWK_oct,
141
+ jwk_KeyManagementAlgorithm as KeyManagementAlgorithm,
142
+ jwk_UnwrapKeyOptions as UnwrapKeyOptions,
143
+ jwk_WrapKeyOptions as WrapKeyOptions,
144
+ jwk_WrapKeyResult as WrapKeyResult,
145
+ jwk_deriveKeyFromPassword as deriveKeyFromPassword,
146
+ jwk_exportKey as exportKey,
147
+ jwk_generateKey as generateKey,
148
+ jwk_importKey as importKey,
149
+ jwk_unwrapKey as unwrapKey,
150
+ jwk_wrapKey as wrapKey,
151
+ };
152
+ }
153
+
154
+ export { deriveKeyFromPassword as d, exportKey as e, generateKey as g, importKey as i, jwk as j, unwrapKey as u, wrapKey as w };