@kasperpeulen/vitest-plugin-nextjs 0.0.0-stage → 0.1.0-alpha.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.
- package/README.md +249 -2
- package/dist/_virtual/_rolldown/runtime.js +14 -0
- package/dist/action-ids.d.ts +18 -0
- package/dist/action-ids.js +44 -0
- package/dist/adapter.d.ts +8 -0
- package/dist/adapter.js +12 -0
- package/dist/app-requests.d.ts +63 -0
- package/dist/app-requests.js +253 -0
- package/dist/browser-context.d.ts +24 -0
- package/dist/browser-context.js +157 -0
- package/dist/build-output.d.ts +68 -0
- package/dist/build-output.js +216 -0
- package/dist/build.d.ts +26 -0
- package/dist/build.js +39 -0
- package/dist/built-files.d.ts +9 -0
- package/dist/built-files.js +22 -0
- package/dist/cache.d.ts +44 -0
- package/dist/cache.js +99 -0
- package/dist/client-components.d.ts +3 -0
- package/dist/client-components.js +4 -0
- package/dist/client-file-key.d.ts +5 -0
- package/dist/client-file-key.js +5 -0
- package/dist/client-files.d.ts +83 -0
- package/dist/client-files.js +186 -0
- package/dist/client-graph.d.ts +94 -0
- package/dist/client-graph.js +266 -0
- package/dist/client-modules.d.ts +10 -0
- package/dist/client-modules.js +43 -0
- package/dist/client.d.ts +42 -0
- package/dist/client.js +263 -0
- package/dist/commonjs.d.ts +44 -0
- package/dist/commonjs.js +111 -0
- package/dist/compat/browser/client.d.ts +19 -0
- package/dist/compat/browser/client.js +13 -0
- package/dist/compat/build.d.ts +145 -0
- package/dist/compat/build.js +149 -0
- package/dist/compat/checks.d.ts +16 -0
- package/dist/compat/checks.js +148 -0
- package/dist/compat/patches.d.ts +13 -0
- package/dist/compat/patches.js +30 -0
- package/dist/compat/paths.d.ts +74 -0
- package/dist/compat/paths.js +160 -0
- package/dist/compat/rsc/handlers.d.ts +2 -0
- package/dist/compat/rsc/handlers.js +2 -0
- package/dist/compat/rsc/prerender.d.ts +10 -0
- package/dist/compat/rsc/prerender.js +9 -0
- package/dist/compat/server/headers.d.ts +3 -0
- package/dist/compat/server/headers.js +3 -0
- package/dist/compat/ssr/build-output.d.ts +4 -0
- package/dist/compat/ssr/build-output.js +3 -0
- package/dist/compat/ssr/cache.d.ts +6 -0
- package/dist/compat/ssr/cache.js +6 -0
- package/dist/compat/ssr/handler.d.ts +8 -0
- package/dist/compat/ssr/handler.js +8 -0
- package/dist/compat/ssr/node-server.d.ts +5 -0
- package/dist/compat/ssr/node-server.js +4 -0
- package/dist/compat/ssr/render-errors.d.ts +4 -0
- package/dist/compat/ssr/render-errors.js +4 -0
- package/dist/compat/ssr/senders.d.ts +6 -0
- package/dist/compat/ssr/senders.js +6 -0
- package/dist/compat/types.d.ts +8 -0
- package/dist/compat/types.js +1 -0
- package/dist/compile.d.ts +31 -0
- package/dist/compile.js +140 -0
- package/dist/disk.d.ts +16 -0
- package/dist/disk.js +13 -0
- package/dist/document-loads.d.ts +15 -0
- package/dist/document-loads.js +53 -0
- package/dist/document.d.ts +79 -0
- package/dist/document.js +328 -0
- package/dist/downloads.d.ts +15 -0
- package/dist/downloads.js +71 -0
- package/dist/esm-require.d.ts +11 -0
- package/dist/esm-require.js +32 -0
- package/dist/fast-crypto/index.js +1093 -0
- package/dist/fast-crypto.d.ts +20 -0
- package/dist/fast-crypto.js +151 -0
- package/dist/flight.d.ts +19 -0
- package/dist/flight.js +42 -0
- package/dist/form-submission.d.ts +15 -0
- package/dist/form-submission.js +27 -0
- package/dist/from-page.d.ts +5 -0
- package/dist/from-page.js +44 -0
- package/dist/globals.d.ts +1 -0
- package/dist/globals.js +197 -0
- package/dist/host-document.d.ts +26 -0
- package/dist/host-document.js +34 -0
- package/dist/index.d.ts +280 -0
- package/dist/index.js +281 -0
- package/dist/internal.d.ts +2 -0
- package/dist/internal.js +3 -0
- package/dist/leftovers.d.ts +11 -0
- package/dist/leftovers.js +55 -0
- package/dist/mode.d.ts +169 -0
- package/dist/mode.js +31 -0
- package/dist/node/LICENSES.md +3540 -0
- package/dist/node/assert/strict.js +29 -0
- package/dist/node/assert.js +3044 -0
- package/dist/node/buffer.js +1499 -0
- package/dist/node/child_process.js +45 -0
- package/dist/node/cluster.js +85 -0
- package/dist/node/console.js +96 -0
- package/dist/node/constants.js +495 -0
- package/dist/node/crypto.js +1388 -0
- package/dist/node/dgram.js +78 -0
- package/dist/node/diagnostics_channel.js +122 -0
- package/dist/node/dns/promises.js +129 -0
- package/dist/node/dns.js +137 -0
- package/dist/node/domain.js +43 -0
- package/dist/node/events.js +1155 -0
- package/dist/node/fs/promises.js +40 -0
- package/dist/node/fs.js +10375 -0
- package/dist/node/http.js +326 -0
- package/dist/node/http2.js +305 -0
- package/dist/node/https.js +57 -0
- package/dist/node/inspector/promises.js +66 -0
- package/dist/node/inspector.js +76 -0
- package/dist/node/module.js +205 -0
- package/dist/node/net.js +392 -0
- package/dist/node/os.js +259 -0
- package/dist/node/path/posix.js +25 -0
- package/dist/node/path/win32.js +25 -0
- package/dist/node/path.js +384 -0
- package/dist/node/perf_hooks.js +313 -0
- package/dist/node/process.js +452 -0
- package/dist/node/punycode.js +178 -0
- package/dist/node/querystring.js +934 -0
- package/dist/node/readline/promises.js +71 -0
- package/dist/node/readline.js +56 -0
- package/dist/node/repl.js +44 -0
- package/dist/node/sqlite.js +22 -0
- package/dist/node/stream/consumers.js +47 -0
- package/dist/node/stream/promises.js +6 -0
- package/dist/node/stream/web.js +52 -0
- package/dist/node/stream.js +4323 -0
- package/dist/node/string_decoder.js +276 -0
- package/dist/node/sys.js +6 -0
- package/dist/node/timers/promises.js +71 -0
- package/dist/node/timers.js +133 -0
- package/dist/node/tls.js +122 -0
- package/dist/node/trace_events.js +23 -0
- package/dist/node/tty.js +69 -0
- package/dist/node/url.js +1426 -0
- package/dist/node/util/types.js +1456 -0
- package/dist/node/util.js +4807 -0
- package/dist/node/v8.js +160 -0
- package/dist/node/vm.js +88 -0
- package/dist/node/wasi.js +16 -0
- package/dist/node/worker_threads.js +123 -0
- package/dist/node/zlib.js +1396 -0
- package/dist/node-builtins.d.ts +40 -0
- package/dist/node-builtins.js +74 -0
- package/dist/node-on-page.d.ts +46 -0
- package/dist/node-on-page.js +101 -0
- package/dist/node-platform.d.ts +36 -0
- package/dist/node-platform.js +152 -0
- package/dist/node-rendered.d.ts +21 -0
- package/dist/node-rendered.js +26 -0
- package/dist/node-server.d.ts +46 -0
- package/dist/node-server.js +193 -0
- package/dist/optimizer-cache-key.d.ts +26 -0
- package/dist/optimizer-cache-key.js +73 -0
- package/dist/own-package.d.ts +1 -0
- package/dist/own-package.js +6 -0
- package/dist/page-store.d.ts +33 -0
- package/dist/page-store.js +80 -0
- package/dist/page-timers.d.ts +22 -0
- package/dist/page-timers.js +47 -0
- package/dist/page.d.ts +112 -0
- package/dist/page.js +549 -0
- package/dist/paths.d.ts +13 -0
- package/dist/paths.js +47 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.js +995 -0
- package/dist/prerender-verdict.d.ts +8 -0
- package/dist/prerender-verdict.js +67 -0
- package/dist/project/context.d.ts +546 -0
- package/dist/project/context.js +111 -0
- package/dist/project/entries.d.ts +42 -0
- package/dist/project/entries.js +189 -0
- package/dist/project/env-worker.d.ts +1 -0
- package/dist/project/env-worker.js +9 -0
- package/dist/project/env.d.ts +36 -0
- package/dist/project/env.js +82 -0
- package/dist/project/layers.d.ts +31 -0
- package/dist/project/layers.js +150 -0
- package/dist/project/loaders.d.ts +71 -0
- package/dist/project/loaders.js +300 -0
- package/dist/project/metadata.d.ts +8 -0
- package/dist/project/metadata.js +34 -0
- package/dist/project/module-exports.d.ts +29 -0
- package/dist/project/module-exports.js +77 -0
- package/dist/project/routes.d.ts +19 -0
- package/dist/project/routes.js +92 -0
- package/dist/project/routing.d.ts +46 -0
- package/dist/project/routing.js +280 -0
- package/dist/project/transform.d.ts +8 -0
- package/dist/project/transform.js +110 -0
- package/dist/project/webpack.d.ts +46 -0
- package/dist/project/webpack.js +160 -0
- package/dist/project.d.ts +264 -0
- package/dist/project.js +58 -0
- package/dist/registry-global.d.ts +5 -0
- package/dist/registry-global.js +5 -0
- package/dist/registry.d.ts +241 -0
- package/dist/registry.js +30 -0
- package/dist/route-chunks.d.ts +13 -0
- package/dist/route-chunks.js +32 -0
- package/dist/route-lookup.d.ts +34 -0
- package/dist/route-lookup.js +207 -0
- package/dist/routes-ahead-shared.d.ts +17 -0
- package/dist/routes-ahead-shared.js +9 -0
- package/dist/routes-ahead.d.ts +10 -0
- package/dist/routes-ahead.js +54 -0
- package/dist/rsc.d.ts +19 -0
- package/dist/rsc.js +171 -0
- package/dist/server-action.d.ts +9 -0
- package/dist/server-action.js +15 -0
- package/dist/server-code.d.ts +176 -0
- package/dist/server-code.js +357 -0
- package/dist/server-in-front.d.ts +37 -0
- package/dist/server-in-front.js +90 -0
- package/dist/server-layers.d.ts +22 -0
- package/dist/server-layers.js +33 -0
- package/dist/setup-browser.d.ts +1 -0
- package/dist/setup-browser.js +8 -0
- package/dist/setup-node.d.ts +1 -0
- package/dist/setup-node.js +6 -0
- package/dist/setup.d.ts +8 -0
- package/dist/setup.js +62 -0
- package/dist/speed-features.d.ts +13 -0
- package/dist/speed-features.js +95 -0
- package/dist/ssr.d.ts +70 -0
- package/dist/ssr.js +480 -0
- package/dist/static-build.d.ts +36 -0
- package/dist/static-build.js +92 -0
- package/dist/storage.d.ts +31 -0
- package/dist/storage.js +94 -0
- package/dist/styles-shared.d.ts +137 -0
- package/dist/styles-shared.js +124 -0
- package/dist/styles.d.ts +102 -0
- package/dist/styles.js +730 -0
- package/dist/stylesheet-versions.d.ts +17 -0
- package/dist/stylesheet-versions.js +43 -0
- package/dist/tester.html +10 -0
- package/dist/use-cache.d.ts +15 -0
- package/dist/use-cache.js +93 -0
- package/dist/vitest-version.d.ts +6 -0
- package/dist/vitest-version.js +12 -0
- package/package.json +125 -3
|
@@ -0,0 +1,1093 @@
|
|
|
1
|
+
//#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/_u64.js
|
|
2
|
+
const fromNumH = (n) => n / 2 ** 32 | 0;
|
|
3
|
+
const fromNumL = (n) => n >>> 0;
|
|
4
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
5
|
+
const h = fromNumH(n);
|
|
6
|
+
const l = fromNumL(n);
|
|
7
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
8
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/utils.js
|
|
12
|
+
/**
|
|
13
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
14
|
+
* @param a - value to test
|
|
15
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
16
|
+
* @example
|
|
17
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
18
|
+
* ```ts
|
|
19
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function isBytes(a) {
|
|
23
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
24
|
+
}
|
|
25
|
+
const atitle = (title) => title ? `"${title}" ` : "";
|
|
26
|
+
/**
|
|
27
|
+
* Asserts something is a non-negative integer.
|
|
28
|
+
* @param n - number to validate
|
|
29
|
+
* @param title - label included in thrown errors
|
|
30
|
+
* @returns The validated number.
|
|
31
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
32
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
33
|
+
* @example
|
|
34
|
+
* Validate a non-negative integer option.
|
|
35
|
+
* ```ts
|
|
36
|
+
* anumber(32, 'length');
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function anumber(n, title = "") {
|
|
40
|
+
if (typeof n !== "number") throw new TypeError(atitle(title) + "expected number, got " + typeof n);
|
|
41
|
+
if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(atitle(title) + "expected integer >= 0, got " + n);
|
|
42
|
+
return n;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Asserts something is Uint8Array.
|
|
46
|
+
* @param value - value to validate
|
|
47
|
+
* @param length - optional exact length constraint
|
|
48
|
+
* @param title - label included in thrown errors
|
|
49
|
+
* @returns The validated byte array.
|
|
50
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
51
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
52
|
+
* @example
|
|
53
|
+
* Validate that a value is a byte array.
|
|
54
|
+
* ```ts
|
|
55
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function abytes(value, length, title = "") {
|
|
59
|
+
if (isBytes(value) && (length === void 0 || value.length === length)) return value;
|
|
60
|
+
if (length !== void 0) anumber(length, "length");
|
|
61
|
+
const bytes = isBytes(value);
|
|
62
|
+
const ofLen = length !== void 0 ? ` of length ${length}` : "";
|
|
63
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
64
|
+
const message = atitle(title) + "expected Uint8Array" + ofLen + ", got " + got;
|
|
65
|
+
if (!bytes) throw new TypeError(message);
|
|
66
|
+
throw new RangeError(message);
|
|
67
|
+
}
|
|
68
|
+
const aobject = (value, label) => {
|
|
69
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
|
|
70
|
+
};
|
|
71
|
+
const aopts = (value, label) => {
|
|
72
|
+
aobject(value, label);
|
|
73
|
+
const proto = Object.getPrototypeOf(value);
|
|
74
|
+
if (proto !== Object.prototype && proto !== null) throw new TypeError(`"${label}" expected plain object`);
|
|
75
|
+
if (Object.hasOwn(value, "__proto__")) throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
79
|
+
* @param instance - hash instance to validate
|
|
80
|
+
* @param checkFinished - whether to reject finalized instances
|
|
81
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
82
|
+
* @example
|
|
83
|
+
* Validate that a hash instance is still usable.
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
86
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
87
|
+
* const hash = sha256.create();
|
|
88
|
+
* aexists(hash);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function aexists(instance, checkFinished = true) {
|
|
92
|
+
if (instance.destroyed) throw new Error("hash was destroyed");
|
|
93
|
+
if (checkFinished && instance.finished) throw new Error("digest() was already called");
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
97
|
+
* @param out - destination buffer
|
|
98
|
+
* @param instance - hash instance providing output length
|
|
99
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
100
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
101
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
102
|
+
* @example
|
|
103
|
+
* Validate a caller-provided digest buffer.
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
106
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
107
|
+
* const hash = sha256.create();
|
|
108
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
function aoutput(out, instance) {
|
|
112
|
+
abytes(out, void 0, "output");
|
|
113
|
+
const min = instance.outputLen;
|
|
114
|
+
if (!(out.length >= min)) throw new RangeError("\"output\" expected length >= " + min);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
118
|
+
* @param arrays - arrays to overwrite with zeros
|
|
119
|
+
* @example
|
|
120
|
+
* Zeroize sensitive buffers in place.
|
|
121
|
+
* ```ts
|
|
122
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
function clean(...arrays) {
|
|
126
|
+
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Creates a DataView for byte-level manipulation.
|
|
130
|
+
* @param arr - source typed array
|
|
131
|
+
* @returns DataView over the same buffer region.
|
|
132
|
+
* @example
|
|
133
|
+
* Create a DataView over an existing buffer.
|
|
134
|
+
* ```ts
|
|
135
|
+
* createView(new Uint8Array(4));
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
function createView(arr) {
|
|
139
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Rotate-right operation for uint32 values.
|
|
143
|
+
* @param word - source word
|
|
144
|
+
* @param shift - shift amount in bits
|
|
145
|
+
* @returns Rotated word.
|
|
146
|
+
* @example
|
|
147
|
+
* Rotate a 32-bit word to the right.
|
|
148
|
+
* ```ts
|
|
149
|
+
* rotr(0x12345678, 8);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
function rotr(word, shift) {
|
|
153
|
+
return word << 32 - shift | word >>> shift;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Merges default options and passed options.
|
|
157
|
+
* @param defaults - base option object
|
|
158
|
+
* @param opts - user overrides
|
|
159
|
+
* @param title - label included in thrown override errors
|
|
160
|
+
* @returns Fresh merged option object with a null prototype.
|
|
161
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
162
|
+
* @example
|
|
163
|
+
* Merge user overrides onto default options.
|
|
164
|
+
* ```ts
|
|
165
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
function checkOpts(defaults, opts, title = "opts") {
|
|
169
|
+
aopts(defaults, "defaults");
|
|
170
|
+
if (opts !== void 0) aopts(opts, title);
|
|
171
|
+
return Object.assign(Object.create(null), defaults, opts);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
175
|
+
* @param hashCons - hash constructor or factory
|
|
176
|
+
* @param info - optional metadata such as DER OID
|
|
177
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
178
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
179
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
180
|
+
* init time.
|
|
181
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
182
|
+
* @example
|
|
183
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
186
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
187
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
188
|
+
* wrapped(new Uint8Array([1]));
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
function createHasher(hashCons, info = {}) {
|
|
192
|
+
if (typeof hashCons !== "function") throw new TypeError("\"hashCons\" expected function, got type=" + typeof hashCons);
|
|
193
|
+
info = checkOpts({}, info, "info");
|
|
194
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
195
|
+
const tmp = hashCons(void 0);
|
|
196
|
+
hashC.outputLen = tmp.outputLen;
|
|
197
|
+
hashC.blockLen = tmp.blockLen;
|
|
198
|
+
hashC.canXOF = tmp.canXOF;
|
|
199
|
+
hashC.create = (opts) => hashCons(opts);
|
|
200
|
+
Object.assign(hashC, info);
|
|
201
|
+
return Object.freeze(hashC);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
205
|
+
* @param suffix - final OID byte for the selected hash.
|
|
206
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
207
|
+
* suffixes are meaningful downstream.
|
|
208
|
+
* @returns Object containing the DER-encoded OID.
|
|
209
|
+
* @example
|
|
210
|
+
* Build OID metadata for a NIST hash.
|
|
211
|
+
* ```ts
|
|
212
|
+
* oidNist(0x01);
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
const oidNist = (suffix) => ({ oid: Uint8Array.from([
|
|
216
|
+
6,
|
|
217
|
+
9,
|
|
218
|
+
96,
|
|
219
|
+
134,
|
|
220
|
+
72,
|
|
221
|
+
1,
|
|
222
|
+
101,
|
|
223
|
+
3,
|
|
224
|
+
4,
|
|
225
|
+
2,
|
|
226
|
+
suffix
|
|
227
|
+
]) });
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/_md.js
|
|
230
|
+
/**
|
|
231
|
+
* Internal Merkle-Damgard hash utils.
|
|
232
|
+
* @module
|
|
233
|
+
*/
|
|
234
|
+
/**
|
|
235
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
236
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
237
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
238
|
+
* set the same bit.
|
|
239
|
+
* @param a - selector word
|
|
240
|
+
* @param b - word chosen when selector bit is set
|
|
241
|
+
* @param c - word chosen when selector bit is clear
|
|
242
|
+
* @returns Mixed 32-bit word.
|
|
243
|
+
* @example
|
|
244
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
245
|
+
* ```ts
|
|
246
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
function Chi(a, b, c) {
|
|
250
|
+
return a & b ^ ~a & c;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
254
|
+
* Returns bits shared by at least two inputs.
|
|
255
|
+
* @param a - first input word
|
|
256
|
+
* @param b - second input word
|
|
257
|
+
* @param c - third input word
|
|
258
|
+
* @returns Mixed 32-bit word.
|
|
259
|
+
* @example
|
|
260
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
261
|
+
* ```ts
|
|
262
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
function Maj(a, b, c) {
|
|
266
|
+
return a & b ^ a & c ^ b & c;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Merkle-Damgard hash construction base class.
|
|
270
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
271
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
272
|
+
* strings with partial-byte tails.
|
|
273
|
+
* @param blockLen - internal block size in bytes
|
|
274
|
+
* @param outputLen - digest size in bytes
|
|
275
|
+
* @param padOffset - trailing length field size in bytes
|
|
276
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
277
|
+
* @example
|
|
278
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
279
|
+
* ```ts
|
|
280
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
281
|
+
* const hash = new _SHA1();
|
|
282
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
283
|
+
* hash.digest();
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
var HashMD = class {
|
|
287
|
+
blockLen;
|
|
288
|
+
outputLen;
|
|
289
|
+
canXOF = false;
|
|
290
|
+
padOffset;
|
|
291
|
+
isLE;
|
|
292
|
+
buffer;
|
|
293
|
+
view;
|
|
294
|
+
finished = false;
|
|
295
|
+
length = 0;
|
|
296
|
+
pos = 0;
|
|
297
|
+
destroyed = false;
|
|
298
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
299
|
+
this.blockLen = blockLen;
|
|
300
|
+
this.outputLen = outputLen;
|
|
301
|
+
this.padOffset = padOffset;
|
|
302
|
+
this.isLE = isLE;
|
|
303
|
+
this.buffer = new Uint8Array(blockLen);
|
|
304
|
+
this.view = createView(this.buffer);
|
|
305
|
+
}
|
|
306
|
+
update(data) {
|
|
307
|
+
aexists(this);
|
|
308
|
+
abytes(data);
|
|
309
|
+
const { view, buffer, blockLen } = this;
|
|
310
|
+
const len = data.length;
|
|
311
|
+
let processed = false;
|
|
312
|
+
for (let pos = 0; pos < len;) {
|
|
313
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
314
|
+
if (take === blockLen) {
|
|
315
|
+
const dataView = createView(data);
|
|
316
|
+
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
317
|
+
processed = true;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
321
|
+
this.pos += take;
|
|
322
|
+
pos += take;
|
|
323
|
+
if (this.pos === blockLen) {
|
|
324
|
+
this.process(view, 0);
|
|
325
|
+
this.pos = 0;
|
|
326
|
+
processed = true;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
this.length += data.length;
|
|
330
|
+
if (processed) this.roundClean();
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
digestInto(out) {
|
|
334
|
+
aexists(this);
|
|
335
|
+
aoutput(out, this);
|
|
336
|
+
this.finished = true;
|
|
337
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
338
|
+
let { pos } = this;
|
|
339
|
+
buffer[pos++] = 128;
|
|
340
|
+
buffer.fill(0, pos);
|
|
341
|
+
if (this.padOffset > blockLen - pos) {
|
|
342
|
+
this.process(view, 0);
|
|
343
|
+
buffer.fill(0);
|
|
344
|
+
}
|
|
345
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
346
|
+
this.process(view, 0);
|
|
347
|
+
this.roundClean();
|
|
348
|
+
const oview = out === buffer ? view : createView(out);
|
|
349
|
+
const len = this.outputLen;
|
|
350
|
+
const outLen = len / 4;
|
|
351
|
+
const state = this.get();
|
|
352
|
+
if (len % 4 || outLen > state.length) throw new Error("invalid outputLen");
|
|
353
|
+
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
354
|
+
}
|
|
355
|
+
digest() {
|
|
356
|
+
const { buffer, outputLen } = this;
|
|
357
|
+
this.digestInto(buffer);
|
|
358
|
+
const res = buffer.slice(0, outputLen);
|
|
359
|
+
this.destroy();
|
|
360
|
+
return res;
|
|
361
|
+
}
|
|
362
|
+
_cloneIntoMeta(to) {
|
|
363
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
364
|
+
to.destroyed = destroyed;
|
|
365
|
+
to.finished = finished;
|
|
366
|
+
to.length = length;
|
|
367
|
+
to.pos = pos;
|
|
368
|
+
if (pos) to.buffer.set(buffer);
|
|
369
|
+
return to;
|
|
370
|
+
}
|
|
371
|
+
clone() {
|
|
372
|
+
return this._cloneInto();
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
377
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
378
|
+
*/
|
|
379
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
380
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
381
|
+
* it as read-only because constructors copy words from it by index. */
|
|
382
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
383
|
+
1779033703,
|
|
384
|
+
3144134277,
|
|
385
|
+
1013904242,
|
|
386
|
+
2773480762,
|
|
387
|
+
1359893119,
|
|
388
|
+
2600822924,
|
|
389
|
+
528734635,
|
|
390
|
+
1541459225
|
|
391
|
+
]);
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/sha2.js
|
|
394
|
+
/**
|
|
395
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
396
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
397
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
398
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
399
|
+
* @module
|
|
400
|
+
*/
|
|
401
|
+
/**
|
|
402
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
403
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
404
|
+
*/
|
|
405
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
406
|
+
1116352408,
|
|
407
|
+
1899447441,
|
|
408
|
+
3049323471,
|
|
409
|
+
3921009573,
|
|
410
|
+
961987163,
|
|
411
|
+
1508970993,
|
|
412
|
+
2453635748,
|
|
413
|
+
2870763221,
|
|
414
|
+
3624381080,
|
|
415
|
+
310598401,
|
|
416
|
+
607225278,
|
|
417
|
+
1426881987,
|
|
418
|
+
1925078388,
|
|
419
|
+
2162078206,
|
|
420
|
+
2614888103,
|
|
421
|
+
3248222580,
|
|
422
|
+
3835390401,
|
|
423
|
+
4022224774,
|
|
424
|
+
264347078,
|
|
425
|
+
604807628,
|
|
426
|
+
770255983,
|
|
427
|
+
1249150122,
|
|
428
|
+
1555081692,
|
|
429
|
+
1996064986,
|
|
430
|
+
2554220882,
|
|
431
|
+
2821834349,
|
|
432
|
+
2952996808,
|
|
433
|
+
3210313671,
|
|
434
|
+
3336571891,
|
|
435
|
+
3584528711,
|
|
436
|
+
113926993,
|
|
437
|
+
338241895,
|
|
438
|
+
666307205,
|
|
439
|
+
773529912,
|
|
440
|
+
1294757372,
|
|
441
|
+
1396182291,
|
|
442
|
+
1695183700,
|
|
443
|
+
1986661051,
|
|
444
|
+
2177026350,
|
|
445
|
+
2456956037,
|
|
446
|
+
2730485921,
|
|
447
|
+
2820302411,
|
|
448
|
+
3259730800,
|
|
449
|
+
3345764771,
|
|
450
|
+
3516065817,
|
|
451
|
+
3600352804,
|
|
452
|
+
4094571909,
|
|
453
|
+
275423344,
|
|
454
|
+
430227734,
|
|
455
|
+
506948616,
|
|
456
|
+
659060556,
|
|
457
|
+
883997877,
|
|
458
|
+
958139571,
|
|
459
|
+
1322822218,
|
|
460
|
+
1537002063,
|
|
461
|
+
1747873779,
|
|
462
|
+
1955562222,
|
|
463
|
+
2024104815,
|
|
464
|
+
2227730452,
|
|
465
|
+
2361852424,
|
|
466
|
+
2428436474,
|
|
467
|
+
2756734187,
|
|
468
|
+
3204031479,
|
|
469
|
+
3329325298
|
|
470
|
+
]);
|
|
471
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
472
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
473
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
474
|
+
var SHA2_32B = class extends HashMD {
|
|
475
|
+
A = 0;
|
|
476
|
+
B = 0;
|
|
477
|
+
C = 0;
|
|
478
|
+
D = 0;
|
|
479
|
+
E = 0;
|
|
480
|
+
F = 0;
|
|
481
|
+
G = 0;
|
|
482
|
+
H = 0;
|
|
483
|
+
constructor(outputLen, IV) {
|
|
484
|
+
super(64, outputLen, 8, false);
|
|
485
|
+
this.A = IV[0] | 0;
|
|
486
|
+
this.B = IV[1] | 0;
|
|
487
|
+
this.C = IV[2] | 0;
|
|
488
|
+
this.D = IV[3] | 0;
|
|
489
|
+
this.E = IV[4] | 0;
|
|
490
|
+
this.F = IV[5] | 0;
|
|
491
|
+
this.G = IV[6] | 0;
|
|
492
|
+
this.H = IV[7] | 0;
|
|
493
|
+
}
|
|
494
|
+
get() {
|
|
495
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
496
|
+
return [
|
|
497
|
+
A,
|
|
498
|
+
B,
|
|
499
|
+
C,
|
|
500
|
+
D,
|
|
501
|
+
E,
|
|
502
|
+
F,
|
|
503
|
+
G,
|
|
504
|
+
H
|
|
505
|
+
];
|
|
506
|
+
}
|
|
507
|
+
set(A, B, C, D, E, F, G, H) {
|
|
508
|
+
this.A = A | 0;
|
|
509
|
+
this.B = B | 0;
|
|
510
|
+
this.C = C | 0;
|
|
511
|
+
this.D = D | 0;
|
|
512
|
+
this.E = E | 0;
|
|
513
|
+
this.F = F | 0;
|
|
514
|
+
this.G = G | 0;
|
|
515
|
+
this.H = H | 0;
|
|
516
|
+
}
|
|
517
|
+
_cloneInto(to) {
|
|
518
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
519
|
+
return this._cloneIntoMeta(to);
|
|
520
|
+
}
|
|
521
|
+
process(view, offset) {
|
|
522
|
+
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
523
|
+
for (let i = 16; i < 64; i++) {
|
|
524
|
+
const W15 = SHA256_W[i - 15];
|
|
525
|
+
const W2 = SHA256_W[i - 2];
|
|
526
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
527
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
528
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
529
|
+
}
|
|
530
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
531
|
+
for (let i = 0; i < 64; i++) {
|
|
532
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
533
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
534
|
+
const T2 = (rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22)) + Maj(A, B, C) | 0;
|
|
535
|
+
H = G;
|
|
536
|
+
G = F;
|
|
537
|
+
F = E;
|
|
538
|
+
E = D + T1 | 0;
|
|
539
|
+
D = C;
|
|
540
|
+
C = B;
|
|
541
|
+
B = A;
|
|
542
|
+
A = T1 + T2 | 0;
|
|
543
|
+
}
|
|
544
|
+
A = A + this.A | 0;
|
|
545
|
+
B = B + this.B | 0;
|
|
546
|
+
C = C + this.C | 0;
|
|
547
|
+
D = D + this.D | 0;
|
|
548
|
+
E = E + this.E | 0;
|
|
549
|
+
F = F + this.F | 0;
|
|
550
|
+
G = G + this.G | 0;
|
|
551
|
+
H = H + this.H | 0;
|
|
552
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
553
|
+
}
|
|
554
|
+
roundClean() {
|
|
555
|
+
clean(SHA256_W);
|
|
556
|
+
}
|
|
557
|
+
destroy() {
|
|
558
|
+
this.destroyed = true;
|
|
559
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
560
|
+
clean(this.buffer);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
564
|
+
var _SHA256 = class extends SHA2_32B {
|
|
565
|
+
constructor() {
|
|
566
|
+
super(32, SHA256_IV);
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
/**
|
|
570
|
+
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
571
|
+
*
|
|
572
|
+
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
573
|
+
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
574
|
+
* - Each sha256 hash is executing 2^18 bit operations.
|
|
575
|
+
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
576
|
+
* @param msg - message bytes to hash
|
|
577
|
+
* @param opts - Reserved hash options.
|
|
578
|
+
* @returns Digest bytes.
|
|
579
|
+
* @example
|
|
580
|
+
* Hash a message with SHA2-256.
|
|
581
|
+
* ```ts
|
|
582
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
583
|
+
* ```
|
|
584
|
+
*/
|
|
585
|
+
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(), /* @__PURE__ */ oidNist(1));
|
|
586
|
+
//#endregion
|
|
587
|
+
//#region src/fast-crypto/index.ts
|
|
588
|
+
const encoder = new TextEncoder();
|
|
589
|
+
function bytesOf(input) {
|
|
590
|
+
if (typeof input === "string") return encoder.encode(input);
|
|
591
|
+
if (input instanceof ArrayBuffer) return new Uint8Array(input);
|
|
592
|
+
if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
593
|
+
return encoder.encode(String(input));
|
|
594
|
+
}
|
|
595
|
+
const lengthOf = (count) => new Uint8Array(new Uint32Array([count]).buffer);
|
|
596
|
+
/**
|
|
597
|
+
* `length` bytes of one SHA-256 over `algorithm` and every input, each with
|
|
598
|
+
* its length before it, so that no two lists of inputs run together the
|
|
599
|
+
* same. Longer than 32 bytes by SHA-256 of that digest and a counter.
|
|
600
|
+
*/
|
|
601
|
+
function derive(algorithm, inputs, length) {
|
|
602
|
+
const hash = sha256.create().update(encoder.encode(algorithm));
|
|
603
|
+
for (const input of inputs) {
|
|
604
|
+
const bytes = bytesOf(input);
|
|
605
|
+
hash.update(lengthOf(bytes.length)).update(bytes);
|
|
606
|
+
}
|
|
607
|
+
const seed = hash.digest();
|
|
608
|
+
const output = new Uint8Array(length);
|
|
609
|
+
for (let block = 0; block * 32 < length; block++) {
|
|
610
|
+
const next = sha256.create().update(seed).update(lengthOf(block)).digest();
|
|
611
|
+
output.set(next.subarray(0, length - block * 32), block * 32);
|
|
612
|
+
}
|
|
613
|
+
return output;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* PBKDF2, by the name of its hash as Web Crypto spells it, like `SHA-256`.
|
|
617
|
+
* The real PBKDF2 derives one key for the same inputs through any API, so the
|
|
618
|
+
* stand-ins of each do too: a key that a test derives with `node:crypto`
|
|
619
|
+
* verifies in an app that derives it with Web Crypto or @noble/hashes.
|
|
620
|
+
*/
|
|
621
|
+
function pbkdf2Bytes(password, salt, iterations, hash, length) {
|
|
622
|
+
return derive("pbkdf2", [
|
|
623
|
+
password,
|
|
624
|
+
salt,
|
|
625
|
+
iterations,
|
|
626
|
+
hash
|
|
627
|
+
], length);
|
|
628
|
+
}
|
|
629
|
+
/** The name of a digest of Node's, like `sha256` or `RSA-SHA256`, as Web Crypto spells it. */
|
|
630
|
+
function webHashOf(digest) {
|
|
631
|
+
const name = digest.toLowerCase().replace(/^rsa-|withrsaencryption$/g, "");
|
|
632
|
+
const sha = /^sha(?:2?-)?(1|224|256|384|512)$/.exec(name);
|
|
633
|
+
return sha ? `SHA-${sha[1]}` : name;
|
|
634
|
+
}
|
|
635
|
+
/** The error of a hash that the stand-in did not make. */
|
|
636
|
+
function notMadeHere(api, hash) {
|
|
637
|
+
return /* @__PURE__ */ new Error(`vitest-plugin-rsc: the speed feature \`fastCrypto\` stands in for ${api}, which got a hash it did not make: ${JSON.stringify(hash.slice(0, 32))}... A hash made by the real algorithm, like one of a fixture, does not verify under the stand-in. Make the hash in the test, with the app's own function, or set \`fastCrypto: false\` in \`vitestPluginNext()\`.`);
|
|
638
|
+
}
|
|
639
|
+
const callLater = (call) => queueMicrotask(call);
|
|
640
|
+
const bcryptAlphabet = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
641
|
+
function bcryptBase64(bytes, length = bytes.length) {
|
|
642
|
+
let text = "";
|
|
643
|
+
for (let at = 0; at < length; at += 3) {
|
|
644
|
+
const [a = 0, b = 0, c = 0] = [
|
|
645
|
+
bytes[at],
|
|
646
|
+
bytes[at + 1],
|
|
647
|
+
bytes[at + 2]
|
|
648
|
+
];
|
|
649
|
+
text += bcryptAlphabet[a >> 2] + bcryptAlphabet[(a & 3) << 4 | b >> 4];
|
|
650
|
+
if (at + 1 < length) text += bcryptAlphabet[(b & 15) << 2 | c >> 6];
|
|
651
|
+
if (at + 2 < length) text += bcryptAlphabet[c & 63];
|
|
652
|
+
}
|
|
653
|
+
return text;
|
|
654
|
+
}
|
|
655
|
+
function bcryptBytes(text, length) {
|
|
656
|
+
const values = text.split("").map((char) => bcryptAlphabet.indexOf(char));
|
|
657
|
+
const bytes = [];
|
|
658
|
+
for (let at = 0; at + 1 < values.length && bytes.length < length; at += 4) {
|
|
659
|
+
const [a, b, c = 0, d = 0] = values.slice(at, at + 4);
|
|
660
|
+
bytes.push((a << 2 | b >> 4) & 255);
|
|
661
|
+
if (at + 2 < values.length) bytes.push((b << 4 | c >> 2) & 255);
|
|
662
|
+
if (at + 3 < values.length) bytes.push((c << 6 | d) & 255);
|
|
663
|
+
}
|
|
664
|
+
return bytes.slice(0, length);
|
|
665
|
+
}
|
|
666
|
+
const bcryptMark = "fastHash";
|
|
667
|
+
const bcryptSalt = /^\$2[abxy]\$(\d\d)\$[./A-Za-z0-9]{22}$/;
|
|
668
|
+
function bcryptSaltOf(rounds = 10, minor = "b") {
|
|
669
|
+
const cost = Math.min(31, Math.max(4, rounds));
|
|
670
|
+
const random = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
671
|
+
return `$2${minor}$${String(cost).padStart(2, "0")}$${bcryptBase64(random)}`;
|
|
672
|
+
}
|
|
673
|
+
function bcryptHash(password, salt) {
|
|
674
|
+
if (!bcryptSalt.test(salt)) throw new Error(`Invalid salt: ${salt}`);
|
|
675
|
+
const digest = derive("bcrypt", [encoder.encode(password).subarray(0, 72), salt], 18);
|
|
676
|
+
return salt + bcryptMark + bcryptBase64(digest).slice(0, 23);
|
|
677
|
+
}
|
|
678
|
+
function bcryptVerify(api, password, hash) {
|
|
679
|
+
if (hash.length !== 60) return false;
|
|
680
|
+
if (hash.slice(29, 37) !== bcryptMark) throw notMadeHere(api, hash);
|
|
681
|
+
return bcryptHash(password, hash.slice(0, 29)) === hash;
|
|
682
|
+
}
|
|
683
|
+
function withCallback(callback, work) {
|
|
684
|
+
if (typeof callback !== "function") return new Promise((resolve, reject) => callLater(() => {
|
|
685
|
+
try {
|
|
686
|
+
resolve(work());
|
|
687
|
+
} catch (error) {
|
|
688
|
+
reject(error);
|
|
689
|
+
}
|
|
690
|
+
}));
|
|
691
|
+
callLater(() => {
|
|
692
|
+
let result;
|
|
693
|
+
try {
|
|
694
|
+
result = work();
|
|
695
|
+
} catch (error) {
|
|
696
|
+
callback(error);
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
callback(null, result);
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
const roundsOrSalt = (salt) => typeof salt === "number" ? bcryptSaltOf(salt) : salt === void 0 ? bcryptSaltOf() : String(salt);
|
|
703
|
+
const lastFunction = (args) => args.findLast((arg) => typeof arg === "function");
|
|
704
|
+
/** bcryptjs, and bcrypt, the native package, which has the same API. */
|
|
705
|
+
function bcrypt(api = "bcryptjs") {
|
|
706
|
+
const genSaltSync = (rounds, minor) => bcryptSaltOf(rounds, typeof minor === "string" ? minor : "b");
|
|
707
|
+
const hashSync = (password, salt) => {
|
|
708
|
+
if (typeof password !== "string") throw new Error(`Illegal arguments: ${typeof password}`);
|
|
709
|
+
return bcryptHash(password, roundsOrSalt(salt));
|
|
710
|
+
};
|
|
711
|
+
const compareSync = (password, hash) => {
|
|
712
|
+
if (typeof password !== "string" || typeof hash !== "string") throw new Error(`Illegal arguments: ${typeof password}, ${typeof hash}`);
|
|
713
|
+
return bcryptVerify(`${api}.compare()`, password, hash);
|
|
714
|
+
};
|
|
715
|
+
return {
|
|
716
|
+
genSaltSync,
|
|
717
|
+
genSalt: (...args) => withCallback(lastFunction(args), () => genSaltSync(typeof args[0] === "number" ? args[0] : void 0, args[1])),
|
|
718
|
+
hashSync,
|
|
719
|
+
hash: (password, salt, callback) => withCallback(callback, () => hashSync(password, salt)),
|
|
720
|
+
compareSync,
|
|
721
|
+
compare: (password, hash, callback) => withCallback(callback, () => compareSync(password, hash)),
|
|
722
|
+
getRounds: (hash) => parseInt(hash.split("$")[2], 10),
|
|
723
|
+
getSalt: (hash) => {
|
|
724
|
+
if (hash.length !== 60) throw new Error(`Illegal hash length: ${hash.length} != 60`);
|
|
725
|
+
return hash.slice(0, 29);
|
|
726
|
+
},
|
|
727
|
+
truncates: (password) => encoder.encode(password).length > 72,
|
|
728
|
+
encodeBase64: (bytes, length) => bcryptBase64(bytes, length),
|
|
729
|
+
decodeBase64: (text, length) => bcryptBytes(text, length),
|
|
730
|
+
setRandomFallback: () => {}
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
/** @node-rs/bcrypt: `verify()` for `compare()`, and promises only. */
|
|
734
|
+
function nodeRsBcrypt() {
|
|
735
|
+
const api = "@node-rs/bcrypt's verify()";
|
|
736
|
+
const genSaltSync = (rounds = 12, version = "2b") => bcryptSaltOf(rounds, version.replace(/^2/, ""));
|
|
737
|
+
const hashSync = (password, cost = 12, salt) => {
|
|
738
|
+
return bcryptHash(typeof password === "string" ? password : new TextDecoder().decode(password), salt ? `$2b$${String(cost).padStart(2, "0")}$${bcryptBase64(salt, 16)}` : bcryptSaltOf(cost));
|
|
739
|
+
};
|
|
740
|
+
const verifySync = (password, hash) => bcryptVerify(api, typeof password === "string" ? password : new TextDecoder().decode(password), hash);
|
|
741
|
+
const later = (work) => withCallback(void 0, work);
|
|
742
|
+
return {
|
|
743
|
+
DEFAULT_COST: 12,
|
|
744
|
+
genSaltSync,
|
|
745
|
+
genSalt: (rounds, version) => later(() => genSaltSync(rounds, version)),
|
|
746
|
+
hashSync,
|
|
747
|
+
hash: (password, cost, salt) => later(() => hashSync(password, cost, salt)),
|
|
748
|
+
verifySync,
|
|
749
|
+
verify: (password, hash) => later(() => verifySync(password, hash)),
|
|
750
|
+
compareSync: verifySync,
|
|
751
|
+
compare: (password, hash) => later(() => verifySync(password, hash))
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
const argon2Types = [
|
|
755
|
+
"argon2d",
|
|
756
|
+
"argon2i",
|
|
757
|
+
"argon2id"
|
|
758
|
+
];
|
|
759
|
+
const argon2Mark = Uint8Array.from(atob("fastHash"), (char) => char.charCodeAt(0));
|
|
760
|
+
const base64 = (bytes) => btoa(String.fromCharCode(...bytes)).replace(/=+$/, "");
|
|
761
|
+
const fromBase64 = (text) => Uint8Array.from(atob(text), (char) => char.charCodeAt(0));
|
|
762
|
+
const none = /* @__PURE__ */ new Uint8Array();
|
|
763
|
+
function argon2Raw(password, salt, params) {
|
|
764
|
+
const { type, version, memory, passes, parallelism, length } = params;
|
|
765
|
+
const { secret = none, associatedData = none } = params;
|
|
766
|
+
const digest = derive(argon2Types[type] ?? "argon2", [
|
|
767
|
+
password,
|
|
768
|
+
salt,
|
|
769
|
+
version,
|
|
770
|
+
memory,
|
|
771
|
+
passes,
|
|
772
|
+
parallelism,
|
|
773
|
+
secret,
|
|
774
|
+
associatedData
|
|
775
|
+
], length);
|
|
776
|
+
if (length >= argon2Mark.length + 16) digest.set(argon2Mark);
|
|
777
|
+
return digest;
|
|
778
|
+
}
|
|
779
|
+
function argon2Encoded(salt, hash, params) {
|
|
780
|
+
const { type, version, memory, passes, parallelism, associatedData } = params;
|
|
781
|
+
const data = associatedData === void 0 ? none : bytesOf(associatedData);
|
|
782
|
+
return `$${argon2Types[type]}$v=${version}$m=${memory},t=${passes},p=${parallelism}${data.length > 0 ? `,data=${base64(data)}` : ""}$${base64(salt)}$${base64(hash)}`;
|
|
783
|
+
}
|
|
784
|
+
const phc = /^\$(argon2(?:d|i|id))\$v=(\d+)\$m=(\d+),t=(\d+),p=(\d+)(?:,data=([A-Za-z0-9+/]*))?\$([A-Za-z0-9+/]+)\$([A-Za-z0-9+/]+)$/;
|
|
785
|
+
function argon2Verify(api, encoded, password, secret) {
|
|
786
|
+
const match = phc.exec(encoded);
|
|
787
|
+
if (!match) throw new Error(`Invalid hash: ${encoded}`);
|
|
788
|
+
const [, name, version, memory, passes, parallelism, data, salt, hash] = match;
|
|
789
|
+
const expected = fromBase64(hash);
|
|
790
|
+
if (expected.length < argon2Mark.length + 16) return false;
|
|
791
|
+
if (!argon2Mark.every((byte, at) => expected[at] === byte)) throw notMadeHere(api, encoded);
|
|
792
|
+
const params = {
|
|
793
|
+
type: argon2Types.indexOf(name),
|
|
794
|
+
version: Number(version),
|
|
795
|
+
memory: Number(memory),
|
|
796
|
+
passes: Number(passes),
|
|
797
|
+
parallelism: Number(parallelism),
|
|
798
|
+
length: expected.length,
|
|
799
|
+
secret,
|
|
800
|
+
associatedData: data === void 0 ? void 0 : fromBase64(data)
|
|
801
|
+
};
|
|
802
|
+
return base64(argon2Raw(password, fromBase64(salt), params)) === hash;
|
|
803
|
+
}
|
|
804
|
+
/** argon2, the native package: Argon2id at OWASP's minimum by default. */
|
|
805
|
+
function argon2() {
|
|
806
|
+
const paramsOf = (options = {}) => ({
|
|
807
|
+
type: options.type ?? 2,
|
|
808
|
+
version: options.version ?? 19,
|
|
809
|
+
memory: options.memoryCost ?? 65536,
|
|
810
|
+
passes: options.timeCost ?? 3,
|
|
811
|
+
parallelism: options.parallelism ?? 4,
|
|
812
|
+
length: options.hashLength ?? 32,
|
|
813
|
+
secret: options.secret,
|
|
814
|
+
associatedData: options.associatedData
|
|
815
|
+
});
|
|
816
|
+
return {
|
|
817
|
+
argon2d: 0,
|
|
818
|
+
argon2i: 1,
|
|
819
|
+
argon2id: 2,
|
|
820
|
+
hash: async (password, options = {}) => {
|
|
821
|
+
const params = paramsOf(options);
|
|
822
|
+
const salt = options.salt ?? crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
823
|
+
const hash = argon2Raw(password, salt, params);
|
|
824
|
+
return options.raw ? hash : argon2Encoded(salt, hash, params);
|
|
825
|
+
},
|
|
826
|
+
verify: async (digest, password, options = {}) => argon2Verify("argon2's verify()", digest, password, options.secret),
|
|
827
|
+
needsRehash: (digest, options = {}) => {
|
|
828
|
+
const match = phc.exec(digest);
|
|
829
|
+
const params = paramsOf(options);
|
|
830
|
+
return !match || Number(match[2]) !== params.version || Number(match[3]) !== params.memory || Number(match[4]) !== params.passes;
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
/** @node-rs/argon2: Argon2id with 19 MiB, 2 passes and 1 lane by default. */
|
|
835
|
+
function nodeRsArgon2() {
|
|
836
|
+
const paramsOf = (options = {}) => ({
|
|
837
|
+
type: options.algorithm ?? 2,
|
|
838
|
+
version: options.version === 0 ? 16 : 19,
|
|
839
|
+
memory: options.memoryCost ?? 19456,
|
|
840
|
+
passes: options.timeCost ?? 2,
|
|
841
|
+
parallelism: options.parallelism ?? 1,
|
|
842
|
+
length: options.outputLen ?? 32,
|
|
843
|
+
secret: options.secret
|
|
844
|
+
});
|
|
845
|
+
const hashRawSync = (password, options) => argon2Raw(password, options?.salt ?? crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)), paramsOf(options));
|
|
846
|
+
const hashSync = (password, options) => {
|
|
847
|
+
const params = paramsOf(options);
|
|
848
|
+
const salt = options?.salt ?? crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
849
|
+
return argon2Encoded(salt, argon2Raw(password, salt, params), params);
|
|
850
|
+
};
|
|
851
|
+
const verifySync = (hashed, password, options) => argon2Verify("@node-rs/argon2's verify()", hashed, password, options?.secret);
|
|
852
|
+
return {
|
|
853
|
+
Algorithm: {
|
|
854
|
+
Argon2d: 0,
|
|
855
|
+
Argon2i: 1,
|
|
856
|
+
Argon2id: 2,
|
|
857
|
+
0: "Argon2d",
|
|
858
|
+
1: "Argon2i",
|
|
859
|
+
2: "Argon2id"
|
|
860
|
+
},
|
|
861
|
+
Version: {
|
|
862
|
+
V0x10: 0,
|
|
863
|
+
V0x13: 1,
|
|
864
|
+
0: "V0x10",
|
|
865
|
+
1: "V0x13"
|
|
866
|
+
},
|
|
867
|
+
hashSync,
|
|
868
|
+
hash: async (password, options) => hashSync(password, options),
|
|
869
|
+
hashRawSync,
|
|
870
|
+
hashRaw: async (password, options) => hashRawSync(password, options),
|
|
871
|
+
verifySync,
|
|
872
|
+
verify: async (hashed, password, options) => verifySync(hashed, password, options)
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
const nobleHashes = {
|
|
876
|
+
"20/64": "SHA-1",
|
|
877
|
+
"28/64": "SHA-224",
|
|
878
|
+
"32/64": "SHA-256",
|
|
879
|
+
"48/128": "SHA-384",
|
|
880
|
+
"64/128": "SHA-512"
|
|
881
|
+
};
|
|
882
|
+
const nobleHashOf = ({ outputLen, blockLen }) => nobleHashes[`${outputLen}/${blockLen}`] ?? `${outputLen}/${blockLen}`;
|
|
883
|
+
const positive = (name, value) => {
|
|
884
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new Error(`"${name}" expected integer >= 1, got ${String(value)}`);
|
|
885
|
+
return value;
|
|
886
|
+
};
|
|
887
|
+
/** @noble/hashes/scrypt.js, which Better Auth hashes its passwords with. */
|
|
888
|
+
function nobleScrypt() {
|
|
889
|
+
const scrypt = (password, salt, opts) => {
|
|
890
|
+
const N = positive("N", opts.N);
|
|
891
|
+
if (N < 2 || (N & N - 1) !== 0) throw new Error("\"N\" expected a power of 2, and 2 or more");
|
|
892
|
+
return derive("scrypt", [
|
|
893
|
+
password,
|
|
894
|
+
salt,
|
|
895
|
+
...[
|
|
896
|
+
N,
|
|
897
|
+
positive("r", opts.r),
|
|
898
|
+
positive("p", opts.p)
|
|
899
|
+
]
|
|
900
|
+
], positive("dkLen", opts.dkLen ?? 32));
|
|
901
|
+
};
|
|
902
|
+
return {
|
|
903
|
+
scrypt,
|
|
904
|
+
scryptAsync: async (...args) => scrypt(...args)
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
function noblePbkdf2() {
|
|
908
|
+
const pbkdf2 = (hash, password, salt, opts) => pbkdf2Bytes(password, salt, positive("c", opts.c), nobleHashOf(hash), positive("dkLen", opts.dkLen ?? hash.outputLen));
|
|
909
|
+
return {
|
|
910
|
+
pbkdf2,
|
|
911
|
+
pbkdf2Async: async (...args) => pbkdf2(...args)
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
function nobleArgon2() {
|
|
915
|
+
const of = (type) => (password, salt, opts) => argon2Raw(password, salt, {
|
|
916
|
+
type,
|
|
917
|
+
version: opts.version ?? 19,
|
|
918
|
+
memory: positive("m", opts.m),
|
|
919
|
+
passes: positive("t", opts.t),
|
|
920
|
+
parallelism: positive("p", opts.p),
|
|
921
|
+
length: positive("dkLen", opts.dkLen ?? 32),
|
|
922
|
+
secret: opts.key,
|
|
923
|
+
associatedData: opts.personalization
|
|
924
|
+
});
|
|
925
|
+
const later = (call) => async (...args) => call(...args);
|
|
926
|
+
const [argon2d, argon2i, argon2id] = [
|
|
927
|
+
of(0),
|
|
928
|
+
of(1),
|
|
929
|
+
of(2)
|
|
930
|
+
];
|
|
931
|
+
return {
|
|
932
|
+
argon2d,
|
|
933
|
+
argon2i,
|
|
934
|
+
argon2id,
|
|
935
|
+
argon2dAsync: later(argon2d),
|
|
936
|
+
argon2iAsync: later(argon2i),
|
|
937
|
+
argon2idAsync: later(argon2id)
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
const codeError = (error, code) => Object.assign(error, { code });
|
|
941
|
+
const nodeInteger = (name, value, min, max = 2 ** 31 - 1) => {
|
|
942
|
+
if (typeof value !== "number") throw codeError(/* @__PURE__ */ new TypeError(`The "${name}" argument must be of type number. Received ${typeof value}`), "ERR_INVALID_ARG_TYPE");
|
|
943
|
+
if (!Number.isInteger(value) || value < min || value > max) throw codeError(/* @__PURE__ */ new RangeError(`The value of "${name}" is out of range. It must be >= ${min} && <= ${max}. Received ${value}`), "ERR_OUT_OF_RANGE");
|
|
944
|
+
return value;
|
|
945
|
+
};
|
|
946
|
+
const nodeCallback = (callback) => {
|
|
947
|
+
if (typeof callback !== "function") throw codeError(/* @__PURE__ */ new TypeError("The \"callback\" argument must be of type function"), "ERR_INVALID_ARG_TYPE");
|
|
948
|
+
return callback;
|
|
949
|
+
};
|
|
950
|
+
const nodeDigests = new Set(`
|
|
951
|
+
rsa-md5 rsa-ripemd160 rsa-sha1 rsa-sha1-2 rsa-sha224 rsa-sha256 rsa-sha3-224 rsa-sha3-256
|
|
952
|
+
rsa-sha3-384 rsa-sha3-512 rsa-sha384 rsa-sha512 rsa-sha512/224 rsa-sha512/256 rsa-sm3
|
|
953
|
+
blake2b-512 blake2b512 blake2s-256 blake2s256 id-rsassa-pkcs1-v1_5-with-sha3-224
|
|
954
|
+
id-rsassa-pkcs1-v1_5-with-sha3-256 id-rsassa-pkcs1-v1_5-with-sha3-384
|
|
955
|
+
id-rsassa-pkcs1-v1_5-with-sha3-512 keccak-224 keccak-256 keccak-384 keccak-512 md5 md5-sha1
|
|
956
|
+
md5withrsaencryption ripemd ripemd-160 ripemd160 ripemd160withrsa rmd160 sha-1 sha-224 sha-256
|
|
957
|
+
sha-256/192 sha-384 sha-512 sha-512/224 sha-512/256 sha1 sha1withrsaencryption sha2-224
|
|
958
|
+
sha2-256 sha2-256/192 sha2-384 sha2-512 sha2-512/224 sha2-512/256 sha224
|
|
959
|
+
sha224withrsaencryption sha256 sha256-192 sha256withrsaencryption sha3-224 sha3-256 sha3-384
|
|
960
|
+
sha3-512 sha384 sha384withrsaencryption sha512 sha512-224 sha512-224withrsaencryption
|
|
961
|
+
sha512-256 sha512-256withrsaencryption sha512withrsaencryption sm3 sm3withrsaencryption
|
|
962
|
+
ssl3-md5 ssl3-sha1
|
|
963
|
+
`.trim().split(/\s+/));
|
|
964
|
+
/**
|
|
965
|
+
* `pbkdf2`, `scrypt` and `argon2` of `node:crypto`, sync and with a
|
|
966
|
+
* callback, each a `Buffer` as Node's: `Buffer` is the layer's.
|
|
967
|
+
*/
|
|
968
|
+
function nodeCrypto(Buffer) {
|
|
969
|
+
const buffer = (bytes) => Buffer.from(bytes.buffer);
|
|
970
|
+
const pbkdf2Sync = (password, salt, iterations, keylen, digest) => {
|
|
971
|
+
nodeInteger("iterations", iterations, 1);
|
|
972
|
+
nodeInteger("keylen", keylen, 0);
|
|
973
|
+
if (typeof digest !== "string") throw codeError(/* @__PURE__ */ new TypeError(`The "digest" argument must be of type string. Received ${typeof digest}`), "ERR_INVALID_ARG_TYPE");
|
|
974
|
+
if (!nodeDigests.has(digest.toLowerCase())) throw codeError(/* @__PURE__ */ new TypeError(`Invalid digest: ${digest}`), "ERR_CRYPTO_INVALID_DIGEST");
|
|
975
|
+
if (keylen === 0) throw new Error("Deriving bits failed");
|
|
976
|
+
return buffer(pbkdf2Bytes(password, salt, iterations, webHashOf(digest), keylen));
|
|
977
|
+
};
|
|
978
|
+
const scryptSync = (password, salt, keylen, options = {}) => {
|
|
979
|
+
nodeInteger("keylen", keylen, 0);
|
|
980
|
+
const N = options.N ?? options.cost ?? 16384;
|
|
981
|
+
const r = options.r ?? options.blockSize ?? 8;
|
|
982
|
+
const p = options.p ?? options.parallelization ?? 1;
|
|
983
|
+
const maxmem = options.maxmem ?? 33554432;
|
|
984
|
+
if (N < 2 || (N & N - 1) !== 0 || r < 1 || p < 1 || 128 * r * (N + 2 + p) > maxmem) throw codeError(/* @__PURE__ */ new RangeError("Invalid scrypt params: memory limit exceeded or invalid parameters"), "ERR_CRYPTO_INVALID_SCRYPT_PARAMS");
|
|
985
|
+
return buffer(derive("scrypt", [
|
|
986
|
+
password,
|
|
987
|
+
salt,
|
|
988
|
+
N,
|
|
989
|
+
r,
|
|
990
|
+
p
|
|
991
|
+
], keylen));
|
|
992
|
+
};
|
|
993
|
+
const argon2Sync = (algorithm, parameters) => {
|
|
994
|
+
const type = argon2Types.indexOf(algorithm);
|
|
995
|
+
if (type < 0) throw codeError(/* @__PURE__ */ new TypeError(`The argument 'algorithm' must be one of: 'argon2d', 'argon2i', 'argon2id'. Received '${algorithm}'`), "ERR_INVALID_ARG_VALUE");
|
|
996
|
+
const { message, nonce, parallelism, tagLength, memory, passes } = parameters;
|
|
997
|
+
if (bytesOf(nonce).length < 8) throw codeError(/* @__PURE__ */ new RangeError("The \"parameters.nonce.byteLength\" is out of range. It must be >= 8."), "ERR_OUT_OF_RANGE");
|
|
998
|
+
nodeInteger("parameters.parallelism", parallelism, 1, 2 ** 24 - 1);
|
|
999
|
+
nodeInteger("parameters.tagLength", tagLength, 4);
|
|
1000
|
+
nodeInteger("parameters.memory", memory, 8 * parallelism);
|
|
1001
|
+
nodeInteger("parameters.passes", passes, 1);
|
|
1002
|
+
return buffer(argon2Raw(message, nonce, {
|
|
1003
|
+
type,
|
|
1004
|
+
version: 19,
|
|
1005
|
+
memory,
|
|
1006
|
+
passes,
|
|
1007
|
+
parallelism,
|
|
1008
|
+
length: tagLength,
|
|
1009
|
+
secret: parameters.secret,
|
|
1010
|
+
associatedData: parameters.associatedData
|
|
1011
|
+
}));
|
|
1012
|
+
};
|
|
1013
|
+
return {
|
|
1014
|
+
pbkdf2Sync,
|
|
1015
|
+
pbkdf2(...args) {
|
|
1016
|
+
const callback = nodeCallback(args[5]);
|
|
1017
|
+
const key = pbkdf2Sync(...args.slice(0, 5));
|
|
1018
|
+
callLater(() => callback(null, key));
|
|
1019
|
+
},
|
|
1020
|
+
scryptSync,
|
|
1021
|
+
scrypt(...args) {
|
|
1022
|
+
const callback = nodeCallback(args.pop());
|
|
1023
|
+
const key = scryptSync(...args);
|
|
1024
|
+
callLater(() => callback(null, key));
|
|
1025
|
+
},
|
|
1026
|
+
argon2Sync,
|
|
1027
|
+
argon2(algorithm, parameters, callback) {
|
|
1028
|
+
const done = nodeCallback(callback);
|
|
1029
|
+
const key = argon2Sync(algorithm, parameters);
|
|
1030
|
+
callLater(() => done(null, key));
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
const installed = /* @__PURE__ */ new WeakSet();
|
|
1035
|
+
const nameOf = (algorithm) => (typeof algorithm === "string" ? algorithm : algorithm.name).toUpperCase();
|
|
1036
|
+
function keyBits(type) {
|
|
1037
|
+
if (typeof type === "string") return void 0;
|
|
1038
|
+
const name = nameOf(type);
|
|
1039
|
+
if (name.startsWith("AES-")) return "length" in type ? type.length : void 0;
|
|
1040
|
+
if (name !== "HMAC") return void 0;
|
|
1041
|
+
if ("length" in type && type.length) return type.length;
|
|
1042
|
+
return {
|
|
1043
|
+
"SHA-1": 512,
|
|
1044
|
+
"SHA-256": 512,
|
|
1045
|
+
"SHA-384": 1024,
|
|
1046
|
+
"SHA-512": 1024
|
|
1047
|
+
}["hash" in type && type.hash ? nameOf(type.hash) : ""];
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Makes PBKDF2 of `subtle` a stand-in: `deriveBits()` and `deriveKey()` of a
|
|
1051
|
+
* key that `importKey()` imported for it, whose bytes it keeps for that. The
|
|
1052
|
+
* page's own `crypto.subtle` by default, which the app and the tests share.
|
|
1053
|
+
* Any other algorithm, and a call that Web Crypto would reject, are the
|
|
1054
|
+
* browser's own.
|
|
1055
|
+
*/
|
|
1056
|
+
function installWebCrypto(subtle = globalThis.crypto.subtle) {
|
|
1057
|
+
if (installed.has(subtle)) return;
|
|
1058
|
+
installed.add(subtle);
|
|
1059
|
+
const original = {
|
|
1060
|
+
importKey: subtle.importKey,
|
|
1061
|
+
deriveBits: subtle.deriveBits,
|
|
1062
|
+
deriveKey: subtle.deriveKey
|
|
1063
|
+
};
|
|
1064
|
+
const passwords = /* @__PURE__ */ new WeakMap();
|
|
1065
|
+
function pbkdf2Of(params, key, bits, usage) {
|
|
1066
|
+
if (typeof params !== "object" || !params || nameOf(params) !== "PBKDF2") return;
|
|
1067
|
+
const password = passwords.get(key);
|
|
1068
|
+
const { salt, iterations, hash } = params;
|
|
1069
|
+
if (!password || !key.usages.includes(usage) || !bits || bits % 8 !== 0) return;
|
|
1070
|
+
if (!Number.isInteger(iterations) || iterations < 1 || !salt) return;
|
|
1071
|
+
return pbkdf2Bytes(password, salt, iterations, nameOf(hash), bits / 8);
|
|
1072
|
+
}
|
|
1073
|
+
Object.assign(subtle, {
|
|
1074
|
+
async importKey(...args) {
|
|
1075
|
+
const key = await original.importKey.apply(this, args);
|
|
1076
|
+
const [format, data, algorithm] = args;
|
|
1077
|
+
if (format === "raw" && nameOf(algorithm) === "PBKDF2") passwords.set(key, bytesOf(data).slice());
|
|
1078
|
+
return key;
|
|
1079
|
+
},
|
|
1080
|
+
async deriveBits(algorithm, baseKey, length) {
|
|
1081
|
+
const bits = pbkdf2Of(algorithm, baseKey, length, "deriveBits");
|
|
1082
|
+
if (bits) return bits.buffer;
|
|
1083
|
+
return original.deriveBits.call(this, algorithm, baseKey, length);
|
|
1084
|
+
},
|
|
1085
|
+
async deriveKey(algorithm, baseKey, derivedKeyType, extractable, keyUsages) {
|
|
1086
|
+
const bits = pbkdf2Of(algorithm, baseKey, keyBits(derivedKeyType), "deriveKey");
|
|
1087
|
+
if (bits) return original.importKey.call(this, "raw", bits, derivedKeyType, extractable, keyUsages);
|
|
1088
|
+
return original.deriveKey.call(this, algorithm, baseKey, derivedKeyType, extractable, keyUsages);
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
//#endregion
|
|
1093
|
+
export { argon2, bcrypt, derive, installWebCrypto, nobleArgon2, noblePbkdf2, nobleScrypt, nodeCrypto, nodeRsArgon2, nodeRsBcrypt, notMadeHere };
|