@mnemom/mnemom 0.17.37 → 0.17.38-next.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 (114) hide show
  1. package/dist/commands/agent-run.d.ts +11 -0
  2. package/dist/commands/agent-run.js +37 -0
  3. package/dist/commands/agent-share-render.d.ts +5 -5
  4. package/dist/commands/agent-share-render.js +9 -7
  5. package/dist/commands/agent-share-test-issuer.d.ts +1 -1
  6. package/dist/commands/agent-share-test-issuer.js +1 -1
  7. package/dist/commands/agent.d.ts +8 -1
  8. package/dist/commands/agent.js +14 -3
  9. package/dist/commands/scan.d.ts +15 -2
  10. package/dist/commands/scan.js +48 -3
  11. package/dist/index.js +1 -1
  12. package/dist/lib/commit-report.d.ts +7 -13
  13. package/dist/lib/commit-report.js +12 -5
  14. package/dist/lib/conduct-receipt.d.ts +8 -3
  15. package/dist/lib/conduct-receipt.js +29 -7
  16. package/dist/lib/experimental.d.ts +1 -1
  17. package/dist/lib/experimental.js +1 -12
  18. package/dist/lib/runner/executor-client.d.ts +5 -0
  19. package/dist/lib/runner/executor-client.js +8 -0
  20. package/dist/lib/runner/job.d.ts +37 -0
  21. package/dist/lib/runner/job.js +74 -1
  22. package/dist/lib/runner/phases.d.ts +7 -2
  23. package/dist/lib/runner/phases.js +7 -2
  24. package/dist/lib/runner/proxy.d.ts +26 -2
  25. package/dist/lib/runner/proxy.js +33 -1
  26. package/dist/lib/runner/record-commit.d.ts +16 -0
  27. package/dist/lib/runner/record-commit.js +7 -0
  28. package/dist/lib/scan/spend/anthropic-usage.d.ts +20 -1
  29. package/dist/lib/scan/spend/anthropic-usage.js +32 -7
  30. package/dist/lib/scan/spend/anthropic-wif.d.ts +42 -0
  31. package/dist/lib/scan/spend/anthropic-wif.js +153 -0
  32. package/package.json +2 -5
  33. package/node_modules/@mnemom/aip-verifier/LICENSE +0 -190
  34. package/node_modules/@mnemom/aip-verifier/dist/claim-lint-CVITIRcU.d.cts +0 -478
  35. package/node_modules/@mnemom/aip-verifier/dist/claim-lint-CVITIRcU.d.ts +0 -478
  36. package/node_modules/@mnemom/aip-verifier/dist/index.cjs +0 -971
  37. package/node_modules/@mnemom/aip-verifier/dist/index.cjs.map +0 -1
  38. package/node_modules/@mnemom/aip-verifier/dist/index.d.cts +0 -330
  39. package/node_modules/@mnemom/aip-verifier/dist/index.d.ts +0 -330
  40. package/node_modules/@mnemom/aip-verifier/dist/index.js +0 -933
  41. package/node_modules/@mnemom/aip-verifier/dist/index.js.map +0 -1
  42. package/node_modules/@mnemom/aip-verifier/dist/render-lines.cjs +0 -879
  43. package/node_modules/@mnemom/aip-verifier/dist/render-lines.cjs.map +0 -1
  44. package/node_modules/@mnemom/aip-verifier/dist/render-lines.d.cts +0 -96
  45. package/node_modules/@mnemom/aip-verifier/dist/render-lines.d.ts +0 -96
  46. package/node_modules/@mnemom/aip-verifier/dist/render-lines.js +0 -875
  47. package/node_modules/@mnemom/aip-verifier/dist/render-lines.js.map +0 -1
  48. package/node_modules/@mnemom/aip-verifier/package.json +0 -86
  49. package/node_modules/@noble/ed25519/LICENSE +0 -21
  50. package/node_modules/@noble/ed25519/README.md +0 -371
  51. package/node_modules/@noble/ed25519/index.d.ts +0 -391
  52. package/node_modules/@noble/ed25519/index.js +0 -933
  53. package/node_modules/@noble/ed25519/index.ts +0 -1134
  54. package/node_modules/@noble/ed25519/package.json +0 -54
  55. package/node_modules/@noble/hashes/LICENSE +0 -21
  56. package/node_modules/@noble/hashes/README.md +0 -478
  57. package/node_modules/@noble/hashes/_blake.d.ts +0 -20
  58. package/node_modules/@noble/hashes/_blake.js +0 -59
  59. package/node_modules/@noble/hashes/_md.d.ts +0 -95
  60. package/node_modules/@noble/hashes/_md.js +0 -209
  61. package/node_modules/@noble/hashes/_u64.d.ts +0 -36
  62. package/node_modules/@noble/hashes/_u64.js +0 -77
  63. package/node_modules/@noble/hashes/argon2.d.ts +0 -148
  64. package/node_modules/@noble/hashes/argon2.js +0 -577
  65. package/node_modules/@noble/hashes/blake1.d.ts +0 -162
  66. package/node_modules/@noble/hashes/blake1.js +0 -630
  67. package/node_modules/@noble/hashes/blake2.d.ts +0 -203
  68. package/node_modules/@noble/hashes/blake2.js +0 -573
  69. package/node_modules/@noble/hashes/blake3.d.ts +0 -68
  70. package/node_modules/@noble/hashes/blake3.js +0 -310
  71. package/node_modules/@noble/hashes/eskdf.d.ts +0 -91
  72. package/node_modules/@noble/hashes/eskdf.js +0 -225
  73. package/node_modules/@noble/hashes/hkdf.d.ts +0 -70
  74. package/node_modules/@noble/hashes/hkdf.js +0 -152
  75. package/node_modules/@noble/hashes/hmac.d.ts +0 -50
  76. package/node_modules/@noble/hashes/hmac.js +0 -93
  77. package/node_modules/@noble/hashes/index.d.ts +0 -1
  78. package/node_modules/@noble/hashes/index.js +0 -34
  79. package/node_modules/@noble/hashes/legacy.d.ts +0 -107
  80. package/node_modules/@noble/hashes/legacy.js +0 -348
  81. package/node_modules/@noble/hashes/package.json +0 -91
  82. package/node_modules/@noble/hashes/pbkdf2.d.ts +0 -66
  83. package/node_modules/@noble/hashes/pbkdf2.js +0 -176
  84. package/node_modules/@noble/hashes/scrypt.d.ts +0 -98
  85. package/node_modules/@noble/hashes/scrypt.js +0 -304
  86. package/node_modules/@noble/hashes/sha2.d.ts +0 -176
  87. package/node_modules/@noble/hashes/sha2.js +0 -446
  88. package/node_modules/@noble/hashes/sha3-addons.d.ts +0 -533
  89. package/node_modules/@noble/hashes/sha3-addons.js +0 -973
  90. package/node_modules/@noble/hashes/sha3.d.ts +0 -208
  91. package/node_modules/@noble/hashes/sha3.js +0 -475
  92. package/node_modules/@noble/hashes/src/_blake.ts +0 -67
  93. package/node_modules/@noble/hashes/src/_md.ts +0 -227
  94. package/node_modules/@noble/hashes/src/_u64.ts +0 -111
  95. package/node_modules/@noble/hashes/src/argon2.ts +0 -693
  96. package/node_modules/@noble/hashes/src/blake1.ts +0 -677
  97. package/node_modules/@noble/hashes/src/blake2.ts +0 -648
  98. package/node_modules/@noble/hashes/src/blake3.ts +0 -344
  99. package/node_modules/@noble/hashes/src/eskdf.ts +0 -265
  100. package/node_modules/@noble/hashes/src/hkdf.ts +0 -161
  101. package/node_modules/@noble/hashes/src/hmac.ts +0 -130
  102. package/node_modules/@noble/hashes/src/index.ts +0 -33
  103. package/node_modules/@noble/hashes/src/legacy.ts +0 -361
  104. package/node_modules/@noble/hashes/src/pbkdf2.ts +0 -219
  105. package/node_modules/@noble/hashes/src/scrypt.ts +0 -375
  106. package/node_modules/@noble/hashes/src/sha2.ts +0 -486
  107. package/node_modules/@noble/hashes/src/sha3-addons.ts +0 -1157
  108. package/node_modules/@noble/hashes/src/sha3.ts +0 -525
  109. package/node_modules/@noble/hashes/src/utils.ts +0 -978
  110. package/node_modules/@noble/hashes/src/webcrypto.ts +0 -283
  111. package/node_modules/@noble/hashes/utils.d.ts +0 -560
  112. package/node_modules/@noble/hashes/utils.js +0 -687
  113. package/node_modules/@noble/hashes/webcrypto.d.ts +0 -121
  114. package/node_modules/@noble/hashes/webcrypto.js +0 -228
@@ -1,121 +0,0 @@
1
- import { type Pbkdf2Opt } from './pbkdf2.ts';
2
- import { type KDFInput, type TArg, type TRet } from './utils.ts';
3
- /** Callable WebCrypto hash function descriptor. */
4
- export type WebHash = {
5
- /**
6
- * Hashes one message with the selected WebCrypto digest.
7
- * @param msg - message bytes to hash
8
- * @returns Promise resolving to digest bytes.
9
- */
10
- (msg: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
11
- /** WebCrypto algorithm name passed to `crypto.subtle`. */
12
- webCryptoName: string;
13
- /** Digest size in bytes. */
14
- outputLen: number;
15
- /** Input block size in bytes. */
16
- blockLen: number;
17
- };
18
- /** WebCrypto SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
19
- /**
20
- * WebCrypto SHA2-256 hash function from RFC 6234.
21
- * @param msg - message bytes to hash
22
- * @returns Promise resolving to digest bytes.
23
- * @example
24
- * Hash a message with WebCrypto SHA2-256.
25
- * ```ts
26
- * await sha256(new Uint8Array([97, 98, 99]));
27
- * ```
28
- */
29
- export declare const sha256: TRet<WebHash>;
30
- /**
31
- * WebCrypto SHA2-384 hash function from RFC 6234.
32
- * @param msg - message bytes to hash
33
- * @returns Promise resolving to digest bytes.
34
- * @example
35
- * Hash a message with WebCrypto SHA2-384.
36
- * ```ts
37
- * await sha384(new Uint8Array([97, 98, 99]));
38
- * ```
39
- */
40
- export declare const sha384: TRet<WebHash>;
41
- /**
42
- * WebCrypto SHA2-512 hash function from RFC 6234.
43
- * @param msg - message bytes to hash
44
- * @returns Promise resolving to digest bytes.
45
- * @example
46
- * Hash a message with WebCrypto SHA2-512.
47
- * ```ts
48
- * await sha512(new Uint8Array([97, 98, 99]));
49
- * ```
50
- */
51
- export declare const sha512: TRet<WebHash>;
52
- /** WebCrypto HMAC function type. See {@link hmac} for usage. */
53
- type WebHmacFn = {
54
- (hash: TArg<WebHash>, key: TArg<Uint8Array>, message: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
55
- create(hash: TArg<WebHash>, key: TArg<Uint8Array>): any;
56
- };
57
- /**
58
- * WebCrypto HMAC: RFC2104 message authentication code.
59
- * @param hash - function that would be used e.g. sha256. Webcrypto version.
60
- * @param key - authentication key bytes
61
- * @param message - message bytes to authenticate
62
- * @returns Promise resolving to authentication tag bytes.
63
- * `.create()` exists only to mirror the synchronous API surface
64
- * and always throws `not implemented`.
65
- * @example
66
- * Compute an RFC 2104 HMAC with WebCrypto.
67
- * ```ts
68
- * import { hmac, sha256 } from '@noble/hashes/webcrypto.js';
69
- * const key = new Uint8Array([1, 2, 3]);
70
- * const message = new Uint8Array([4, 5, 6]);
71
- * const mac = await hmac(sha256, key, message);
72
- * ```
73
- */
74
- export declare const hmac: TRet<WebHmacFn>;
75
- /**
76
- * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
77
- * Combines hkdf_extract + hkdf_expand in one step
78
- * @param hash - hash function that would be used (e.g. sha256). Webcrypto version.
79
- * @param ikm - input keying material, the initial key
80
- * @param salt - optional salt value (a non-secret random value)
81
- * @param info - optional context and application specific information bytes
82
- * @param length - length of output keying material in bytes.
83
- * RFC 5869 §2.3 allows `0..255*HashLen`, so `0` requests an empty OKM.
84
- * @returns Promise resolving to derived key bytes.
85
- * The RFC `L <= 255 * HashLen` bound is enforced before calling WebCrypto.
86
- * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
87
- * @example
88
- * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
89
- * ```ts
90
- * import { hkdf, sha256 } from '@noble/hashes/webcrypto.js';
91
- * import { randomBytes, utf8ToBytes } from '@noble/hashes/utils.js';
92
- * const inputKey = randomBytes(32);
93
- * const salt = randomBytes(32);
94
- * const info = utf8ToBytes('application-key');
95
- * const okm = await hkdf(sha256, inputKey, salt, info, 32);
96
- * ```
97
- */
98
- export declare function hkdf(hash: TArg<WebHash>, ikm: TArg<Uint8Array>, salt: TArg<Uint8Array | undefined>, info: TArg<Uint8Array | undefined>, length: number): Promise<TRet<Uint8Array>>;
99
- /**
100
- * WebCrypto PBKDF2-HMAC: RFC 8018 key derivation function.
101
- * @param hash - hash function that would be used e.g. sha256. Webcrypto version.
102
- * @param password - password from which a derived key is generated; string
103
- * inputs are normalized through `kdfInputToBytes()`, i.e. UTF-8
104
- * @param salt - cryptographic salt; string inputs are normalized through
105
- * `kdfInputToBytes()`, i.e. UTF-8
106
- * @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
107
- * must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
108
- * @returns Promise resolving to derived key bytes.
109
- * Positive-iteration enforcement is currently delegated to backend
110
- * `deriveBits()` rejection (for example `c = 0`), not a dedicated
111
- * library-side guard. Values above the signed 32-bit backend range are rejected locally.
112
- * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
113
- * @example
114
- * WebCrypto PBKDF2-HMAC: RFC 2898 key derivation function.
115
- * ```ts
116
- * import { pbkdf2, sha256 } from '@noble/hashes/webcrypto.js';
117
- * const key = await pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
118
- * ```
119
- */
120
- export declare function pbkdf2(hash: TArg<WebHash>, password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: Pbkdf2Opt): Promise<TRet<Uint8Array>>;
121
- export {};
@@ -1,228 +0,0 @@
1
- import {} from "./pbkdf2.js";
2
- import { abytes, ahash, anumber, checkOpts, clean, copyBytes, kdfInputToBytes, } from "./utils.js";
3
- function _subtle() {
4
- const cr = typeof globalThis === 'object' ? globalThis.crypto : null;
5
- const sb = cr?.subtle;
6
- if (typeof sb === 'object' && sb != null)
7
- return sb;
8
- throw new Error('crypto.subtle must be defined');
9
- }
10
- function createWebHash(name, blockLen, outputLen) {
11
- const hashC = async (msg) => {
12
- abytes(msg);
13
- const crypto = _subtle();
14
- return new Uint8Array(await crypto.digest(name, msg));
15
- };
16
- hashC.webCryptoName = name; // make sure it won't interfere with function name
17
- hashC.outputLen = outputLen;
18
- hashC.blockLen = blockLen;
19
- hashC.create = () => {
20
- // Present only so this async wrapper satisfies the shared
21
- // hash-wrapper shape checked by `ahashWeb()`.
22
- throw new Error('not implemented');
23
- };
24
- // Later WebCrypto HMAC/HKDF/PBKDF2 calls read descriptor metadata directly, so freezing prevents
25
- // callers from retargeting a `sha256` wrapper into a different backend digest by mutation.
26
- return Object.freeze(hashC);
27
- }
28
- function ahashWeb(hash) {
29
- ahash(hash);
30
- const name = hash.webCryptoName;
31
- if (typeof name !== 'string')
32
- throw new Error('non-web hash');
33
- return name;
34
- }
35
- /** WebCrypto SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
36
- // export const sha1: WebHash = createHash('SHA-1', 64, 20);
37
- /**
38
- * WebCrypto SHA2-256 hash function from RFC 6234.
39
- * @param msg - message bytes to hash
40
- * @returns Promise resolving to digest bytes.
41
- * @example
42
- * Hash a message with WebCrypto SHA2-256.
43
- * ```ts
44
- * await sha256(new Uint8Array([97, 98, 99]));
45
- * ```
46
- */
47
- export const sha256 = /* @__PURE__ */ createWebHash('SHA-256', 64, 32);
48
- /**
49
- * WebCrypto SHA2-384 hash function from RFC 6234.
50
- * @param msg - message bytes to hash
51
- * @returns Promise resolving to digest bytes.
52
- * @example
53
- * Hash a message with WebCrypto SHA2-384.
54
- * ```ts
55
- * await sha384(new Uint8Array([97, 98, 99]));
56
- * ```
57
- */
58
- export const sha384 = /* @__PURE__ */ createWebHash('SHA-384', 128, 48);
59
- /**
60
- * WebCrypto SHA2-512 hash function from RFC 6234.
61
- * @param msg - message bytes to hash
62
- * @returns Promise resolving to digest bytes.
63
- * @example
64
- * Hash a message with WebCrypto SHA2-512.
65
- * ```ts
66
- * await sha512(new Uint8Array([97, 98, 99]));
67
- * ```
68
- */
69
- export const sha512 = /* @__PURE__ */ createWebHash('SHA-512', 128, 64);
70
- /**
71
- * WebCrypto HMAC: RFC2104 message authentication code.
72
- * @param hash - function that would be used e.g. sha256. Webcrypto version.
73
- * @param key - authentication key bytes
74
- * @param message - message bytes to authenticate
75
- * @returns Promise resolving to authentication tag bytes.
76
- * `.create()` exists only to mirror the synchronous API surface
77
- * and always throws `not implemented`.
78
- * @example
79
- * Compute an RFC 2104 HMAC with WebCrypto.
80
- * ```ts
81
- * import { hmac, sha256 } from '@noble/hashes/webcrypto.js';
82
- * const key = new Uint8Array([1, 2, 3]);
83
- * const message = new Uint8Array([4, 5, 6]);
84
- * const mac = await hmac(sha256, key, message);
85
- * ```
86
- */
87
- export const hmac = /* @__PURE__ */ (() => {
88
- const hmac_ = async (hash, key, message) => {
89
- const crypto = _subtle();
90
- abytes(key, undefined, 'key');
91
- abytes(message, undefined, 'message');
92
- const hashName = ahashWeb(hash);
93
- // importKey() snapshots key synchronously, but message is not passed to sign() until after
94
- // importKey() resolves. Keep the wrapper's inputs stable across that await.
95
- const _message = copyBytes(message);
96
- try {
97
- // WebCrypto keys can't be zeroized
98
- // prettier-ignore
99
- const wkey = await crypto.importKey('raw', key, { name: 'HMAC', hash: hashName }, false, ['sign']);
100
- return new Uint8Array(await crypto.sign('HMAC', wkey, _message));
101
- }
102
- finally {
103
- clean(_message);
104
- }
105
- };
106
- hmac_.create = (_hash, _key) => {
107
- throw new Error('not implemented');
108
- };
109
- return hmac_;
110
- })();
111
- /**
112
- * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
113
- * Combines hkdf_extract + hkdf_expand in one step
114
- * @param hash - hash function that would be used (e.g. sha256). Webcrypto version.
115
- * @param ikm - input keying material, the initial key
116
- * @param salt - optional salt value (a non-secret random value)
117
- * @param info - optional context and application specific information bytes
118
- * @param length - length of output keying material in bytes.
119
- * RFC 5869 §2.3 allows `0..255*HashLen`, so `0` requests an empty OKM.
120
- * @returns Promise resolving to derived key bytes.
121
- * The RFC `L <= 255 * HashLen` bound is enforced before calling WebCrypto.
122
- * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
123
- * @example
124
- * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
125
- * ```ts
126
- * import { hkdf, sha256 } from '@noble/hashes/webcrypto.js';
127
- * import { randomBytes, utf8ToBytes } from '@noble/hashes/utils.js';
128
- * const inputKey = randomBytes(32);
129
- * const salt = randomBytes(32);
130
- * const info = utf8ToBytes('application-key');
131
- * const okm = await hkdf(sha256, inputKey, salt, info, 32);
132
- * ```
133
- */
134
- export async function hkdf(hash, ikm, salt, info, length) {
135
- const crypto = _subtle();
136
- const hashName = ahashWeb(hash);
137
- const hashOutputLen = hash.outputLen;
138
- abytes(ikm, undefined, 'ikm');
139
- anumber(length, 'length');
140
- if (length > 255 * hashOutputLen)
141
- throw new Error('Length must be <= 255*HashLen');
142
- if (salt !== undefined)
143
- abytes(salt, undefined, 'salt');
144
- if (info !== undefined)
145
- abytes(info, undefined, 'info');
146
- // salt and info reach deriveBits() only after importKey() resolves, so snapshot them now.
147
- const _salt = salt === undefined ? new Uint8Array(0) : copyBytes(salt);
148
- const _info = info === undefined ? new Uint8Array(0) : copyBytes(info);
149
- try {
150
- const wkey = await crypto.importKey('raw', ikm, 'HKDF', false, ['deriveBits']);
151
- const opts = { name: 'HKDF', hash: hashName, salt: _salt, info: _info };
152
- const out = new Uint8Array(await crypto.deriveBits(opts, wkey, 8 * length));
153
- if (out.length !== length) {
154
- clean(out);
155
- throw new Error('WebCrypto returned an invalid derived key length');
156
- }
157
- return out;
158
- }
159
- finally {
160
- clean(_salt, _info);
161
- }
162
- }
163
- /**
164
- * WebCrypto PBKDF2-HMAC: RFC 8018 key derivation function.
165
- * @param hash - hash function that would be used e.g. sha256. Webcrypto version.
166
- * @param password - password from which a derived key is generated; string
167
- * inputs are normalized through `kdfInputToBytes()`, i.e. UTF-8
168
- * @param salt - cryptographic salt; string inputs are normalized through
169
- * `kdfInputToBytes()`, i.e. UTF-8
170
- * @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
171
- * must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
172
- * @returns Promise resolving to derived key bytes.
173
- * Positive-iteration enforcement is currently delegated to backend
174
- * `deriveBits()` rejection (for example `c = 0`), not a dedicated
175
- * library-side guard. Values above the signed 32-bit backend range are rejected locally.
176
- * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
177
- * @example
178
- * WebCrypto PBKDF2-HMAC: RFC 2898 key derivation function.
179
- * ```ts
180
- * import { pbkdf2, sha256 } from '@noble/hashes/webcrypto.js';
181
- * const key = await pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
182
- * ```
183
- */
184
- export async function pbkdf2(hash, password, salt, opts) {
185
- const crypto = _subtle();
186
- const hashName = ahashWeb(hash);
187
- const _opts = checkOpts({ dkLen: 32 }, opts);
188
- const { c, dkLen } = _opts;
189
- anumber(c, 'c');
190
- anumber(dkLen, 'dkLen');
191
- // Node's native WebCrypto PBKDF2 binding accepts only a signed 32-bit iteration count and aborts
192
- // the process on larger values instead of returning a rejected promise.
193
- if (c > 0x7fffffff)
194
- throw new Error('"c" exceeds WebCrypto backend limit');
195
- // RFC 8018 §5.2 defines dkLen as a positive integer.
196
- if (dkLen < 1)
197
- throw new Error('"dkLen" must be >= 1');
198
- // SubtleCrypto.deriveBits() accepts an unsigned-long bit count. Byte lengths at or above
199
- // 2^29 would wrap after multiplication by eight instead of requesting the intended length.
200
- if (dkLen >= 2 ** 29)
201
- throw new Error('derived key too long');
202
- const _password = kdfInputToBytes(password, 'password');
203
- try {
204
- const saltBytes = kdfInputToBytes(salt, 'salt');
205
- // String conversion already returns an owned array. Caller-owned byte salts need a snapshot
206
- // because deriveBits() does not receive them until after importKey() resolves.
207
- const _salt = typeof salt === 'string' ? saltBytes : copyBytes(saltBytes);
208
- try {
209
- const key = await crypto.importKey('raw', _password, 'PBKDF2', false, [
210
- 'deriveBits',
211
- ]);
212
- const deriveOpts = { name: 'PBKDF2', salt: _salt, iterations: c, hash: hashName };
213
- const out = new Uint8Array(await crypto.deriveBits(deriveOpts, key, 8 * dkLen));
214
- if (out.length !== dkLen) {
215
- clean(out);
216
- throw new Error('WebCrypto returned an invalid derived key length');
217
- }
218
- return out;
219
- }
220
- finally {
221
- clean(_salt);
222
- }
223
- }
224
- finally {
225
- if (typeof password === 'string')
226
- clean(_password);
227
- }
228
- }