@jscrypto/core 0.1.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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/blocks.d.ts +9 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +34 -0
- package/dist/blocks.js.map +1 -0
- package/dist/bytes.d.ts +5 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +36 -0
- package/dist/bytes.js.map +1 -0
- package/dist/component.d.ts +67 -0
- package/dist/component.d.ts.map +1 -0
- package/dist/component.js +2 -0
- package/dist/component.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.cjs +590 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +552 -0
- package/dist/index.mjs.map +7 -0
- package/dist/jscrypto-core.iife.js +576 -0
- package/dist/jscrypto-core.iife.js.map +7 -0
- package/dist/jscrypto-core.iife.min.js +7 -0
- package/dist/jscrypto-core.iife.min.js.map +7 -0
- package/dist/jscrypto-core.umd.js +587 -0
- package/dist/jscrypto-core.umd.js.map +7 -0
- package/dist/jscrypto-core.umd.min.js +18 -0
- package/dist/jscrypto-core.umd.min.js.map +7 -0
- package/dist/passphrase.d.ts +31 -0
- package/dist/passphrase.d.ts.map +1 -0
- package/dist/passphrase.js +222 -0
- package/dist/passphrase.js.map +1 -0
- package/dist/registry.d.ts +28 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +73 -0
- package/dist/registry.js.map +1 -0
- package/dist/transform.d.ts +13 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +133 -0
- package/dist/transform.js.map +1 -0
- package/package.json +40 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @jscrypto/core v0.1.0
|
|
3
|
+
* Copyright 2026 Chen, Yi-Cyuan
|
|
4
|
+
* Released under the MIT license
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
|
|
25
|
+
// packages/core/src/index.ts
|
|
26
|
+
var index_exports = {};
|
|
27
|
+
__export(index_exports, {
|
|
28
|
+
CryptoError: () => CryptoError,
|
|
29
|
+
DuplicateComponentError: () => DuplicateComponentError,
|
|
30
|
+
MissingComponentError: () => MissingComponentError,
|
|
31
|
+
assertBlockMultiple: () => assertBlockMultiple,
|
|
32
|
+
assertBlockSize: () => assertBlockSize,
|
|
33
|
+
assertBytes: () => assertBytes,
|
|
34
|
+
assertIv: () => assertIv,
|
|
35
|
+
assertPaddedInput: () => assertPaddedInput,
|
|
36
|
+
concatBytes: () => concatBytes,
|
|
37
|
+
createRegistry: () => createRegistry,
|
|
38
|
+
equalBytes: () => equalBytes,
|
|
39
|
+
getBlockPaddingLength: () => getBlockPaddingLength,
|
|
40
|
+
xorBytes: () => xorBytes
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(index_exports);
|
|
43
|
+
|
|
44
|
+
// packages/core/src/errors.ts
|
|
45
|
+
var CryptoError = class extends Error {
|
|
46
|
+
constructor(message) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.name = new.target.name;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var DuplicateComponentError = class extends CryptoError {
|
|
52
|
+
constructor(kind, name) {
|
|
53
|
+
super(`Component already registered: ${kind}:${name}`);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var MissingComponentError = class extends CryptoError {
|
|
57
|
+
constructor(kind, name) {
|
|
58
|
+
super(`Component not found: ${kind}:${name}`);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// packages/core/src/bytes.ts
|
|
63
|
+
function concatBytes(...chunks) {
|
|
64
|
+
const length = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
65
|
+
const output = new Uint8Array(length);
|
|
66
|
+
let offset = 0;
|
|
67
|
+
for (const chunk of chunks) {
|
|
68
|
+
output.set(chunk, offset);
|
|
69
|
+
offset += chunk.length;
|
|
70
|
+
}
|
|
71
|
+
return output;
|
|
72
|
+
}
|
|
73
|
+
function equalBytes(a, b) {
|
|
74
|
+
if (a.length !== b.length) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
let diff = 0;
|
|
78
|
+
for (let i = 0; i < a.length; i++) {
|
|
79
|
+
diff |= a[i] ^ b[i];
|
|
80
|
+
}
|
|
81
|
+
return diff === 0;
|
|
82
|
+
}
|
|
83
|
+
function xorBytes(a, b) {
|
|
84
|
+
if (a.length !== b.length) {
|
|
85
|
+
throw new Error("XOR inputs must have the same length.");
|
|
86
|
+
}
|
|
87
|
+
const output = new Uint8Array(a.length);
|
|
88
|
+
for (let i = 0; i < a.length; i++) {
|
|
89
|
+
output[i] = a[i] ^ b[i];
|
|
90
|
+
}
|
|
91
|
+
return output;
|
|
92
|
+
}
|
|
93
|
+
function assertBytes(value, name) {
|
|
94
|
+
if (!(value instanceof Uint8Array)) {
|
|
95
|
+
throw new TypeError(`${name} must be a Uint8Array.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// packages/core/src/passphrase.ts
|
|
100
|
+
function createPassphraseCipher(registry, options) {
|
|
101
|
+
return {
|
|
102
|
+
encrypt(plaintext) {
|
|
103
|
+
return concatBytes(this.createEncryptor().finalize(plaintext));
|
|
104
|
+
},
|
|
105
|
+
decrypt(input) {
|
|
106
|
+
return concatBytes(this.createDecryptor().finalize(input));
|
|
107
|
+
},
|
|
108
|
+
createEncryptor() {
|
|
109
|
+
return createPassphraseEncryptor(registry, options);
|
|
110
|
+
},
|
|
111
|
+
createDecryptor() {
|
|
112
|
+
return createPassphraseDecryptor(registry, options);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function createPassphraseEncryptor(registry, options) {
|
|
117
|
+
const format = resolveFormat(registry, options);
|
|
118
|
+
const salt = options.salt ? options.salt.slice() : randomBytes(options.saltSize ?? 8);
|
|
119
|
+
const { key, iv } = deriveKeyIv(registry, options, salt);
|
|
120
|
+
const encryptor = registry.createCipher(toTransformOptions(options, key, iv)).createEncryptor();
|
|
121
|
+
if (!format) {
|
|
122
|
+
return encryptor;
|
|
123
|
+
}
|
|
124
|
+
if (!isStreamingOpenSslFormat(format)) {
|
|
125
|
+
return createBufferedFormatEncryptor(format, salt, encryptor);
|
|
126
|
+
}
|
|
127
|
+
let emittedHeader = false;
|
|
128
|
+
const emitHeader = () => {
|
|
129
|
+
if (emittedHeader) {
|
|
130
|
+
return new Uint8Array();
|
|
131
|
+
}
|
|
132
|
+
emittedHeader = true;
|
|
133
|
+
return format.stringify({ ciphertext: new Uint8Array(), salt });
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
process(input) {
|
|
137
|
+
return concatBytes(emitHeader(), encryptor.process(input));
|
|
138
|
+
},
|
|
139
|
+
finalize(input = new Uint8Array()) {
|
|
140
|
+
return concatBytes(emitHeader(), encryptor.finalize(input));
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function createPassphraseDecryptor(registry, options) {
|
|
145
|
+
const format = resolveFormat(registry, options);
|
|
146
|
+
if (!format) {
|
|
147
|
+
const { key, iv } = deriveKeyIv(registry, options, new Uint8Array());
|
|
148
|
+
return registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
|
|
149
|
+
}
|
|
150
|
+
if (!isStreamingOpenSslFormat(format)) {
|
|
151
|
+
return createBufferedFormatDecryptor(registry, options, format);
|
|
152
|
+
}
|
|
153
|
+
let header = new Uint8Array();
|
|
154
|
+
let decryptor;
|
|
155
|
+
const initDecryptor = (input) => {
|
|
156
|
+
if (decryptor) {
|
|
157
|
+
return input;
|
|
158
|
+
}
|
|
159
|
+
header = new Uint8Array(concatBytes(header, input));
|
|
160
|
+
if (header.length < 16) {
|
|
161
|
+
return new Uint8Array();
|
|
162
|
+
}
|
|
163
|
+
const parsed = format.parse(header.slice(0, 16));
|
|
164
|
+
const hasSalt = parsed.salt !== void 0;
|
|
165
|
+
const salt = parsed.salt ?? new Uint8Array();
|
|
166
|
+
const ciphertext = hasSalt ? concatBytes(parsed.ciphertext, header.slice(16)) : header;
|
|
167
|
+
const { key, iv } = deriveKeyIv(registry, options, salt);
|
|
168
|
+
decryptor = registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
|
|
169
|
+
header = new Uint8Array();
|
|
170
|
+
return ciphertext;
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
process(input) {
|
|
174
|
+
const ciphertext = initDecryptor(input);
|
|
175
|
+
return decryptor ? decryptor.process(ciphertext) : new Uint8Array();
|
|
176
|
+
},
|
|
177
|
+
finalize(input = new Uint8Array()) {
|
|
178
|
+
const ciphertext = initDecryptor(input);
|
|
179
|
+
if (!decryptor) {
|
|
180
|
+
const { key, iv } = deriveKeyIv(registry, options, new Uint8Array());
|
|
181
|
+
decryptor = registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
|
|
182
|
+
const buffered = header;
|
|
183
|
+
header = new Uint8Array();
|
|
184
|
+
return decryptor.finalize(buffered);
|
|
185
|
+
}
|
|
186
|
+
return decryptor.finalize(ciphertext);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function createBufferedFormatEncryptor(format, salt, encryptor) {
|
|
191
|
+
const chunks = [];
|
|
192
|
+
return {
|
|
193
|
+
process(input) {
|
|
194
|
+
const output = encryptor.process(input);
|
|
195
|
+
if (output.length !== 0) {
|
|
196
|
+
chunks.push(output);
|
|
197
|
+
}
|
|
198
|
+
return new Uint8Array();
|
|
199
|
+
},
|
|
200
|
+
finalize(input = new Uint8Array()) {
|
|
201
|
+
const output = encryptor.finalize(input);
|
|
202
|
+
if (output.length !== 0) {
|
|
203
|
+
chunks.push(output);
|
|
204
|
+
}
|
|
205
|
+
return format.stringify({ ciphertext: concatBytes(...chunks), salt });
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function createBufferedFormatDecryptor(registry, options, format) {
|
|
210
|
+
const chunks = [];
|
|
211
|
+
return {
|
|
212
|
+
process(input) {
|
|
213
|
+
if (input.length !== 0) {
|
|
214
|
+
chunks.push(input);
|
|
215
|
+
}
|
|
216
|
+
return new Uint8Array();
|
|
217
|
+
},
|
|
218
|
+
finalize(input = new Uint8Array()) {
|
|
219
|
+
if (input.length !== 0) {
|
|
220
|
+
chunks.push(input);
|
|
221
|
+
}
|
|
222
|
+
const parsed = format.parse(concatBytes(...chunks));
|
|
223
|
+
const { key, iv } = deriveKeyIv(registry, options, parsed.salt ?? new Uint8Array());
|
|
224
|
+
return registry.createCipher(toTransformOptions(options, key, iv)).decrypt(parsed.ciphertext);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function deriveKeyIv(registry, options, salt) {
|
|
229
|
+
const keySize = resolveKeySize(registry, options);
|
|
230
|
+
const ivSize = resolveIvSize(registry, options);
|
|
231
|
+
const derived = deriveSync(registry, options, salt, keySize + ivSize);
|
|
232
|
+
return {
|
|
233
|
+
key: derived.slice(0, keySize),
|
|
234
|
+
iv: ivSize === 0 ? void 0 : derived.slice(keySize, keySize + ivSize)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function deriveSync(registry, options, salt, length) {
|
|
238
|
+
const kdfOptions = normalizeNamedOptions(options.kdf);
|
|
239
|
+
const kdf = registry.get("kdf", kdfOptions.name);
|
|
240
|
+
const result = kdf.derive({
|
|
241
|
+
...withoutName(kdfOptions),
|
|
242
|
+
passphrase: options.passphrase,
|
|
243
|
+
salt,
|
|
244
|
+
length
|
|
245
|
+
});
|
|
246
|
+
if (result instanceof Promise) {
|
|
247
|
+
throw new Error(`KDF ${kdfOptions.name} is asynchronous; use an async passphrase cipher API.`);
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
function toTransformOptions(options, key, iv) {
|
|
252
|
+
const {
|
|
253
|
+
passphrase,
|
|
254
|
+
kdf,
|
|
255
|
+
format,
|
|
256
|
+
salt,
|
|
257
|
+
saltSize,
|
|
258
|
+
keySize,
|
|
259
|
+
ivSize,
|
|
260
|
+
...transformOptions
|
|
261
|
+
} = options;
|
|
262
|
+
return {
|
|
263
|
+
...transformOptions,
|
|
264
|
+
key,
|
|
265
|
+
...iv ? { iv } : {}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function resolveKeySize(registry, options) {
|
|
269
|
+
if (options.keySize !== void 0) {
|
|
270
|
+
assertPositiveInteger(options.keySize, "keySize");
|
|
271
|
+
return options.keySize;
|
|
272
|
+
}
|
|
273
|
+
const cipher = registry.get("cipher", options.cipher);
|
|
274
|
+
if (!cipher.keySizes || cipher.keySizes.length === 0) {
|
|
275
|
+
throw new Error(`${options.cipher} passphrase cipher requires keySize.`);
|
|
276
|
+
}
|
|
277
|
+
return Math.max(...cipher.keySizes);
|
|
278
|
+
}
|
|
279
|
+
function resolveIvSize(registry, options) {
|
|
280
|
+
if (options.ivSize !== void 0) {
|
|
281
|
+
assertNonNegativeInteger(options.ivSize, "ivSize");
|
|
282
|
+
return options.ivSize;
|
|
283
|
+
}
|
|
284
|
+
const cipher = registry.get("cipher", options.cipher);
|
|
285
|
+
return cipher.type === "block" ? cipher.blockSize : 0;
|
|
286
|
+
}
|
|
287
|
+
function resolveFormat(registry, options) {
|
|
288
|
+
if (!options.format) {
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
return registry.get("format", normalizeNamedOptions(options.format).name);
|
|
292
|
+
}
|
|
293
|
+
function isStreamingOpenSslFormat(format) {
|
|
294
|
+
return format.name === "OpenSSL";
|
|
295
|
+
}
|
|
296
|
+
function normalizeNamedOptions(options) {
|
|
297
|
+
return typeof options === "string" ? { name: options } : options;
|
|
298
|
+
}
|
|
299
|
+
function withoutName(options) {
|
|
300
|
+
const { name, ...rest } = options;
|
|
301
|
+
return rest;
|
|
302
|
+
}
|
|
303
|
+
function randomBytes(length) {
|
|
304
|
+
assertNonNegativeInteger(length, "saltSize");
|
|
305
|
+
const bytes = new Uint8Array(length);
|
|
306
|
+
if (bytes.length === 0) {
|
|
307
|
+
return bytes;
|
|
308
|
+
}
|
|
309
|
+
const crypto = globalThis;
|
|
310
|
+
crypto.crypto?.getRandomValues(bytes);
|
|
311
|
+
if (bytes.some((byte) => byte !== 0)) {
|
|
312
|
+
return bytes;
|
|
313
|
+
}
|
|
314
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
315
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
316
|
+
}
|
|
317
|
+
return bytes;
|
|
318
|
+
}
|
|
319
|
+
function assertPositiveInteger(value, label) {
|
|
320
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
321
|
+
throw new RangeError(`${label} must be a positive integer.`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function assertNonNegativeInteger(value, label) {
|
|
325
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
326
|
+
throw new RangeError(`${label} must be a non-negative integer.`);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// packages/core/src/transform.ts
|
|
331
|
+
function createEncryptor(registry, options) {
|
|
332
|
+
const cipher = registry.get("cipher", options.cipher);
|
|
333
|
+
if (cipher.type === "stream") {
|
|
334
|
+
return createStreamEncryptor(cipher.createEncryptor({
|
|
335
|
+
key: options.key,
|
|
336
|
+
options
|
|
337
|
+
}));
|
|
338
|
+
}
|
|
339
|
+
const { mode, padding } = resolveBlockOptions(registry, options);
|
|
340
|
+
const blockCipher = cipher.create(options.key);
|
|
341
|
+
const modeEncryptor = mode.createEncryptor({
|
|
342
|
+
cipher: blockCipher,
|
|
343
|
+
iv: options.iv
|
|
344
|
+
});
|
|
345
|
+
if (mode.requiresPadding === false) {
|
|
346
|
+
return createStreamEncryptor(modeEncryptor);
|
|
347
|
+
}
|
|
348
|
+
let finalized = false;
|
|
349
|
+
let pending = new Uint8Array();
|
|
350
|
+
return {
|
|
351
|
+
process(input) {
|
|
352
|
+
assertNotFinalized(finalized);
|
|
353
|
+
const data = concatBytes(pending, input);
|
|
354
|
+
const processLength = data.length - data.length % blockCipher.blockSize;
|
|
355
|
+
pending = data.slice(processLength);
|
|
356
|
+
return processLength === 0 ? new Uint8Array() : modeEncryptor.process(data.subarray(0, processLength));
|
|
357
|
+
},
|
|
358
|
+
finalize(input = new Uint8Array()) {
|
|
359
|
+
assertNotFinalized(finalized);
|
|
360
|
+
const processed = input.length === 0 ? new Uint8Array() : this.process(input);
|
|
361
|
+
finalized = true;
|
|
362
|
+
const finalBlock = padding.pad(pending, blockCipher.blockSize);
|
|
363
|
+
pending = new Uint8Array();
|
|
364
|
+
return concatBytes(processed, modeEncryptor.finalize(finalBlock));
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
function createDecryptor(registry, options) {
|
|
369
|
+
const cipher = registry.get("cipher", options.cipher);
|
|
370
|
+
if (cipher.type === "stream") {
|
|
371
|
+
return createStreamDecryptor(cipher.createDecryptor({
|
|
372
|
+
key: options.key,
|
|
373
|
+
options
|
|
374
|
+
}));
|
|
375
|
+
}
|
|
376
|
+
const { mode, padding } = resolveBlockOptions(registry, options);
|
|
377
|
+
const blockCipher = cipher.create(options.key);
|
|
378
|
+
const modeDecryptor = mode.createDecryptor({
|
|
379
|
+
cipher: blockCipher,
|
|
380
|
+
iv: options.iv
|
|
381
|
+
});
|
|
382
|
+
if (mode.requiresPadding === false) {
|
|
383
|
+
return createStreamDecryptor(modeDecryptor);
|
|
384
|
+
}
|
|
385
|
+
let finalized = false;
|
|
386
|
+
let pending = new Uint8Array();
|
|
387
|
+
let plaintextPending = new Uint8Array();
|
|
388
|
+
return {
|
|
389
|
+
process(input) {
|
|
390
|
+
assertNotFinalized(finalized);
|
|
391
|
+
const data = concatBytes(pending, input);
|
|
392
|
+
const processLength = data.length - data.length % blockCipher.blockSize;
|
|
393
|
+
pending = data.slice(processLength);
|
|
394
|
+
if (processLength === 0) {
|
|
395
|
+
return new Uint8Array();
|
|
396
|
+
}
|
|
397
|
+
const decrypted = modeDecryptor.process(data.subarray(0, processLength));
|
|
398
|
+
const output = plaintextPending;
|
|
399
|
+
plaintextPending = decrypted;
|
|
400
|
+
return output;
|
|
401
|
+
},
|
|
402
|
+
finalize(input = new Uint8Array()) {
|
|
403
|
+
assertNotFinalized(finalized);
|
|
404
|
+
const processed = input.length === 0 ? new Uint8Array() : this.process(input);
|
|
405
|
+
finalized = true;
|
|
406
|
+
if (pending.length !== 0) {
|
|
407
|
+
padding.unpad(pending, blockCipher.blockSize);
|
|
408
|
+
}
|
|
409
|
+
const finalPlaintext = padding.unpad(plaintextPending, blockCipher.blockSize);
|
|
410
|
+
pending = new Uint8Array();
|
|
411
|
+
plaintextPending = new Uint8Array();
|
|
412
|
+
return concatBytes(processed, finalPlaintext, modeDecryptor.finalize());
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function createStreamEncryptor(modeEncryptor) {
|
|
417
|
+
let finalized = false;
|
|
418
|
+
return {
|
|
419
|
+
process(input) {
|
|
420
|
+
assertNotFinalized(finalized);
|
|
421
|
+
return modeEncryptor.process(input);
|
|
422
|
+
},
|
|
423
|
+
finalize(input = new Uint8Array()) {
|
|
424
|
+
assertNotFinalized(finalized);
|
|
425
|
+
finalized = true;
|
|
426
|
+
return concatBytes(
|
|
427
|
+
input.length === 0 ? new Uint8Array() : modeEncryptor.process(input),
|
|
428
|
+
modeEncryptor.finalize()
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function createStreamDecryptor(modeDecryptor) {
|
|
434
|
+
let finalized = false;
|
|
435
|
+
return {
|
|
436
|
+
process(input) {
|
|
437
|
+
assertNotFinalized(finalized);
|
|
438
|
+
return modeDecryptor.process(input);
|
|
439
|
+
},
|
|
440
|
+
finalize(input = new Uint8Array()) {
|
|
441
|
+
assertNotFinalized(finalized);
|
|
442
|
+
finalized = true;
|
|
443
|
+
return concatBytes(
|
|
444
|
+
input.length === 0 ? new Uint8Array() : modeDecryptor.process(input),
|
|
445
|
+
modeDecryptor.finalize()
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function resolveBlockOptions(registry, options) {
|
|
451
|
+
if (!options.mode) {
|
|
452
|
+
throw new Error(`${options.cipher} block cipher requires a mode.`);
|
|
453
|
+
}
|
|
454
|
+
if (!options.padding) {
|
|
455
|
+
throw new Error(`${options.cipher} block cipher requires padding.`);
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
cipher: registry.get("cipher", options.cipher),
|
|
459
|
+
mode: registry.get("mode", options.mode),
|
|
460
|
+
padding: registry.get("padding", options.padding)
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
function assertNotFinalized(finalized) {
|
|
464
|
+
if (finalized) {
|
|
465
|
+
throw new Error("Transform is already finalized.");
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// packages/core/src/registry.ts
|
|
470
|
+
function createRegistry(components = []) {
|
|
471
|
+
const entries = /* @__PURE__ */ new Map();
|
|
472
|
+
const key = (kind, name) => `${kind}:${name}`;
|
|
473
|
+
const registry = {
|
|
474
|
+
use(component) {
|
|
475
|
+
if (component.kind === "preset") {
|
|
476
|
+
for (const item of component.components()) {
|
|
477
|
+
registry.use(item);
|
|
478
|
+
}
|
|
479
|
+
return registry;
|
|
480
|
+
}
|
|
481
|
+
const entryKey = key(component.kind, component.name);
|
|
482
|
+
if (entries.has(entryKey)) {
|
|
483
|
+
throw new DuplicateComponentError(component.kind, component.name);
|
|
484
|
+
}
|
|
485
|
+
entries.set(entryKey, component);
|
|
486
|
+
return registry;
|
|
487
|
+
},
|
|
488
|
+
has(kind, name) {
|
|
489
|
+
return entries.has(key(kind, name));
|
|
490
|
+
},
|
|
491
|
+
get(kind, name) {
|
|
492
|
+
const component = entries.get(key(kind, name));
|
|
493
|
+
if (!component) {
|
|
494
|
+
throw new MissingComponentError(kind, name);
|
|
495
|
+
}
|
|
496
|
+
return component;
|
|
497
|
+
},
|
|
498
|
+
list(kind) {
|
|
499
|
+
const components2 = [...entries.values()];
|
|
500
|
+
return kind ? components2.filter((component) => component.kind === kind) : components2;
|
|
501
|
+
},
|
|
502
|
+
createCipher(options) {
|
|
503
|
+
return {
|
|
504
|
+
encrypt(plaintext) {
|
|
505
|
+
return createEncryptor(registry, options).finalize(plaintext);
|
|
506
|
+
},
|
|
507
|
+
decrypt(ciphertext) {
|
|
508
|
+
return createDecryptor(registry, options).finalize(ciphertext);
|
|
509
|
+
},
|
|
510
|
+
createEncryptor() {
|
|
511
|
+
return createEncryptor(registry, options);
|
|
512
|
+
},
|
|
513
|
+
createDecryptor() {
|
|
514
|
+
return createDecryptor(registry, options);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
},
|
|
518
|
+
createPassphraseCipher(options) {
|
|
519
|
+
return createPassphraseCipher(registry, options);
|
|
520
|
+
},
|
|
521
|
+
encrypt(options) {
|
|
522
|
+
return createEncryptor(registry, options).finalize(options.plaintext);
|
|
523
|
+
},
|
|
524
|
+
decrypt(options) {
|
|
525
|
+
return createDecryptor(registry, options).finalize(options.ciphertext);
|
|
526
|
+
},
|
|
527
|
+
createEncryptor(options) {
|
|
528
|
+
return createEncryptor(registry, options);
|
|
529
|
+
},
|
|
530
|
+
createDecryptor(options) {
|
|
531
|
+
return createDecryptor(registry, options);
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
for (const component of components) {
|
|
535
|
+
registry.use(component);
|
|
536
|
+
}
|
|
537
|
+
return registry;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// packages/core/src/blocks.ts
|
|
541
|
+
function assertBlockSize(blockSize, options = {}) {
|
|
542
|
+
const { max } = options;
|
|
543
|
+
if (!Number.isInteger(blockSize) || blockSize <= 0) {
|
|
544
|
+
throw new RangeError("blockSize must be a positive integer.");
|
|
545
|
+
}
|
|
546
|
+
if (max !== void 0 && blockSize > max) {
|
|
547
|
+
throw new RangeError(`blockSize must be an integer between 1 and ${max}.`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function assertBlockMultiple(input, blockSize, label) {
|
|
551
|
+
assertBlockSize(blockSize);
|
|
552
|
+
if (input.length % blockSize !== 0) {
|
|
553
|
+
throw new Error(`${label} input length must be a multiple of the block size.`);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function assertPaddedInput(input, blockSize, label, options = {}) {
|
|
557
|
+
assertBlockSize(blockSize, options);
|
|
558
|
+
if (input.length === 0 || input.length % blockSize !== 0) {
|
|
559
|
+
throw new Error(`Invalid ${label} padded data.`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
function assertIv(blockSize, iv, modeName) {
|
|
563
|
+
if (!iv) {
|
|
564
|
+
throw new Error(`${modeName} mode requires an IV.`);
|
|
565
|
+
}
|
|
566
|
+
if (iv.length !== blockSize) {
|
|
567
|
+
throw new Error(`${modeName} IV length must match the cipher block size.`);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
function getBlockPaddingLength(inputLength, blockSize) {
|
|
571
|
+
const remainder = inputLength % blockSize;
|
|
572
|
+
return remainder === 0 ? blockSize : blockSize - remainder;
|
|
573
|
+
}
|
|
574
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
575
|
+
0 && (module.exports = {
|
|
576
|
+
CryptoError,
|
|
577
|
+
DuplicateComponentError,
|
|
578
|
+
MissingComponentError,
|
|
579
|
+
assertBlockMultiple,
|
|
580
|
+
assertBlockSize,
|
|
581
|
+
assertBytes,
|
|
582
|
+
assertIv,
|
|
583
|
+
assertPaddedInput,
|
|
584
|
+
concatBytes,
|
|
585
|
+
createRegistry,
|
|
586
|
+
equalBytes,
|
|
587
|
+
getBlockPaddingLength,
|
|
588
|
+
xorBytes
|
|
589
|
+
});
|
|
590
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/errors.ts", "../src/bytes.ts", "../src/passphrase.ts", "../src/transform.ts", "../src/registry.ts", "../src/blocks.ts"],
|
|
4
|
+
"sourcesContent": ["export type {\n AnyComponent,\n BlockCipher,\n BlockCipherComponent,\n BlockModeTransformParams,\n CipherComponent,\n Component,\n ComponentKind,\n FormatComponent,\n FormatParseResult,\n FormatStringifyParams,\n KdfComponent,\n ModeComponent,\n PaddingComponent,\n PresetComponent,\n StreamCipherComponent,\n StreamCipherTransformParams,\n Transform,\n} from './component.js';\nexport { CryptoError, DuplicateComponentError, MissingComponentError } from './errors.js';\nexport type {\n CreatePassphraseCipherOptions,\n FormatOptions,\n KdfOptions,\n PassphraseCipherFacade,\n} from './passphrase.js';\nexport type { CipherFacade, Registry } from './registry.js';\nexport { createRegistry } from './registry.js';\nexport type { CreateTransformOptions } from './transform.js';\nexport { assertBlockMultiple, assertBlockSize, assertIv, assertPaddedInput, getBlockPaddingLength } from './blocks.js';\nexport { assertBytes, concatBytes, equalBytes, xorBytes } from './bytes.js';\n", "export class CryptoError extends Error {\n constructor(message: string) {\n super(message);\n this.name = new.target.name;\n }\n}\n\nexport class DuplicateComponentError extends CryptoError {\n constructor(kind: string, name: string) {\n super(`Component already registered: ${kind}:${name}`);\n }\n}\n\nexport class MissingComponentError extends CryptoError {\n constructor(kind: string, name: string) {\n super(`Component not found: ${kind}:${name}`);\n }\n}\n\n", "export function concatBytes(...chunks: readonly Uint8Array[]): Uint8Array {\n const length = chunks.reduce((sum, chunk) => sum + chunk.length, 0);\n const output = new Uint8Array(length);\n let offset = 0;\n\n for (const chunk of chunks) {\n output.set(chunk, offset);\n offset += chunk.length;\n }\n\n return output;\n}\n\nexport function equalBytes(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) {\n return false;\n }\n\n let diff = 0;\n for (let i = 0; i < a.length; i++) {\n diff |= a[i] ^ b[i];\n }\n return diff === 0;\n}\n\nexport function xorBytes(a: Uint8Array, b: Uint8Array): Uint8Array {\n if (a.length !== b.length) {\n throw new Error('XOR inputs must have the same length.');\n }\n\n const output = new Uint8Array(a.length);\n for (let i = 0; i < a.length; i++) {\n output[i] = a[i] ^ b[i];\n }\n return output;\n}\n\nexport function assertBytes(value: unknown, name: string): asserts value is Uint8Array {\n if (!(value instanceof Uint8Array)) {\n throw new TypeError(`${name} must be a Uint8Array.`);\n }\n}\n", "import type { CipherComponent, FormatComponent, KdfComponent, Transform } from './component.js';\nimport type { Registry } from './registry.js';\nimport type { CreateTransformOptions } from './transform.js';\nimport { concatBytes } from './bytes.js';\n\nexport interface CreatePassphraseCipherOptions {\n cipher: string;\n mode?: string;\n padding?: string;\n passphrase: Uint8Array | string;\n kdf: string | KdfOptions;\n format?: string | FormatOptions;\n salt?: Uint8Array;\n saltSize?: number;\n keySize?: number;\n ivSize?: number;\n [option: string]: unknown;\n}\n\nexport interface KdfOptions {\n name: string;\n [option: string]: unknown;\n}\n\nexport interface FormatOptions {\n name: string;\n [option: string]: unknown;\n}\n\nexport interface PassphraseCipherFacade {\n encrypt(plaintext: Uint8Array): Uint8Array;\n decrypt(input: Uint8Array): Uint8Array;\n createEncryptor(): Transform;\n createDecryptor(): Transform;\n}\n\nexport function createPassphraseCipher(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n): PassphraseCipherFacade {\n return {\n encrypt(plaintext) {\n return concatBytes(this.createEncryptor().finalize(plaintext));\n },\n\n decrypt(input) {\n return concatBytes(this.createDecryptor().finalize(input));\n },\n\n createEncryptor() {\n return createPassphraseEncryptor(registry, options);\n },\n\n createDecryptor() {\n return createPassphraseDecryptor(registry, options);\n },\n };\n}\n\nfunction createPassphraseEncryptor(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n): Transform {\n const format = resolveFormat(registry, options);\n const salt = options.salt ? options.salt.slice() : randomBytes(options.saltSize ?? 8);\n const { key, iv } = deriveKeyIv(registry, options, salt);\n const encryptor = registry.createCipher(toTransformOptions(options, key, iv)).createEncryptor();\n\n if (!format) {\n return encryptor;\n }\n\n if (!isStreamingOpenSslFormat(format)) {\n return createBufferedFormatEncryptor(format, salt, encryptor);\n }\n\n let emittedHeader = false;\n const emitHeader = (): Uint8Array => {\n if (emittedHeader) {\n return new Uint8Array();\n }\n emittedHeader = true;\n return format.stringify({ ciphertext: new Uint8Array(), salt });\n };\n\n return {\n process(input) {\n return concatBytes(emitHeader(), encryptor.process(input));\n },\n\n finalize(input = new Uint8Array()) {\n return concatBytes(emitHeader(), encryptor.finalize(input));\n },\n };\n}\n\nfunction createPassphraseDecryptor(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n): Transform {\n const format = resolveFormat(registry, options);\n\n if (!format) {\n const { key, iv } = deriveKeyIv(registry, options, new Uint8Array());\n return registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();\n }\n\n if (!isStreamingOpenSslFormat(format)) {\n return createBufferedFormatDecryptor(registry, options, format);\n }\n\n let header = new Uint8Array();\n let decryptor: Transform | undefined;\n\n const initDecryptor = (input: Uint8Array): Uint8Array => {\n if (decryptor) {\n return input;\n }\n\n header = new Uint8Array(concatBytes(header, input));\n if (header.length < 16) {\n return new Uint8Array();\n }\n\n const parsed = format.parse(header.slice(0, 16));\n const hasSalt = parsed.salt !== undefined;\n const salt = parsed.salt ?? new Uint8Array();\n const ciphertext = hasSalt ? concatBytes(parsed.ciphertext, header.slice(16)) : header;\n const { key, iv } = deriveKeyIv(registry, options, salt);\n decryptor = registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();\n header = new Uint8Array();\n return ciphertext;\n };\n\n return {\n process(input) {\n const ciphertext = initDecryptor(input);\n return decryptor ? decryptor.process(ciphertext) : new Uint8Array();\n },\n\n finalize(input = new Uint8Array()) {\n const ciphertext = initDecryptor(input);\n if (!decryptor) {\n const { key, iv } = deriveKeyIv(registry, options, new Uint8Array());\n decryptor = registry.createCipher(toTransformOptions(options, key, iv)).createDecryptor();\n const buffered = header;\n header = new Uint8Array();\n return decryptor.finalize(buffered);\n }\n return decryptor.finalize(ciphertext);\n },\n };\n}\n\nfunction createBufferedFormatEncryptor(\n format: FormatComponent,\n salt: Uint8Array,\n encryptor: Transform,\n): Transform {\n const chunks: Uint8Array[] = [];\n\n return {\n process(input) {\n const output = encryptor.process(input);\n if (output.length !== 0) {\n chunks.push(output);\n }\n return new Uint8Array();\n },\n\n finalize(input = new Uint8Array()) {\n const output = encryptor.finalize(input);\n if (output.length !== 0) {\n chunks.push(output);\n }\n return format.stringify({ ciphertext: concatBytes(...chunks), salt });\n },\n };\n}\n\nfunction createBufferedFormatDecryptor(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n format: FormatComponent,\n): Transform {\n const chunks: Uint8Array[] = [];\n\n return {\n process(input) {\n if (input.length !== 0) {\n chunks.push(input);\n }\n return new Uint8Array();\n },\n\n finalize(input = new Uint8Array()) {\n if (input.length !== 0) {\n chunks.push(input);\n }\n const parsed = format.parse(concatBytes(...chunks));\n const { key, iv } = deriveKeyIv(registry, options, parsed.salt ?? new Uint8Array());\n return registry.createCipher(toTransformOptions(options, key, iv)).decrypt(parsed.ciphertext);\n },\n };\n}\n\nfunction deriveKeyIv(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n salt: Uint8Array,\n): { key: Uint8Array; iv?: Uint8Array } {\n const keySize = resolveKeySize(registry, options);\n const ivSize = resolveIvSize(registry, options);\n const derived = deriveSync(registry, options, salt, keySize + ivSize);\n return {\n key: derived.slice(0, keySize),\n iv: ivSize === 0 ? undefined : derived.slice(keySize, keySize + ivSize),\n };\n}\n\nfunction deriveSync(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n salt: Uint8Array,\n length: number,\n): Uint8Array {\n const kdfOptions = normalizeNamedOptions(options.kdf);\n const kdf = registry.get<'kdf', KdfComponent>('kdf', kdfOptions.name);\n const result = kdf.derive({\n ...withoutName(kdfOptions),\n passphrase: options.passphrase,\n salt,\n length,\n });\n if (result instanceof Promise) {\n throw new Error(`KDF ${kdfOptions.name} is asynchronous; use an async passphrase cipher API.`);\n }\n return result;\n}\n\nfunction toTransformOptions(\n options: CreatePassphraseCipherOptions,\n key: Uint8Array,\n iv: Uint8Array | undefined,\n): CreateTransformOptions {\n const {\n passphrase,\n kdf,\n format,\n salt,\n saltSize,\n keySize,\n ivSize,\n ...transformOptions\n } = options;\n\n return {\n ...transformOptions,\n key,\n ...(iv ? { iv } : {}),\n };\n}\n\nfunction resolveKeySize(registry: Registry, options: CreatePassphraseCipherOptions): number {\n if (options.keySize !== undefined) {\n assertPositiveInteger(options.keySize, 'keySize');\n return options.keySize;\n }\n\n const cipher = registry.get<'cipher', CipherComponent>('cipher', options.cipher);\n if (!cipher.keySizes || cipher.keySizes.length === 0) {\n throw new Error(`${options.cipher} passphrase cipher requires keySize.`);\n }\n return Math.max(...cipher.keySizes);\n}\n\nfunction resolveIvSize(registry: Registry, options: CreatePassphraseCipherOptions): number {\n if (options.ivSize !== undefined) {\n assertNonNegativeInteger(options.ivSize, 'ivSize');\n return options.ivSize;\n }\n\n const cipher = registry.get<'cipher', CipherComponent>('cipher', options.cipher);\n return cipher.type === 'block' ? cipher.blockSize : 0;\n}\n\nfunction resolveFormat(\n registry: Registry,\n options: CreatePassphraseCipherOptions,\n): FormatComponent | undefined {\n if (!options.format) {\n return undefined;\n }\n return registry.get<'format', FormatComponent>('format', normalizeNamedOptions(options.format).name);\n}\n\nfunction isStreamingOpenSslFormat(format: FormatComponent): boolean {\n return format.name === 'OpenSSL';\n}\n\nfunction normalizeNamedOptions(options: string | { name: string }): { name: string; [option: string]: unknown } {\n return typeof options === 'string' ? { name: options } : options;\n}\n\nfunction withoutName(options: { name: string; [option: string]: unknown }): Record<string, unknown> {\n const { name, ...rest } = options;\n return rest;\n}\n\nfunction randomBytes(length: number): Uint8Array {\n assertNonNegativeInteger(length, 'saltSize');\n const bytes = new Uint8Array(length);\n if (bytes.length === 0) {\n return bytes;\n }\n\n const crypto = globalThis as typeof globalThis & {\n crypto?: {\n getRandomValues<T extends Uint8Array>(array: T): T;\n };\n };\n crypto.crypto?.getRandomValues(bytes);\n if (bytes.some((byte) => byte !== 0)) {\n return bytes;\n }\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Math.floor(Math.random() * 256);\n }\n return bytes;\n}\n\nfunction assertPositiveInteger(value: number, label: string): void {\n if (!Number.isInteger(value) || value <= 0) {\n throw new RangeError(`${label} must be a positive integer.`);\n }\n}\n\nfunction assertNonNegativeInteger(value: number, label: string): void {\n if (!Number.isInteger(value) || value < 0) {\n throw new RangeError(`${label} must be a non-negative integer.`);\n }\n}\n", "import type { BlockCipherComponent, CipherComponent, ModeComponent, PaddingComponent, Transform } from './component.js';\nimport type { Registry } from './registry.js';\nimport { concatBytes } from './bytes.js';\n\nexport interface CreateTransformOptions {\n cipher: string;\n mode?: string;\n padding?: string;\n key: Uint8Array;\n iv?: Uint8Array;\n [option: string]: unknown;\n}\n\nexport function createEncryptor(registry: Registry, options: CreateTransformOptions): Transform {\n const cipher = registry.get<'cipher', CipherComponent>('cipher', options.cipher);\n if (cipher.type === 'stream') {\n return createStreamEncryptor(cipher.createEncryptor({\n key: options.key,\n options,\n }));\n }\n\n const { mode, padding } = resolveBlockOptions(registry, options);\n const blockCipher = cipher.create(options.key);\n\n const modeEncryptor = mode.createEncryptor({\n cipher: blockCipher,\n iv: options.iv,\n });\n\n if (mode.requiresPadding === false) {\n return createStreamEncryptor(modeEncryptor);\n }\n\n let finalized = false;\n let pending: Uint8Array = new Uint8Array();\n\n return {\n process(input) {\n assertNotFinalized(finalized);\n const data = concatBytes(pending, input);\n const processLength = data.length - (data.length % blockCipher.blockSize);\n pending = data.slice(processLength);\n return processLength === 0 ? new Uint8Array() : modeEncryptor.process(data.subarray(0, processLength));\n },\n\n finalize(input = new Uint8Array()) {\n assertNotFinalized(finalized);\n const processed = input.length === 0 ? new Uint8Array() : this.process(input);\n finalized = true;\n const finalBlock = padding.pad(pending, blockCipher.blockSize);\n pending = new Uint8Array();\n return concatBytes(processed, modeEncryptor.finalize(finalBlock));\n },\n };\n}\n\nexport function createDecryptor(registry: Registry, options: CreateTransformOptions): Transform {\n const cipher = registry.get<'cipher', CipherComponent>('cipher', options.cipher);\n if (cipher.type === 'stream') {\n return createStreamDecryptor(cipher.createDecryptor({\n key: options.key,\n options,\n }));\n }\n\n const { mode, padding } = resolveBlockOptions(registry, options);\n const blockCipher = cipher.create(options.key);\n\n const modeDecryptor = mode.createDecryptor({\n cipher: blockCipher,\n iv: options.iv,\n });\n\n if (mode.requiresPadding === false) {\n return createStreamDecryptor(modeDecryptor);\n }\n\n let finalized = false;\n let pending: Uint8Array = new Uint8Array();\n let plaintextPending: Uint8Array = new Uint8Array();\n\n return {\n process(input) {\n assertNotFinalized(finalized);\n const data = concatBytes(pending, input);\n const processLength = data.length - (data.length % blockCipher.blockSize);\n pending = data.slice(processLength);\n\n if (processLength === 0) {\n return new Uint8Array();\n }\n\n const decrypted = modeDecryptor.process(data.subarray(0, processLength));\n const output = plaintextPending;\n plaintextPending = decrypted;\n return output;\n },\n\n finalize(input = new Uint8Array()) {\n assertNotFinalized(finalized);\n const processed = input.length === 0 ? new Uint8Array() : this.process(input);\n finalized = true;\n if (pending.length !== 0) {\n padding.unpad(pending, blockCipher.blockSize);\n }\n const finalPlaintext = padding.unpad(plaintextPending, blockCipher.blockSize);\n pending = new Uint8Array();\n plaintextPending = new Uint8Array();\n return concatBytes(processed, finalPlaintext, modeDecryptor.finalize());\n },\n };\n}\n\nfunction createStreamEncryptor(modeEncryptor: Transform): Transform {\n let finalized = false;\n\n return {\n process(input) {\n assertNotFinalized(finalized);\n return modeEncryptor.process(input);\n },\n\n finalize(input = new Uint8Array()) {\n assertNotFinalized(finalized);\n finalized = true;\n return concatBytes(\n input.length === 0 ? new Uint8Array() : modeEncryptor.process(input),\n modeEncryptor.finalize(),\n );\n },\n };\n}\n\nfunction createStreamDecryptor(modeDecryptor: Transform): Transform {\n let finalized = false;\n\n return {\n process(input) {\n assertNotFinalized(finalized);\n return modeDecryptor.process(input);\n },\n\n finalize(input = new Uint8Array()) {\n assertNotFinalized(finalized);\n finalized = true;\n return concatBytes(\n input.length === 0 ? new Uint8Array() : modeDecryptor.process(input),\n modeDecryptor.finalize(),\n );\n },\n };\n}\n\nfunction resolveBlockOptions(registry: Registry, options: CreateTransformOptions): {\n cipher: BlockCipherComponent;\n mode: ModeComponent;\n padding: PaddingComponent;\n} {\n if (!options.mode) {\n throw new Error(`${options.cipher} block cipher requires a mode.`);\n }\n if (!options.padding) {\n throw new Error(`${options.cipher} block cipher requires padding.`);\n }\n\n return {\n cipher: registry.get<'cipher', BlockCipherComponent>('cipher', options.cipher),\n mode: registry.get('mode', options.mode),\n padding: registry.get('padding', options.padding),\n };\n}\n\nfunction assertNotFinalized(finalized: boolean): void {\n if (finalized) {\n throw new Error('Transform is already finalized.');\n }\n}\n", "import type { AnyComponent, Component, ComponentKind, PresetComponent } from './component.js';\nimport { DuplicateComponentError, MissingComponentError } from './errors.js';\nimport type { CreatePassphraseCipherOptions, PassphraseCipherFacade } from './passphrase.js';\nimport { createPassphraseCipher } from './passphrase.js';\nimport type { CreateTransformOptions } from './transform.js';\nimport { createDecryptor, createEncryptor } from './transform.js';\nimport type { Transform } from './component.js';\n\nexport interface Registry {\n use(component: AnyComponent): Registry;\n has(kind: ComponentKind, name: string): boolean;\n get<Kind extends ComponentKind, T extends Component<Kind>>(kind: Kind, name: string): T;\n list(kind?: ComponentKind): AnyComponent[];\n createCipher(options: CreateTransformOptions): CipherFacade;\n createPassphraseCipher(options: CreatePassphraseCipherOptions): PassphraseCipherFacade;\n encrypt(options: CreateTransformOptions & { plaintext: Uint8Array }): Uint8Array;\n decrypt(options: CreateTransformOptions & { ciphertext: Uint8Array }): Uint8Array;\n createEncryptor(options: CreateTransformOptions): Transform;\n createDecryptor(options: CreateTransformOptions): Transform;\n}\n\nexport interface CipherFacade {\n encrypt(plaintext: Uint8Array): Uint8Array;\n decrypt(ciphertext: Uint8Array): Uint8Array;\n createEncryptor(): Transform;\n createDecryptor(): Transform;\n}\n\nexport function createRegistry(components: Iterable<AnyComponent> = []): Registry {\n const entries = new Map<string, AnyComponent>();\n\n const key = (kind: ComponentKind, name: string) => `${kind}:${name}`;\n\n const registry: Registry = {\n use(component) {\n if (component.kind === 'preset') {\n for (const item of (component as PresetComponent).components()) {\n registry.use(item);\n }\n return registry;\n }\n\n const entryKey = key(component.kind, component.name);\n if (entries.has(entryKey)) {\n throw new DuplicateComponentError(component.kind, component.name);\n }\n entries.set(entryKey, component);\n return registry;\n },\n\n has(kind, name) {\n return entries.has(key(kind, name));\n },\n\n get(kind, name) {\n const component = entries.get(key(kind, name));\n if (!component) {\n throw new MissingComponentError(kind, name);\n }\n return component as never;\n },\n\n list(kind) {\n const components = [...entries.values()];\n return kind ? components.filter((component) => component.kind === kind) : components;\n },\n\n createCipher(options) {\n return {\n encrypt(plaintext) {\n return createEncryptor(registry, options).finalize(plaintext);\n },\n\n decrypt(ciphertext) {\n return createDecryptor(registry, options).finalize(ciphertext);\n },\n\n createEncryptor() {\n return createEncryptor(registry, options);\n },\n\n createDecryptor() {\n return createDecryptor(registry, options);\n },\n };\n },\n\n createPassphraseCipher(options) {\n return createPassphraseCipher(registry, options);\n },\n\n encrypt(options) {\n return createEncryptor(registry, options).finalize(options.plaintext);\n },\n\n decrypt(options) {\n return createDecryptor(registry, options).finalize(options.ciphertext);\n },\n\n createEncryptor(options) {\n return createEncryptor(registry, options);\n },\n\n createDecryptor(options) {\n return createDecryptor(registry, options);\n },\n };\n\n for (const component of components) {\n registry.use(component);\n }\n\n return registry;\n}\n", "export interface BlockSizeOptions {\n readonly max?: number;\n}\n\nexport function assertBlockSize(blockSize: number, options: BlockSizeOptions = {}): void {\n const { max } = options;\n if (!Number.isInteger(blockSize) || blockSize <= 0) {\n throw new RangeError('blockSize must be a positive integer.');\n }\n if (max !== undefined && blockSize > max) {\n throw new RangeError(`blockSize must be an integer between 1 and ${max}.`);\n }\n}\n\nexport function assertBlockMultiple(input: Uint8Array, blockSize: number, label: string): void {\n assertBlockSize(blockSize);\n if (input.length % blockSize !== 0) {\n throw new Error(`${label} input length must be a multiple of the block size.`);\n }\n}\n\nexport function assertPaddedInput(input: Uint8Array, blockSize: number, label: string, options: BlockSizeOptions = {}): void {\n assertBlockSize(blockSize, options);\n if (input.length === 0 || input.length % blockSize !== 0) {\n throw new Error(`Invalid ${label} padded data.`);\n }\n}\n\nexport function assertIv(blockSize: number, iv: Uint8Array | undefined, modeName: string): asserts iv is Uint8Array {\n if (!iv) {\n throw new Error(`${modeName} mode requires an IV.`);\n }\n if (iv.length !== blockSize) {\n throw new Error(`${modeName} IV length must match the cipher block size.`);\n }\n}\n\nexport function getBlockPaddingLength(inputLength: number, blockSize: number): number {\n const remainder = inputLength % blockSize;\n return remainder === 0 ? blockSize : blockSize - remainder;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAN,cAA0B,MAAM;AAAA,EACrC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO,WAAW;AAAA,EACzB;AACF;AAEO,IAAM,0BAAN,cAAsC,YAAY;AAAA,EACvD,YAAY,MAAc,MAAc;AACtC,UAAM,iCAAiC,IAAI,IAAI,IAAI,EAAE;AAAA,EACvD;AACF;AAEO,IAAM,wBAAN,cAAoC,YAAY;AAAA,EACrD,YAAY,MAAc,MAAc;AACtC,UAAM,wBAAwB,IAAI,IAAI,IAAI,EAAE;AAAA,EAC9C;AACF;;;ACjBO,SAAS,eAAe,QAA2C;AACxE,QAAM,SAAS,OAAO,OAAO,CAAC,KAAK,UAAU,MAAM,MAAM,QAAQ,CAAC;AAClE,QAAM,SAAS,IAAI,WAAW,MAAM;AACpC,MAAI,SAAS;AAEb,aAAW,SAAS,QAAQ;AAC1B,WAAO,IAAI,OAAO,MAAM;AACxB,cAAU,MAAM;AAAA,EAClB;AAEA,SAAO;AACT;AAEO,SAAS,WAAW,GAAe,GAAwB;AAChE,MAAI,EAAE,WAAW,EAAE,QAAQ;AACzB,WAAO;AAAA,EACT;AAEA,MAAI,OAAO;AACX,WAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,YAAQ,EAAE,CAAC,IAAI,EAAE,CAAC;AAAA,EACpB;AACA,SAAO,SAAS;AAClB;AAEO,SAAS,SAAS,GAAe,GAA2B;AACjE,MAAI,EAAE,WAAW,EAAE,QAAQ;AACzB,UAAM,IAAI,MAAM,uCAAuC;AAAA,EACzD;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AQ,UAAa,YAAY,KAAK;AACxC,UAAM,IAAI,WAAW,8CAA8C,GAAG,GAAG;AAAA,EAC3E;AACF;AAEO,SAAS,oBAAoB,OAAmB,WAAmB,OAAqB;AAC7F,kBAAgB,SAAS;AACzB,MAAI,MAAM,SAAS,cAAc,GAAG;AAClC,UAAM,IAAI,MAAM,GAAG,KAAK,qDAAqD;AAAA,EAC/E;AACF;AAEO,SAAS,kBAAkB,OAAmB,WAAmB,OAAe,UAA4B,CAAC,GAAS;AAC3H,kBAAgB,WAAW,OAAO;AAClC,MAAI,MAAM,WAAW,KAAK,MAAM,SAAS,cAAc,GAAG;AACxD,UAAM,IAAI,MAAM,WAAW,KAAK,eAAe;AAAA,EACjD;AACF;AAEO,SAAS,SAAS,WAAmB,IAA4B,UAA4C;AAClH,MAAI,CAAC,IAAI;AACP,UAAM,IAAI,MAAM,GAAG,QAAQ,uBAAuB;AAAA,EACpD;AACA,MAAI,GAAG,WAAW,WAAW;AAC3B,UAAM,IAAI,MAAM,GAAG,QAAQ,8CAA8C;AAAA,EAC3E;AACF;AAEO,SAAS,sBAAsB,aAAqB,WAA2B;AACpF,QAAM,YAAY,cAAc;AAChC,SAAO,cAAc,IAAI,YAAY,YAAY;AACnD;",
|
|
6
|
+
"names": ["components"]
|
|
7
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { AnyComponent, BlockCipher, BlockCipherComponent, BlockModeTransformParams, CipherComponent, Component, ComponentKind, FormatComponent, FormatParseResult, FormatStringifyParams, KdfComponent, ModeComponent, PaddingComponent, PresetComponent, StreamCipherComponent, StreamCipherTransformParams, Transform, } from './component.js';
|
|
2
|
+
export { CryptoError, DuplicateComponentError, MissingComponentError } from './errors.js';
|
|
3
|
+
export type { CreatePassphraseCipherOptions, FormatOptions, KdfOptions, PassphraseCipherFacade, } from './passphrase.js';
|
|
4
|
+
export type { CipherFacade, Registry } from './registry.js';
|
|
5
|
+
export { createRegistry } from './registry.js';
|
|
6
|
+
export type { CreateTransformOptions } from './transform.js';
|
|
7
|
+
export { assertBlockMultiple, assertBlockSize, assertIv, assertPaddedInput, getBlockPaddingLength } from './blocks.js';
|
|
8
|
+
export { assertBytes, concatBytes, equalBytes, xorBytes } from './bytes.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,WAAW,EACX,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,SAAS,EACT,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,qBAAqB,EACrB,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,qBAAqB,EACrB,2BAA2B,EAC3B,SAAS,GACV,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC1F,YAAY,EACV,6BAA6B,EAC7B,aAAa,EACb,UAAU,EACV,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,QAAQ,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACvH,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|