@stryke/crypto 0.5.43 → 0.6.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/CHANGELOG.md +14 -0
- package/dist/cloudflare.cjs +960 -0
- package/dist/cloudflare.d.cts +251 -0
- package/dist/cloudflare.d.cts.map +1 -0
- package/dist/cloudflare.d.mts +251 -0
- package/dist/cloudflare.d.mts.map +1 -0
- package/dist/cloudflare.mjs +942 -0
- package/dist/cloudflare.mjs.map +1 -0
- package/dist/index.cjs +16 -12
- package/dist/index.d.cts +2 -4
- package/dist/index.d.mts +2 -4
- package/dist/index.mjs +2 -4
- package/dist/neutral-BISXAuIW.cjs +868 -0
- package/dist/{encryption.d.mts → neutral-BJmyEdJq.d.mts} +77 -2
- package/dist/neutral-BJmyEdJq.d.mts.map +1 -0
- package/dist/neutral-BtZoJSl-.mjs +779 -0
- package/dist/neutral-BtZoJSl-.mjs.map +1 -0
- package/dist/{encryption.d.cts → neutral-CW6EXiYo.d.cts} +77 -2
- package/dist/neutral-CW6EXiYo.d.cts.map +1 -0
- package/dist/neutral.cjs +16 -16
- package/dist/neutral.d.cts +2 -4
- package/dist/neutral.d.mts +2 -4
- package/dist/neutral.mjs +2 -4
- package/dist/node.cjs +663 -0
- package/dist/node.d.cts +117 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +117 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +654 -0
- package/dist/node.mjs.map +1 -0
- package/package.json +55 -20
- package/dist/base-64.cjs +0 -440
- package/dist/base-64.d.cts +0 -40
- package/dist/base-64.d.cts.map +0 -1
- package/dist/base-64.d.mts +0 -40
- package/dist/base-64.d.mts.map +0 -1
- package/dist/base-64.mjs +0 -438
- package/dist/base-64.mjs.map +0 -1
- package/dist/convert/src/array-buffer-to-string.cjs +0 -19
- package/dist/convert/src/array-buffer-to-string.mjs +0 -19
- package/dist/convert/src/array-buffer-to-string.mjs.map +0 -1
- package/dist/convert/src/neutral.cjs +0 -7
- package/dist/convert/src/neutral.mjs +0 -9
- package/dist/convert/src/parse-type-definition.cjs +0 -1
- package/dist/convert/src/parse-type-definition.mjs +0 -3
- package/dist/convert/src/string-to-uint8-array.cjs +0 -24
- package/dist/convert/src/string-to-uint8-array.mjs +0 -23
- package/dist/convert/src/string-to-uint8-array.mjs.map +0 -1
- package/dist/convert/src/string-to-utf8-array.cjs +0 -15
- package/dist/convert/src/string-to-utf8-array.mjs +0 -15
- package/dist/convert/src/string-to-utf8-array.mjs.map +0 -1
- package/dist/convert/src/uint8-array-to-stream.cjs +0 -22
- package/dist/convert/src/uint8-array-to-stream.mjs +0 -22
- package/dist/convert/src/uint8-array-to-stream.mjs.map +0 -1
- package/dist/convert/src/uint8-array-to-string.cjs +0 -15
- package/dist/convert/src/uint8-array-to-string.mjs +0 -16
- package/dist/convert/src/uint8-array-to-string.mjs.map +0 -1
- package/dist/convert/src/utf8-array-to-string.cjs +0 -15
- package/dist/convert/src/utf8-array-to-string.mjs +0 -15
- package/dist/convert/src/utf8-array-to-string.mjs.map +0 -1
- package/dist/encryption.cjs +0 -134
- package/dist/encryption.d.cts.map +0 -1
- package/dist/encryption.d.mts.map +0 -1
- package/dist/encryption.mjs +0 -129
- package/dist/encryption.mjs.map +0 -1
- package/dist/encryption.node.cjs +0 -63
- package/dist/encryption.node.d.cts +0 -42
- package/dist/encryption.node.d.cts.map +0 -1
- package/dist/encryption.node.d.mts +0 -42
- package/dist/encryption.node.d.mts.map +0 -1
- package/dist/encryption.node.mjs +0 -62
- package/dist/encryption.node.mjs.map +0 -1
- package/dist/hex.cjs +0 -62
- package/dist/hex.d.cts +0 -18
- package/dist/hex.d.cts.map +0 -1
- package/dist/hex.d.mts +0 -18
- package/dist/hex.d.mts.map +0 -1
- package/dist/hex.mjs +0 -61
- package/dist/hex.mjs.map +0 -1
- package/dist/type-checks/src/index.cjs +0 -5
- package/dist/type-checks/src/index.mjs +0 -7
- package/dist/type-checks/src/is-buffer.cjs +0 -12
- package/dist/type-checks/src/is-buffer.mjs +0 -12
- package/dist/type-checks/src/is-buffer.mjs.map +0 -1
- package/dist/type-checks/src/is-collection.cjs +0 -1
- package/dist/type-checks/src/is-collection.mjs +0 -3
- package/dist/type-checks/src/is-string.cjs +0 -12
- package/dist/type-checks/src/is-string.mjs +0 -12
- package/dist/type-checks/src/is-string.mjs.map +0 -1
- package/dist/type-checks/src/is-undefined.cjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs.map +0 -1
- package/dist/type-checks/src/type-detect.cjs +0 -15
- package/dist/type-checks/src/type-detect.mjs +0 -17
- package/dist/type-checks/src/type-detect.mjs.map +0 -1
|
@@ -0,0 +1,942 @@
|
|
|
1
|
+
//#region ../json/src/canonical.ts
|
|
2
|
+
/**
|
|
3
|
+
* Converts a JavaScript value to a canonical JSON string representation. This function is used for signing JSON objects in a consistent way, ensuring that the same input will always produce the same output string. The canonicalization process includes:
|
|
4
|
+
* - Sorting object keys in lexicographical order.
|
|
5
|
+
* - Removing whitespace and line breaks.
|
|
6
|
+
* - Representing primitive values (null, boolean, number, string) in their standard JSON format.
|
|
7
|
+
* - Recursively applying these rules to nested objects and arrays.
|
|
8
|
+
*
|
|
9
|
+
* This function is designed to produce a deterministic string representation of a JSON value, which is essential for cryptographic signing and verification processes where the exact byte representation of the data must be consistent across different environments and implementations.
|
|
10
|
+
*
|
|
11
|
+
* @param obj - The JavaScript value to convert to a canonical JSON string.
|
|
12
|
+
* @returns A canonical JSON string representation of the input value.
|
|
13
|
+
*/
|
|
14
|
+
function canonicalJson(obj) {
|
|
15
|
+
if (obj === null || obj === void 0) return "null";
|
|
16
|
+
if (typeof obj === "boolean" || typeof obj === "number") return JSON.stringify(obj);
|
|
17
|
+
if (typeof obj === "string") return JSON.stringify(obj);
|
|
18
|
+
if (Array.isArray(obj)) return `[${obj.map((item) => canonicalJson(item)).join(",")}]`;
|
|
19
|
+
if (typeof obj === "object") return `{${Object.keys(obj).sort().map((key) => {
|
|
20
|
+
const value = canonicalJson(obj[key]);
|
|
21
|
+
return `${JSON.stringify(key)}:${value}`;
|
|
22
|
+
}).join(",")}}`;
|
|
23
|
+
return "null";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region ../convert/src/array-buffer-to-string.ts
|
|
28
|
+
/**
|
|
29
|
+
* Convert an ArrayBuffer or Uint8Array to a string
|
|
30
|
+
*
|
|
31
|
+
* @param buffer - The ArrayBuffer or Uint8Array to convert
|
|
32
|
+
* @returns The converted string
|
|
33
|
+
*/
|
|
34
|
+
function arrayBufferToString(buffer) {
|
|
35
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
36
|
+
const len = bytes.byteLength;
|
|
37
|
+
if (len < 65535) return String.fromCharCode.apply(null, bytes);
|
|
38
|
+
let binary = "";
|
|
39
|
+
for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i]);
|
|
40
|
+
return binary;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../type-checks/src/is-undefined.ts
|
|
45
|
+
const isUndefined = (value) => {
|
|
46
|
+
return value === void 0;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region ../type-checks/src/is-buffer.ts
|
|
51
|
+
const isBufferExists = typeof Buffer !== "undefined";
|
|
52
|
+
/**
|
|
53
|
+
* Check if the provided value's type is `Buffer`
|
|
54
|
+
*/
|
|
55
|
+
const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
|
|
56
|
+
return false;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region ../type-checks/src/type-detect.ts
|
|
61
|
+
const globalObject = ((Obj) => {
|
|
62
|
+
if (typeof globalThis === "object") return globalThis;
|
|
63
|
+
Object.defineProperty(Obj, "typeDetectGlobalObject", {
|
|
64
|
+
get() {
|
|
65
|
+
return this;
|
|
66
|
+
},
|
|
67
|
+
configurable: true
|
|
68
|
+
});
|
|
69
|
+
return globalThis;
|
|
70
|
+
})(Object.prototype);
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region ../type-checks/src/is-string.ts
|
|
74
|
+
const isString = (value) => {
|
|
75
|
+
try {
|
|
76
|
+
return typeof value === "string";
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region ../convert/src/string-to-uint8-array.ts
|
|
84
|
+
/**
|
|
85
|
+
* Convert a string to Uint8Array
|
|
86
|
+
*
|
|
87
|
+
* @param text - The text to convert
|
|
88
|
+
* @returns The converted Uint8Array
|
|
89
|
+
*/
|
|
90
|
+
function stringToUint8Array(text) {
|
|
91
|
+
return Uint8Array.from([...encodeURIComponent(text)].map((letter) => letter.codePointAt(0)));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Convert a base64 string to a Uint8Array
|
|
95
|
+
*
|
|
96
|
+
* @param data - The base64 string to convert
|
|
97
|
+
* @returns The converted Uint8Array
|
|
98
|
+
*/
|
|
99
|
+
function base64StringToUint8Array(data) {
|
|
100
|
+
return stringToUint8Array(atob(data));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region ../convert/src/string-to-utf8-array.ts
|
|
105
|
+
const encoder = new TextEncoder();
|
|
106
|
+
/**
|
|
107
|
+
* Convert a string to a utf-8 array
|
|
108
|
+
*
|
|
109
|
+
* @param input - The string to convert
|
|
110
|
+
* @returns The converted utf-8 array
|
|
111
|
+
*/
|
|
112
|
+
function stringToUtf8Array(input) {
|
|
113
|
+
return encoder.encode(input);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region ../convert/src/uint8-array-to-stream.ts
|
|
118
|
+
/**
|
|
119
|
+
* Concatenate an array of Uint8Array chunks into a single Uint8Array
|
|
120
|
+
*
|
|
121
|
+
* @param chunks - Array of Uint8Array chunks to concatenate
|
|
122
|
+
* @returns The concatenated Uint8Array
|
|
123
|
+
*/
|
|
124
|
+
function concatUint8Array(chunks) {
|
|
125
|
+
let total = 0;
|
|
126
|
+
for (const chunk of chunks) total += chunk.length;
|
|
127
|
+
const result = new Uint8Array(total);
|
|
128
|
+
let offset = 0;
|
|
129
|
+
for (const chunk of chunks) {
|
|
130
|
+
result.set(chunk, offset);
|
|
131
|
+
offset += chunk.length;
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region ../convert/src/uint8-array-to-string.ts
|
|
138
|
+
/**
|
|
139
|
+
* Convert a Uint8Array to a base64 string
|
|
140
|
+
*
|
|
141
|
+
* @param buffer - The Uint8Array to convert
|
|
142
|
+
* @returns The converted base64 string
|
|
143
|
+
*/
|
|
144
|
+
function uint8ArrayToString(buffer) {
|
|
145
|
+
return btoa(arrayBufferToString(buffer));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region ../convert/src/utf8-array-to-string.ts
|
|
150
|
+
const decoder = new TextDecoder();
|
|
151
|
+
/**
|
|
152
|
+
* Convert a utf-8 array to string
|
|
153
|
+
*
|
|
154
|
+
* @param input - Utf-8 Array
|
|
155
|
+
* @returns The converted string
|
|
156
|
+
*/
|
|
157
|
+
function utf8ArrayToString(input) {
|
|
158
|
+
return decoder.decode(input);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/base-64.ts
|
|
163
|
+
const ENCODE_MAP = new Uint8Array([
|
|
164
|
+
65,
|
|
165
|
+
66,
|
|
166
|
+
67,
|
|
167
|
+
68,
|
|
168
|
+
69,
|
|
169
|
+
70,
|
|
170
|
+
71,
|
|
171
|
+
72,
|
|
172
|
+
73,
|
|
173
|
+
74,
|
|
174
|
+
75,
|
|
175
|
+
76,
|
|
176
|
+
77,
|
|
177
|
+
78,
|
|
178
|
+
79,
|
|
179
|
+
80,
|
|
180
|
+
81,
|
|
181
|
+
82,
|
|
182
|
+
83,
|
|
183
|
+
84,
|
|
184
|
+
85,
|
|
185
|
+
86,
|
|
186
|
+
87,
|
|
187
|
+
88,
|
|
188
|
+
89,
|
|
189
|
+
90,
|
|
190
|
+
97,
|
|
191
|
+
98,
|
|
192
|
+
99,
|
|
193
|
+
100,
|
|
194
|
+
101,
|
|
195
|
+
102,
|
|
196
|
+
103,
|
|
197
|
+
104,
|
|
198
|
+
105,
|
|
199
|
+
106,
|
|
200
|
+
107,
|
|
201
|
+
108,
|
|
202
|
+
109,
|
|
203
|
+
110,
|
|
204
|
+
111,
|
|
205
|
+
112,
|
|
206
|
+
113,
|
|
207
|
+
114,
|
|
208
|
+
115,
|
|
209
|
+
116,
|
|
210
|
+
117,
|
|
211
|
+
118,
|
|
212
|
+
119,
|
|
213
|
+
120,
|
|
214
|
+
121,
|
|
215
|
+
122,
|
|
216
|
+
48,
|
|
217
|
+
49,
|
|
218
|
+
50,
|
|
219
|
+
51,
|
|
220
|
+
52,
|
|
221
|
+
53,
|
|
222
|
+
54,
|
|
223
|
+
55,
|
|
224
|
+
56,
|
|
225
|
+
57,
|
|
226
|
+
43,
|
|
227
|
+
47
|
|
228
|
+
]);
|
|
229
|
+
const ENCODE_PAD = 61;
|
|
230
|
+
const DECODE_MAP$1 = new Uint8Array([
|
|
231
|
+
100,
|
|
232
|
+
100,
|
|
233
|
+
100,
|
|
234
|
+
100,
|
|
235
|
+
100,
|
|
236
|
+
100,
|
|
237
|
+
100,
|
|
238
|
+
100,
|
|
239
|
+
100,
|
|
240
|
+
100,
|
|
241
|
+
100,
|
|
242
|
+
100,
|
|
243
|
+
100,
|
|
244
|
+
100,
|
|
245
|
+
100,
|
|
246
|
+
100,
|
|
247
|
+
100,
|
|
248
|
+
100,
|
|
249
|
+
100,
|
|
250
|
+
100,
|
|
251
|
+
100,
|
|
252
|
+
100,
|
|
253
|
+
100,
|
|
254
|
+
100,
|
|
255
|
+
100,
|
|
256
|
+
100,
|
|
257
|
+
100,
|
|
258
|
+
100,
|
|
259
|
+
100,
|
|
260
|
+
100,
|
|
261
|
+
100,
|
|
262
|
+
100,
|
|
263
|
+
100,
|
|
264
|
+
100,
|
|
265
|
+
100,
|
|
266
|
+
100,
|
|
267
|
+
100,
|
|
268
|
+
100,
|
|
269
|
+
100,
|
|
270
|
+
100,
|
|
271
|
+
100,
|
|
272
|
+
100,
|
|
273
|
+
100,
|
|
274
|
+
62,
|
|
275
|
+
100,
|
|
276
|
+
100,
|
|
277
|
+
100,
|
|
278
|
+
63,
|
|
279
|
+
52,
|
|
280
|
+
53,
|
|
281
|
+
54,
|
|
282
|
+
55,
|
|
283
|
+
56,
|
|
284
|
+
57,
|
|
285
|
+
58,
|
|
286
|
+
59,
|
|
287
|
+
60,
|
|
288
|
+
61,
|
|
289
|
+
100,
|
|
290
|
+
100,
|
|
291
|
+
100,
|
|
292
|
+
100,
|
|
293
|
+
100,
|
|
294
|
+
100,
|
|
295
|
+
100,
|
|
296
|
+
0,
|
|
297
|
+
1,
|
|
298
|
+
2,
|
|
299
|
+
3,
|
|
300
|
+
4,
|
|
301
|
+
5,
|
|
302
|
+
6,
|
|
303
|
+
7,
|
|
304
|
+
8,
|
|
305
|
+
9,
|
|
306
|
+
10,
|
|
307
|
+
11,
|
|
308
|
+
12,
|
|
309
|
+
13,
|
|
310
|
+
14,
|
|
311
|
+
15,
|
|
312
|
+
16,
|
|
313
|
+
17,
|
|
314
|
+
18,
|
|
315
|
+
19,
|
|
316
|
+
20,
|
|
317
|
+
21,
|
|
318
|
+
22,
|
|
319
|
+
23,
|
|
320
|
+
24,
|
|
321
|
+
25,
|
|
322
|
+
100,
|
|
323
|
+
100,
|
|
324
|
+
100,
|
|
325
|
+
100,
|
|
326
|
+
100,
|
|
327
|
+
100,
|
|
328
|
+
26,
|
|
329
|
+
27,
|
|
330
|
+
28,
|
|
331
|
+
29,
|
|
332
|
+
30,
|
|
333
|
+
31,
|
|
334
|
+
32,
|
|
335
|
+
33,
|
|
336
|
+
34,
|
|
337
|
+
35,
|
|
338
|
+
36,
|
|
339
|
+
37,
|
|
340
|
+
38,
|
|
341
|
+
39,
|
|
342
|
+
40,
|
|
343
|
+
41,
|
|
344
|
+
42,
|
|
345
|
+
43,
|
|
346
|
+
44,
|
|
347
|
+
45,
|
|
348
|
+
46,
|
|
349
|
+
47,
|
|
350
|
+
48,
|
|
351
|
+
49,
|
|
352
|
+
50,
|
|
353
|
+
51,
|
|
354
|
+
100,
|
|
355
|
+
100,
|
|
356
|
+
100,
|
|
357
|
+
100,
|
|
358
|
+
100,
|
|
359
|
+
100,
|
|
360
|
+
100,
|
|
361
|
+
100,
|
|
362
|
+
100,
|
|
363
|
+
100,
|
|
364
|
+
100,
|
|
365
|
+
100,
|
|
366
|
+
100,
|
|
367
|
+
100,
|
|
368
|
+
100,
|
|
369
|
+
100,
|
|
370
|
+
100,
|
|
371
|
+
100,
|
|
372
|
+
100,
|
|
373
|
+
100,
|
|
374
|
+
100,
|
|
375
|
+
100,
|
|
376
|
+
100,
|
|
377
|
+
100,
|
|
378
|
+
100,
|
|
379
|
+
100,
|
|
380
|
+
100,
|
|
381
|
+
100,
|
|
382
|
+
100,
|
|
383
|
+
100,
|
|
384
|
+
100,
|
|
385
|
+
100,
|
|
386
|
+
100,
|
|
387
|
+
100,
|
|
388
|
+
100,
|
|
389
|
+
100,
|
|
390
|
+
100,
|
|
391
|
+
100,
|
|
392
|
+
100,
|
|
393
|
+
100,
|
|
394
|
+
100,
|
|
395
|
+
100,
|
|
396
|
+
100,
|
|
397
|
+
100,
|
|
398
|
+
100,
|
|
399
|
+
100,
|
|
400
|
+
100,
|
|
401
|
+
100,
|
|
402
|
+
100,
|
|
403
|
+
100,
|
|
404
|
+
100,
|
|
405
|
+
100,
|
|
406
|
+
100,
|
|
407
|
+
100,
|
|
408
|
+
100,
|
|
409
|
+
100,
|
|
410
|
+
100,
|
|
411
|
+
100,
|
|
412
|
+
100,
|
|
413
|
+
100,
|
|
414
|
+
100,
|
|
415
|
+
100,
|
|
416
|
+
100,
|
|
417
|
+
100,
|
|
418
|
+
100,
|
|
419
|
+
100,
|
|
420
|
+
100,
|
|
421
|
+
100,
|
|
422
|
+
100,
|
|
423
|
+
100,
|
|
424
|
+
100,
|
|
425
|
+
100,
|
|
426
|
+
100,
|
|
427
|
+
100,
|
|
428
|
+
100,
|
|
429
|
+
100,
|
|
430
|
+
100,
|
|
431
|
+
100,
|
|
432
|
+
100,
|
|
433
|
+
100,
|
|
434
|
+
100,
|
|
435
|
+
100,
|
|
436
|
+
100,
|
|
437
|
+
100,
|
|
438
|
+
100,
|
|
439
|
+
100,
|
|
440
|
+
100,
|
|
441
|
+
100,
|
|
442
|
+
100,
|
|
443
|
+
100,
|
|
444
|
+
100,
|
|
445
|
+
100,
|
|
446
|
+
100,
|
|
447
|
+
100,
|
|
448
|
+
100,
|
|
449
|
+
100,
|
|
450
|
+
100,
|
|
451
|
+
100,
|
|
452
|
+
100,
|
|
453
|
+
100,
|
|
454
|
+
100,
|
|
455
|
+
100,
|
|
456
|
+
100,
|
|
457
|
+
100,
|
|
458
|
+
100,
|
|
459
|
+
100,
|
|
460
|
+
100,
|
|
461
|
+
100,
|
|
462
|
+
100,
|
|
463
|
+
100,
|
|
464
|
+
100,
|
|
465
|
+
100,
|
|
466
|
+
100,
|
|
467
|
+
100,
|
|
468
|
+
100,
|
|
469
|
+
100,
|
|
470
|
+
100,
|
|
471
|
+
100,
|
|
472
|
+
100,
|
|
473
|
+
100,
|
|
474
|
+
100,
|
|
475
|
+
100,
|
|
476
|
+
100,
|
|
477
|
+
100,
|
|
478
|
+
100,
|
|
479
|
+
100,
|
|
480
|
+
100,
|
|
481
|
+
100,
|
|
482
|
+
100,
|
|
483
|
+
100,
|
|
484
|
+
100,
|
|
485
|
+
100
|
|
486
|
+
]);
|
|
487
|
+
/**
|
|
488
|
+
* Encodes a Uint8Array into a Base64 encoded Uint8Array.
|
|
489
|
+
*
|
|
490
|
+
* @credit https://github.com/hi-ogawa/js-utils
|
|
491
|
+
*
|
|
492
|
+
* @param input - The input Uint8Array or string to encode.
|
|
493
|
+
* @returns The Base64 encoded Uint8Array.
|
|
494
|
+
*/
|
|
495
|
+
function encodeBase64(input) {
|
|
496
|
+
if (isString(input)) input = stringToUint8Array(input);
|
|
497
|
+
const xLen = input.length;
|
|
498
|
+
const result = new Uint8Array(Math.ceil(xLen / 3) * 4);
|
|
499
|
+
const chunkLen = Math.floor(xLen / 3);
|
|
500
|
+
for (let i$1 = 0; i$1 < chunkLen; i$1++) {
|
|
501
|
+
const chunk = input[3 * i$1 + 0] << 16 | input[3 * i$1 + 1] << 8 | input[3 * i$1 + 2];
|
|
502
|
+
result[4 * i$1 + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
503
|
+
result[4 * i$1 + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
504
|
+
result[4 * i$1 + 2] = ENCODE_MAP[chunk >> 6 & 63];
|
|
505
|
+
result[4 * i$1 + 3] = ENCODE_MAP[chunk >> 0 & 63];
|
|
506
|
+
}
|
|
507
|
+
const i = chunkLen;
|
|
508
|
+
switch (xLen % 3) {
|
|
509
|
+
case 1: {
|
|
510
|
+
const chunk = input[3 * i + 0] << 16;
|
|
511
|
+
result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
512
|
+
result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
513
|
+
result[4 * i + 2] = ENCODE_PAD;
|
|
514
|
+
result[4 * i + 3] = ENCODE_PAD;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
case 2: {
|
|
518
|
+
const chunk = input[3 * i + 0] << 16 | input[3 * i + 1] << 8;
|
|
519
|
+
result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
520
|
+
result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
521
|
+
result[4 * i + 2] = ENCODE_MAP[chunk >> 6 & 63];
|
|
522
|
+
result[4 * i + 3] = ENCODE_PAD;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return utf8ArrayToString(result);
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Decodes a Base64 encoded Uint8Array into a Uint8Array.
|
|
530
|
+
*
|
|
531
|
+
* @credit https://github.com/hi-ogawa/js-utils
|
|
532
|
+
*
|
|
533
|
+
* @param input - The Base64 encoded Uint8Array or string to decode.
|
|
534
|
+
* @returns The decoded Uint8Array.
|
|
535
|
+
*/
|
|
536
|
+
function decodeBase64(input) {
|
|
537
|
+
if (isString(input)) input = stringToUint8Array(input);
|
|
538
|
+
const yLen = input.length;
|
|
539
|
+
if (yLen % 4 !== 0) throw new Error("invalid length");
|
|
540
|
+
let padLen = 0;
|
|
541
|
+
while (padLen < 2 && padLen < input.length && input[input.length - 1 - padLen] === ENCODE_PAD) padLen++;
|
|
542
|
+
for (let i$1 = 0; i$1 < input.length - padLen; i$1++) if (isUndefined(input[i$1]) || isUndefined(DECODE_MAP$1[input[i$1]]) || DECODE_MAP$1[input[i$1]] >= 64) throw new Error("invalid data");
|
|
543
|
+
const chunkLen = Math.floor((yLen - padLen) / 4);
|
|
544
|
+
const xLen = 3 * chunkLen + (3 - padLen) % 3;
|
|
545
|
+
const result = new Uint8Array(xLen);
|
|
546
|
+
for (let i$1 = 0; i$1 < chunkLen; i$1++) {
|
|
547
|
+
const chunk = DECODE_MAP$1[input[4 * i$1 + 0]] << 18 | DECODE_MAP$1[input[4 * i$1 + 1]] << 12 | DECODE_MAP$1[input[4 * i$1 + 2]] << 6 | DECODE_MAP$1[input[4 * i$1 + 3]] << 0;
|
|
548
|
+
result[3 * i$1] = chunk >> 16 & 255;
|
|
549
|
+
result[3 * i$1 + 1] = chunk >> 8 & 255;
|
|
550
|
+
result[3 * i$1 + 2] = chunk >> 0 & 255;
|
|
551
|
+
}
|
|
552
|
+
const i = chunkLen;
|
|
553
|
+
switch (xLen % 3) {
|
|
554
|
+
case 1: {
|
|
555
|
+
const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12;
|
|
556
|
+
result[3 * i] = chunk >> 16 & 255;
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
case 2: {
|
|
560
|
+
const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12 | DECODE_MAP$1[input[4 * i + 2]] << 6;
|
|
561
|
+
result[3 * i] = chunk >> 16 & 255;
|
|
562
|
+
result[3 * i + 1] = chunk >> 8 & 255;
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return result;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
|
|
570
|
+
*
|
|
571
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
|
|
572
|
+
*
|
|
573
|
+
* @param base64 - The Base64 encoded string to convert.
|
|
574
|
+
* @returns The Base64url encoded string.
|
|
575
|
+
*/
|
|
576
|
+
function base64UrlEncode(base64) {
|
|
577
|
+
return btoa(String.fromCharCode(...base64)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
|
|
581
|
+
*
|
|
582
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
|
|
583
|
+
*
|
|
584
|
+
* @param base64url - The Base64url encoded string to convert.
|
|
585
|
+
* @returns The Base64 encoded string.
|
|
586
|
+
*/
|
|
587
|
+
function base64UrlDecode(base64url) {
|
|
588
|
+
const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
589
|
+
return new Uint8Array([...atob(base64 + "=".repeat((4 - base64.length % 4) % 4))].map((c) => c.charCodeAt(0)));
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/ed25519.ts
|
|
594
|
+
/**
|
|
595
|
+
* Generates an Ed25519 key pair using Cloudflare Workers' `NODE-ED25519` algorithm and returns the public key as a base64 string, the private key as a JWK object, and a key ID derived from the public key. The private key is returned as a JWK for compatibility with Web Crypto API operations.
|
|
596
|
+
*
|
|
597
|
+
* @remarks
|
|
598
|
+
* This function uses Cloudflare Workers' `NODE-ED25519` algorithm parameters, which are specific to Cloudflare Workers' implementation of Ed25519. The returned key pair is structured as follows:
|
|
599
|
+
* - The `publicKey` is returned as a base64url-encoded string of the raw public key bytes (the 'x' parameter from the JWK).
|
|
600
|
+
* - The `privateKeyJwk` is returned as a JWK object containing the private key parameters, which can be used for signing operations with the Web Crypto API.
|
|
601
|
+
* - The `keyId` is generated by hashing the raw public key bytes and taking the first 4 bytes of the hash, encoded as a hex string prefixed with "ed25519:". This provides a unique identifier for the key pair based on its public key.
|
|
602
|
+
*
|
|
603
|
+
* This function is not compatible with standard Web Crypto API Ed25519 implementations in other environments. The legacy function `generateSigningKeyPairLegacy` is also available for backwards compatibility, which returns the private key as a JSON stringified JWK. However, it is recommended to use this structured version for new code.
|
|
604
|
+
*
|
|
605
|
+
* @returns An object containing the `publicKey` as a base64 string, the `privateKeyJwk` as a JWK object, and a `keyId` derived from the public key.
|
|
606
|
+
* @throws {@link DOMException} If key generation or export fails in the Web Crypto API.
|
|
607
|
+
*/
|
|
608
|
+
async function generateSigningKeyPair() {
|
|
609
|
+
const keyPair = await crypto.subtle.generateKey({
|
|
610
|
+
name: "NODE-ED25519",
|
|
611
|
+
namedCurve: "NODE-ED25519"
|
|
612
|
+
}, true, ["sign", "verify"]);
|
|
613
|
+
const publicKeyJwk = await crypto.subtle.exportKey("jwk", keyPair.publicKey);
|
|
614
|
+
const privateKeyJwk = await crypto.subtle.exportKey("jwk", keyPair.privateKey);
|
|
615
|
+
const publicKeyBytes = base64UrlDecode(publicKeyJwk.x);
|
|
616
|
+
const keyIdHash = new Uint8Array(await crypto.subtle.digest("SHA-256", publicKeyBytes)).slice(0, 4);
|
|
617
|
+
const keyId = `ed25519:${Array.from(keyIdHash).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
618
|
+
return {
|
|
619
|
+
publicKey: base64UrlEncode(publicKeyBytes),
|
|
620
|
+
privateKeyJwk,
|
|
621
|
+
keyId
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Generates an Ed25519 key pair and returns the public key as a base64 string, the private key as a JSON stringified JWK, and a key ID derived from the public key. The private key is returned in a legacy format for compatibility with existing code that expects a JSON string.
|
|
626
|
+
*
|
|
627
|
+
* @remarks
|
|
628
|
+
* Legacy function for backwards compatibility during migration. Returns the old format but with a proper key.
|
|
629
|
+
*
|
|
630
|
+
* @deprecated Use `generateSigningKeyPair` instead, which returns a structured key object and separate key ID. This legacy function is retained for backwards compatibility but may be removed in future releases.
|
|
631
|
+
*
|
|
632
|
+
* @returns An object containing the `publicKey` as a base64 string, the `privateKey` as a JSON stringified JWK, and a `keyId` derived from the public key.
|
|
633
|
+
* @throws {@link SyntaxError} If the generated private key cannot be stringified to JSON (should not occur under normal circumstances).
|
|
634
|
+
*/
|
|
635
|
+
async function generateSigningKeyPairLegacy() {
|
|
636
|
+
const { publicKey, privateKeyJwk, keyId } = await generateSigningKeyPair();
|
|
637
|
+
return {
|
|
638
|
+
publicKey,
|
|
639
|
+
privateKey: JSON.stringify(privateKeyJwk),
|
|
640
|
+
keyId
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Creates and attaches an [Ed25519 signature](https://matrix.org/docs/spec/client_server/latest#signing-json-objects) for a JSON object using [Matrix-style](https://matrix.org/docs/spec/client_server/latest#signing-json-objects) signing rules.
|
|
645
|
+
*
|
|
646
|
+
* @see https://matrix.org/docs/spec/client_server/latest#signing-json-objects
|
|
647
|
+
*
|
|
648
|
+
* @remarks
|
|
649
|
+
* - Signature output is encoded as un-padded base64url.
|
|
650
|
+
* - The original `obj` is not mutated.
|
|
651
|
+
* - Uses `NODE-ED25519` algorithm parameters for key import/sign operations.
|
|
652
|
+
*
|
|
653
|
+
* The function canonicalizes a copy of the input object after removing the `signatures`
|
|
654
|
+
* and `unsigned` properties, signs that canonical JSON payload with the provided private key,
|
|
655
|
+
* and returns a new object with the generated signature merged into `obj.signatures`.
|
|
656
|
+
*
|
|
657
|
+
* Existing signatures are preserved, including other keys under the same `serverName`.
|
|
658
|
+
*
|
|
659
|
+
* @param obj - The JSON object to sign.
|
|
660
|
+
* @param serverName - The signing entity name used as the top-level key in `signatures`.
|
|
661
|
+
* @param keyId - The key identifier used under `signatures[serverName]`.
|
|
662
|
+
* @param privateKeyJwk - The Ed25519 private key as a JWK object, or a JSON stringified JWK (legacy compatibility).
|
|
663
|
+
* @returns A new object containing all original fields plus an updated `signatures` map with the new signature.
|
|
664
|
+
* @throws {@link SyntaxError} If `privateKeyJwk` is a string that is not valid JSON.
|
|
665
|
+
* @throws {@link DOMException} If key import or signing fails in the Web Crypto API.
|
|
666
|
+
*/
|
|
667
|
+
async function signJson(obj, serverName, keyId, privateKeyJwk) {
|
|
668
|
+
const jwk = typeof privateKeyJwk === "string" ? JSON.parse(privateKeyJwk) : privateKeyJwk;
|
|
669
|
+
const toSign = { ...obj };
|
|
670
|
+
delete toSign.signatures;
|
|
671
|
+
delete toSign.unsigned;
|
|
672
|
+
const privateKey = await crypto.subtle.importKey("jwk", jwk, {
|
|
673
|
+
name: "NODE-ED25519",
|
|
674
|
+
namedCurve: "NODE-ED25519"
|
|
675
|
+
}, false, ["sign"]);
|
|
676
|
+
const canonical = canonicalJson(toSign);
|
|
677
|
+
const signatureBytes = await crypto.subtle.sign({ name: "NODE-ED25519" }, privateKey, new TextEncoder().encode(canonical));
|
|
678
|
+
const signatureB64 = base64UrlEncode(new Uint8Array(signatureBytes));
|
|
679
|
+
const existingSignatures = obj.signatures ?? {};
|
|
680
|
+
return {
|
|
681
|
+
...obj,
|
|
682
|
+
signatures: {
|
|
683
|
+
...existingSignatures,
|
|
684
|
+
[serverName]: {
|
|
685
|
+
...existingSignatures[serverName] ?? {},
|
|
686
|
+
[keyId]: signatureB64
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Verifies an Ed25519 signature on a JSON object using [Matrix-style signing rules](https://matrix.org/docs/spec/client_server/latest#signing-json-objects). The function extracts the relevant signature from the `signatures` property of the input object, removes the `signatures` and `unsigned` properties to create a canonical JSON payload, and then verifies the signature against the provided public key. The public key is expected to be a base64url-encoded string of the raw public key bytes (the 'x' parameter from the JWK). The function returns `true` if the signature is valid and `false` otherwise. Any errors during the verification process are caught and logged, with a return value of `false` in case of failure.
|
|
693
|
+
*
|
|
694
|
+
* @see https://matrix.org/docs/spec/client_server/latest#signing-json-objects
|
|
695
|
+
*
|
|
696
|
+
* @remarks
|
|
697
|
+
* - Signature input is expected to be un-padded base64url.
|
|
698
|
+
* - The original `obj` is not mutated.
|
|
699
|
+
* - Uses `NODE-ED25519` algorithm parameters for key import/verify operations.
|
|
700
|
+
* - The function does not throw on verification failure; it returns `false` instead. Errors during key import or verification are logged to the console for debugging purposes.
|
|
701
|
+
*
|
|
702
|
+
* @param obj - The JSON object containing the signature to verify.
|
|
703
|
+
* @param serverName - The signing entity name used as the top-level key in `signatures`.
|
|
704
|
+
* @param keyId - The key identifier used under `signatures[serverName]` to locate the specific signature to verify.
|
|
705
|
+
* @param publicKeyB64 - The Ed25519 public key as a base64url-encoded string of the raw public key bytes (the 'x' parameter from the JWK).
|
|
706
|
+
* @returns A boolean indicating whether the signature is valid (`true`) or not (`false`). Returns `false` if the signature is missing, invalid, or if any errors occur during the verification process.
|
|
707
|
+
*/
|
|
708
|
+
async function verifySignature(obj, serverName, keyId, publicKeyB64) {
|
|
709
|
+
try {
|
|
710
|
+
const signature = obj.signatures?.[serverName]?.[keyId];
|
|
711
|
+
if (!signature) return false;
|
|
712
|
+
const toVerify = { ...obj };
|
|
713
|
+
delete toVerify.signatures;
|
|
714
|
+
delete toVerify.unsigned;
|
|
715
|
+
const publicKeyBytes = base64UrlDecode(publicKeyB64);
|
|
716
|
+
const publicKey = await crypto.subtle.importKey("raw", publicKeyBytes, {
|
|
717
|
+
name: "NODE-ED25519",
|
|
718
|
+
namedCurve: "NODE-ED25519"
|
|
719
|
+
}, false, ["verify"]);
|
|
720
|
+
const signatureBytes = base64UrlDecode(signature);
|
|
721
|
+
const canonical = canonicalJson(toVerify);
|
|
722
|
+
return await crypto.subtle.verify({ name: "NODE-ED25519" }, publicKey, signatureBytes, new TextEncoder().encode(canonical));
|
|
723
|
+
} catch (error) {
|
|
724
|
+
console.error("Signature verification failed:", error);
|
|
725
|
+
return false;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
//#endregion
|
|
730
|
+
//#region src/hex.ts
|
|
731
|
+
const ALPHABET = "0123456789ABCDEF";
|
|
732
|
+
const DECODE_MAP = {
|
|
733
|
+
"0": 0,
|
|
734
|
+
"1": 1,
|
|
735
|
+
"2": 2,
|
|
736
|
+
"3": 3,
|
|
737
|
+
"4": 4,
|
|
738
|
+
"5": 5,
|
|
739
|
+
"6": 6,
|
|
740
|
+
"7": 7,
|
|
741
|
+
"8": 8,
|
|
742
|
+
"9": 9,
|
|
743
|
+
a: 10,
|
|
744
|
+
A: 10,
|
|
745
|
+
b: 11,
|
|
746
|
+
B: 11,
|
|
747
|
+
c: 12,
|
|
748
|
+
C: 12,
|
|
749
|
+
d: 13,
|
|
750
|
+
D: 13,
|
|
751
|
+
e: 14,
|
|
752
|
+
E: 14,
|
|
753
|
+
f: 15,
|
|
754
|
+
F: 15
|
|
755
|
+
};
|
|
756
|
+
/**
|
|
757
|
+
* Encodes a Uint8Array into a hexadecimal string.
|
|
758
|
+
*
|
|
759
|
+
* @param input - The input Uint8Array.
|
|
760
|
+
* @returns The hexadecimal string.
|
|
761
|
+
*/
|
|
762
|
+
function encodeHex(input) {
|
|
763
|
+
let result = "";
|
|
764
|
+
for (let i = 0; i < input.length; i++) {
|
|
765
|
+
result += ALPHABET[input[i] >> 4];
|
|
766
|
+
result += ALPHABET[input[i] & 15];
|
|
767
|
+
}
|
|
768
|
+
return result;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Encodes a Uint8Array into an uppercase hexadecimal string.
|
|
772
|
+
*
|
|
773
|
+
* @param input - The input Uint8Array.
|
|
774
|
+
* @returns The uppercase hexadecimal string.
|
|
775
|
+
*/
|
|
776
|
+
function decodeHex(input) {
|
|
777
|
+
if (input.length % 2 !== 0) throw new Error("Invalid hex string");
|
|
778
|
+
const result = new Uint8Array(input.length / 2);
|
|
779
|
+
for (let i = 0; i < input.length; i += 2) {
|
|
780
|
+
if (!(input[i] in DECODE_MAP)) throw new Error("Invalid character");
|
|
781
|
+
if (!(input[i + 1] in DECODE_MAP)) throw new Error("Invalid character");
|
|
782
|
+
result[i / 2] |= DECODE_MAP[input[i]] << 4;
|
|
783
|
+
result[i / 2] |= DECODE_MAP[input[i + 1]];
|
|
784
|
+
}
|
|
785
|
+
return result;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
//#endregion
|
|
789
|
+
//#region src/encryption.ts
|
|
790
|
+
/**
|
|
791
|
+
* Creates a CryptoKey object that can be used to encrypt any string.
|
|
792
|
+
*
|
|
793
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
|
|
794
|
+
*
|
|
795
|
+
* @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
|
|
796
|
+
*/
|
|
797
|
+
async function createKey() {
|
|
798
|
+
return crypto.subtle.generateKey({
|
|
799
|
+
name: "AES-GCM",
|
|
800
|
+
length: 256
|
|
801
|
+
}, true, ["encrypt", "decrypt"]);
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
|
|
805
|
+
*
|
|
806
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
|
|
807
|
+
*
|
|
808
|
+
* @param key - The CryptoKey to encode
|
|
809
|
+
* @returns A promise that resolves to a base64 string representing the key
|
|
810
|
+
*/
|
|
811
|
+
async function encodeKey(key) {
|
|
812
|
+
const exported = await crypto.subtle.exportKey("raw", key);
|
|
813
|
+
return encodeBase64(new Uint8Array(exported)).toString();
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Decodes a base64 string into bytes and then imports the key.
|
|
817
|
+
*
|
|
818
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
|
|
819
|
+
*
|
|
820
|
+
* @param encoded - The base64 encoded key
|
|
821
|
+
* @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
|
|
822
|
+
*/
|
|
823
|
+
async function decodeKey(encoded) {
|
|
824
|
+
const bytes = decodeBase64(encoded);
|
|
825
|
+
return crypto.subtle.importKey("raw", bytes.buffer, "AES-GCM", true, ["encrypt", "decrypt"]);
|
|
826
|
+
}
|
|
827
|
+
const IV_LENGTH = 24;
|
|
828
|
+
/**
|
|
829
|
+
* Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
|
|
830
|
+
*
|
|
831
|
+
* @remarks
|
|
832
|
+
* The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.
|
|
833
|
+
*
|
|
834
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
|
|
835
|
+
*
|
|
836
|
+
* @param key - The CryptoKey to use for encryption
|
|
837
|
+
* @param plaintext - The plaintext string to encrypt
|
|
838
|
+
* @returns A promise that resolves to a base64 string representing the encrypted data
|
|
839
|
+
*/
|
|
840
|
+
async function encrypt(key, plaintext) {
|
|
841
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));
|
|
842
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
843
|
+
name: "AES-GCM",
|
|
844
|
+
iv
|
|
845
|
+
}, key, stringToUtf8Array(plaintext));
|
|
846
|
+
return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
|
|
850
|
+
*
|
|
851
|
+
* @remarks
|
|
852
|
+
* The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.
|
|
853
|
+
*
|
|
854
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
|
|
855
|
+
*
|
|
856
|
+
* @param key - The CryptoKey to use for decryption
|
|
857
|
+
* @param encrypted - The encrypted base64 encoded string to decrypt
|
|
858
|
+
* @returns A promise that resolves to the decrypted string
|
|
859
|
+
*/
|
|
860
|
+
async function decrypt(key, encrypted) {
|
|
861
|
+
return utf8ArrayToString(await crypto.subtle.decrypt({
|
|
862
|
+
name: "AES-GCM",
|
|
863
|
+
iv: decodeHex(encrypted.slice(0, IV_LENGTH))
|
|
864
|
+
}, key, decodeBase64(encrypted.slice(IV_LENGTH))));
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Encrypts a buffer using AES-GCM with a given CryptoKey.
|
|
868
|
+
*
|
|
869
|
+
* @remarks
|
|
870
|
+
* The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.
|
|
871
|
+
*
|
|
872
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
|
|
873
|
+
*
|
|
874
|
+
* @param key - The CryptoKey to use for encryption
|
|
875
|
+
* @param buffer - The buffer to encrypt
|
|
876
|
+
* @returns A promise that resolves to a base64 string representing the encrypted data
|
|
877
|
+
*/
|
|
878
|
+
async function encryptBuffer(key, buffer) {
|
|
879
|
+
const iv = crypto.getRandomValues(new Uint8Array(16));
|
|
880
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
881
|
+
name: "AES-GCM",
|
|
882
|
+
iv
|
|
883
|
+
}, key, buffer);
|
|
884
|
+
return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Decrypts a buffer using AES-GCM with a given CryptoKey.
|
|
888
|
+
*
|
|
889
|
+
* @remarks
|
|
890
|
+
* The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.
|
|
891
|
+
*
|
|
892
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
|
|
893
|
+
*
|
|
894
|
+
* @param key - The CryptoKey to use for decryption
|
|
895
|
+
* @param encrypted - The encrypted base64 encoded string to decrypt
|
|
896
|
+
* @returns A promise that resolves to the decrypted string
|
|
897
|
+
*/
|
|
898
|
+
async function decryptBuffer(key, encrypted) {
|
|
899
|
+
const concatenated = base64StringToUint8Array(encrypted);
|
|
900
|
+
return crypto.subtle.decrypt({
|
|
901
|
+
name: "AES-GCM",
|
|
902
|
+
iv: concatenated.slice(0, 16)
|
|
903
|
+
}, key, concatenated.slice(16));
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
//#endregion
|
|
907
|
+
//#region src/random.ts
|
|
908
|
+
/**
|
|
909
|
+
* Generate a random byte array of the specified length using the Web Crypto API.
|
|
910
|
+
*
|
|
911
|
+
* @param length - The length of the random byte array to generate (default is 32 bytes)
|
|
912
|
+
* @returns A Uint8Array containing random bytes of the specified length
|
|
913
|
+
*/
|
|
914
|
+
function generateRandomBytes(length = 32) {
|
|
915
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Generate a random string of the specified length using characters A-Z, a-z, and 0-9 for CSRF tokens, etc.
|
|
919
|
+
*
|
|
920
|
+
* @remarks
|
|
921
|
+
* This function uses the Web Crypto API's `crypto.getRandomValues` to generate secure random bytes,
|
|
922
|
+
* and then maps those bytes to characters in the specified character set. It uses rejection sampling
|
|
923
|
+
* to ensure a uniform distribution of characters without modulo bias.
|
|
924
|
+
*
|
|
925
|
+
* @param length - The length of the random string to generate (default is 32 characters)
|
|
926
|
+
* @returns A random string of the specified length
|
|
927
|
+
*/
|
|
928
|
+
function generateRandomString(length = 32) {
|
|
929
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
930
|
+
const charsLen = 62;
|
|
931
|
+
const maxValid = 256 - 256 % charsLen;
|
|
932
|
+
const result = [];
|
|
933
|
+
while (result.length < length) {
|
|
934
|
+
const bytes = generateRandomBytes(length - result.length);
|
|
935
|
+
for (const b of bytes) if (b < maxValid && result.length < length && chars[b % charsLen]) result.push(chars[b % charsLen]);
|
|
936
|
+
}
|
|
937
|
+
return result.join("");
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
//#endregion
|
|
941
|
+
export { base64UrlDecode, base64UrlEncode, createKey, decodeBase64, decodeHex, decodeKey, decrypt, decryptBuffer, encodeBase64, encodeHex, encodeKey, encrypt, encryptBuffer, generateRandomBytes, generateRandomString, generateSigningKeyPair, generateSigningKeyPairLegacy, signJson, verifySignature };
|
|
942
|
+
//# sourceMappingURL=cloudflare.mjs.map
|