wilcocrypt 2.1.0 → 2.2.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/.github/ISSUE_TEMPLATE/bug.md +78 -0
- package/.github/ISSUE_TEMPLATE/config.yml +6 -0
- package/.github/ISSUE_TEMPLATE/feature.yml +38 -0
- package/.github/dependabot.yml +11 -0
- package/CHANGELOG.md +62 -0
- package/DOCS.md +335 -0
- package/README.md +69 -59
- package/SECURITY.md +48 -0
- package/package.json +17 -10
- package/src/cli.js +141 -0
- package/src/wilcocrypt.js +422 -0
- package/types/wilcocrypt.d.ts +186 -0
- package/dist/cli.js +0 -4441
- package/dist/cli.min.cjs +0 -2
- package/dist/cli.min.js +0 -2
- package/dist/wilcocrypt.js +0 -987
- package/dist/wilcocrypt.min.js +0 -1
- package/rollup.config.js +0 -54
- package/sea/sea-config.json +0 -5
package/dist/wilcocrypt.js
DELETED
|
@@ -1,987 +0,0 @@
|
|
|
1
|
-
import { createCipheriv, createDecipheriv, randomBytes, scryptSync } from 'crypto';
|
|
2
|
-
import { gzipSync, gunzipSync } from 'zlib';
|
|
3
|
-
import { readFileSync, writeFileSync } from 'fs';
|
|
4
|
-
|
|
5
|
-
var lib = {};
|
|
6
|
-
|
|
7
|
-
var encode_1;
|
|
8
|
-
var hasRequiredEncode;
|
|
9
|
-
|
|
10
|
-
function requireEncode () {
|
|
11
|
-
if (hasRequiredEncode) return encode_1;
|
|
12
|
-
hasRequiredEncode = 1;
|
|
13
|
-
|
|
14
|
-
const MICRO_OPT_LEN = 32;
|
|
15
|
-
const TIMESTAMP32_MAX_SEC = 0x100000000 - 1; // 32-bit unsigned int
|
|
16
|
-
const TIMESTAMP64_MAX_SEC = 0x400000000 - 1; // 34-bit unsigned int
|
|
17
|
-
|
|
18
|
-
// Faster for short strings than buffer.write
|
|
19
|
-
function utf8Write(arr, offset, str) {
|
|
20
|
-
let c = 0;
|
|
21
|
-
for (let i = 0, l = str.length; i < l; i++) {
|
|
22
|
-
c = str.charCodeAt(i);
|
|
23
|
-
if (c < 0x80) {
|
|
24
|
-
arr[offset++] = c;
|
|
25
|
-
} else if (c < 0x800) {
|
|
26
|
-
arr[offset++] = 0xc0 | (c >> 6);
|
|
27
|
-
arr[offset++] = 0x80 | (c & 0x3f);
|
|
28
|
-
} else if (c < 0xd800 || c >= 0xe000) {
|
|
29
|
-
arr[offset++] = 0xe0 | (c >> 12);
|
|
30
|
-
arr[offset++] = 0x80 | (c >> 6) & 0x3f;
|
|
31
|
-
arr[offset++] = 0x80 | (c & 0x3f);
|
|
32
|
-
} else {
|
|
33
|
-
i++;
|
|
34
|
-
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
|
|
35
|
-
arr[offset++] = 0xf0 | (c >> 18);
|
|
36
|
-
arr[offset++] = 0x80 | (c >> 12) & 0x3f;
|
|
37
|
-
arr[offset++] = 0x80 | (c >> 6) & 0x3f;
|
|
38
|
-
arr[offset++] = 0x80 | (c & 0x3f);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Faster for short strings than Buffer.byteLength
|
|
44
|
-
function utf8Length(str) {
|
|
45
|
-
let c = 0, length = 0;
|
|
46
|
-
for (let i = 0, l = str.length; i < l; i++) {
|
|
47
|
-
c = str.charCodeAt(i);
|
|
48
|
-
if (c < 0x80) {
|
|
49
|
-
length += 1;
|
|
50
|
-
} else if (c < 0x800) {
|
|
51
|
-
length += 2;
|
|
52
|
-
} else if (c < 0xd800 || c >= 0xe000) {
|
|
53
|
-
length += 3;
|
|
54
|
-
} else {
|
|
55
|
-
i++;
|
|
56
|
-
length += 4;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return length;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const cache = new Map();
|
|
63
|
-
const cacheMaxSize = process.env.NOTEPACK_ENCODE_CACHE_MAX_SIZE || 1024;
|
|
64
|
-
|
|
65
|
-
/*jshint latedef: nofunc */
|
|
66
|
-
function encodeKey(bytes, defers, key) {
|
|
67
|
-
if (cache.has(key)) {
|
|
68
|
-
const buffer = cache.get(key);
|
|
69
|
-
defers.push({ bin: buffer, length: buffer.length, offset: bytes.length });
|
|
70
|
-
return buffer.length;
|
|
71
|
-
}
|
|
72
|
-
if (cache.size > cacheMaxSize) {
|
|
73
|
-
return _encode(bytes, defers, key);
|
|
74
|
-
}
|
|
75
|
-
const keyBytes = [];
|
|
76
|
-
const size = _encode(keyBytes, [], key);
|
|
77
|
-
const keyBuffer = Buffer.allocUnsafe(size);
|
|
78
|
-
for (let i = 0, l = keyBytes.length; i < l; i++) {
|
|
79
|
-
keyBuffer[i] = keyBytes[i];
|
|
80
|
-
}
|
|
81
|
-
utf8Write(keyBuffer, keyBytes.length, key);
|
|
82
|
-
defers.push({ bin: keyBuffer, length: size, offset: bytes.length });
|
|
83
|
-
cache.set(key, keyBuffer);
|
|
84
|
-
return size;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function _encode(bytes, defers, value) {
|
|
88
|
-
let hi = 0, lo = 0, length = 0, size = 0;
|
|
89
|
-
|
|
90
|
-
switch (typeof value) {
|
|
91
|
-
case 'string':
|
|
92
|
-
if (value.length > MICRO_OPT_LEN) {
|
|
93
|
-
length = Buffer.byteLength(value);
|
|
94
|
-
} else {
|
|
95
|
-
length = utf8Length(value);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (length < 0x20) { // fixstr
|
|
99
|
-
bytes.push(length | 0xa0);
|
|
100
|
-
size = 1;
|
|
101
|
-
} else if (length < 0x100) { // str 8
|
|
102
|
-
bytes.push(0xd9, length);
|
|
103
|
-
size = 2;
|
|
104
|
-
} else if (length < 0x10000) { // str 16
|
|
105
|
-
bytes.push(0xda, length >> 8, length);
|
|
106
|
-
size = 3;
|
|
107
|
-
} else if (length < 0x100000000) { // str 32
|
|
108
|
-
bytes.push(0xdb, length >> 24, length >> 16, length >> 8, length);
|
|
109
|
-
size = 5;
|
|
110
|
-
} else {
|
|
111
|
-
throw new Error('String too long');
|
|
112
|
-
}
|
|
113
|
-
defers.push({ str: value, length: length, offset: bytes.length });
|
|
114
|
-
return size + length;
|
|
115
|
-
case 'number':
|
|
116
|
-
// TODO: encode to float 32?
|
|
117
|
-
|
|
118
|
-
if (Math.floor(value) !== value || !isFinite(value)) { // float 64
|
|
119
|
-
bytes.push(0xcb);
|
|
120
|
-
defers.push({ float: value, length: 8, offset: bytes.length });
|
|
121
|
-
return 9;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (value >= 0) {
|
|
125
|
-
if (value < 0x80) { // positive fixnum
|
|
126
|
-
bytes.push(value);
|
|
127
|
-
return 1;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (value < 0x100) { // uint 8
|
|
131
|
-
bytes.push(0xcc, value);
|
|
132
|
-
return 2;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (value < 0x10000) { // uint 16
|
|
136
|
-
bytes.push(0xcd, value >> 8, value);
|
|
137
|
-
return 3;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (value < 0x100000000) { // uint 32
|
|
141
|
-
bytes.push(0xce, value >> 24, value >> 16, value >> 8, value);
|
|
142
|
-
return 5;
|
|
143
|
-
}
|
|
144
|
-
// uint 64
|
|
145
|
-
hi = (value / Math.pow(2, 32)) >> 0;
|
|
146
|
-
lo = value >>> 0;
|
|
147
|
-
bytes.push(0xcf, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo);
|
|
148
|
-
return 9;
|
|
149
|
-
} else {
|
|
150
|
-
|
|
151
|
-
if (value >= -32) { // negative fixnum
|
|
152
|
-
bytes.push(value);
|
|
153
|
-
return 1;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (value >= -128) { // int 8
|
|
157
|
-
bytes.push(0xd0, value);
|
|
158
|
-
return 2;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (value >= -32768) { // int 16
|
|
162
|
-
bytes.push(0xd1, value >> 8, value);
|
|
163
|
-
return 3;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (value >= -2147483648) { // int 32
|
|
167
|
-
bytes.push(0xd2, value >> 24, value >> 16, value >> 8, value);
|
|
168
|
-
return 5;
|
|
169
|
-
}
|
|
170
|
-
// int 64
|
|
171
|
-
hi = Math.floor(value / Math.pow(2, 32));
|
|
172
|
-
lo = value >>> 0;
|
|
173
|
-
bytes.push(0xd3, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo);
|
|
174
|
-
return 9;
|
|
175
|
-
}
|
|
176
|
-
case 'object':
|
|
177
|
-
// nil
|
|
178
|
-
if (value === null) {
|
|
179
|
-
bytes.push(0xc0);
|
|
180
|
-
return 1;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (Array.isArray(value)) {
|
|
184
|
-
length = value.length;
|
|
185
|
-
|
|
186
|
-
if (length < 0x10) { // fixarray
|
|
187
|
-
bytes.push(length | 0x90);
|
|
188
|
-
size = 1;
|
|
189
|
-
} else if (length < 0x10000) { // array 16
|
|
190
|
-
bytes.push(0xdc, length >> 8, length);
|
|
191
|
-
size = 3;
|
|
192
|
-
} else if (length < 0x100000000) { // array 32
|
|
193
|
-
bytes.push(0xdd, length >> 24, length >> 16, length >> 8, length);
|
|
194
|
-
size = 5;
|
|
195
|
-
} else {
|
|
196
|
-
throw new Error('Array too large');
|
|
197
|
-
}
|
|
198
|
-
for (let i = 0; i < length; i++) {
|
|
199
|
-
size += _encode(bytes, defers, value[i]);
|
|
200
|
-
}
|
|
201
|
-
return size;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (value instanceof Date) {
|
|
205
|
-
const ms = value.getTime();
|
|
206
|
-
const s = Math.floor(ms / 1e3);
|
|
207
|
-
const ns = (ms - s * 1e3) * 1e6;
|
|
208
|
-
|
|
209
|
-
if (s >= 0 && ns >= 0 && s <= TIMESTAMP64_MAX_SEC) {
|
|
210
|
-
if (ns === 0 && s <= TIMESTAMP32_MAX_SEC) {
|
|
211
|
-
// timestamp 32
|
|
212
|
-
bytes.push(0xd6, 0xff, s >> 24, s >> 16, s >> 8, s);
|
|
213
|
-
return 6;
|
|
214
|
-
} else {
|
|
215
|
-
// timestamp 64
|
|
216
|
-
hi = s / 0x100000000;
|
|
217
|
-
lo = s & 0xffffffff;
|
|
218
|
-
bytes.push(0xd7, 0xff, ns >> 22, ns >> 14, ns >> 6, hi, lo >> 24, lo >> 16, lo >> 8, lo);
|
|
219
|
-
return 10;
|
|
220
|
-
}
|
|
221
|
-
} else {
|
|
222
|
-
// timestamp 96
|
|
223
|
-
hi = Math.floor(s / 0x100000000);
|
|
224
|
-
lo = s >>> 0;
|
|
225
|
-
bytes.push(0xc7, 0x0c, 0xff, ns >> 24, ns >> 16, ns >> 8, ns, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo);
|
|
226
|
-
return 15;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (value instanceof Buffer) {
|
|
231
|
-
length = value.length;
|
|
232
|
-
|
|
233
|
-
if (length < 0x100) { // bin 8
|
|
234
|
-
bytes.push(0xc4, length);
|
|
235
|
-
size = 2;
|
|
236
|
-
} else if (length < 0x10000) { // bin 16
|
|
237
|
-
bytes.push(0xc5, length >> 8, length);
|
|
238
|
-
size = 3;
|
|
239
|
-
} else if (length < 0x100000000) { // bin 32
|
|
240
|
-
bytes.push(0xc6, length >> 24, length >> 16, length >> 8, length);
|
|
241
|
-
size = 5;
|
|
242
|
-
} else {
|
|
243
|
-
throw new Error('Buffer too large');
|
|
244
|
-
}
|
|
245
|
-
defers.push({ bin: value, length: length, offset: bytes.length });
|
|
246
|
-
return size + length;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
|
250
|
-
return _encode(bytes, defers, Buffer.from(value));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (typeof value.toJSON === 'function') {
|
|
254
|
-
return _encode(bytes, defers, value.toJSON());
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// note: faster than for...in + hasOwnProperty in most cases
|
|
258
|
-
const keys = [], allKeys = Object.keys(value);
|
|
259
|
-
let key = '';
|
|
260
|
-
|
|
261
|
-
for (let i = 0, l = allKeys.length; i < l; i++) {
|
|
262
|
-
key = allKeys[i];
|
|
263
|
-
if (value[key] !== undefined && typeof value[key] !== 'function') {
|
|
264
|
-
keys.push(key);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
length = keys.length;
|
|
268
|
-
|
|
269
|
-
if (length < 0x10) { // fixmap
|
|
270
|
-
bytes.push(length | 0x80);
|
|
271
|
-
size = 1;
|
|
272
|
-
} else if (length < 0x10000) { // map 16
|
|
273
|
-
bytes.push(0xde, length >> 8, length);
|
|
274
|
-
size = 3;
|
|
275
|
-
} else if (length < 0x100000000) { // map 32
|
|
276
|
-
bytes.push(0xdf, length >> 24, length >> 16, length >> 8, length);
|
|
277
|
-
size = 5;
|
|
278
|
-
} else {
|
|
279
|
-
throw new Error('Object too large');
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
for (let i = 0; i < length; i++) {
|
|
283
|
-
key = keys[i];
|
|
284
|
-
size += encodeKey(bytes, defers, key);
|
|
285
|
-
size += _encode(bytes, defers, value[key]);
|
|
286
|
-
}
|
|
287
|
-
return size;
|
|
288
|
-
case 'boolean': // false/true
|
|
289
|
-
bytes.push(value ? 0xc3 : 0xc2);
|
|
290
|
-
return 1;
|
|
291
|
-
case 'undefined':
|
|
292
|
-
bytes.push(0xc0);
|
|
293
|
-
return 1;
|
|
294
|
-
default:
|
|
295
|
-
// custom types like BigInt (typeof value === 'bigint')
|
|
296
|
-
if (typeof value.toJSON === 'function') {
|
|
297
|
-
return _encode(bytes, defers, value.toJSON());
|
|
298
|
-
}
|
|
299
|
-
throw new Error('Could not encode');
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function encode(value) {
|
|
304
|
-
const bytes = [], defers = [], size = _encode(bytes, defers, value);
|
|
305
|
-
const buf = Buffer.allocUnsafe(size);
|
|
306
|
-
|
|
307
|
-
let deferIndex = 0, deferWritten = 0, nextOffset = -1;
|
|
308
|
-
if (defers.length > 0) {
|
|
309
|
-
nextOffset = defers[0].offset;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
let defer, deferLength = 0, offset = 0;
|
|
313
|
-
for (let i = 0, l = bytes.length; i < l; i++) {
|
|
314
|
-
buf[deferWritten + i] = bytes[i];
|
|
315
|
-
while (i + 1 === nextOffset) {
|
|
316
|
-
defer = defers[deferIndex];
|
|
317
|
-
deferLength = defer.length;
|
|
318
|
-
offset = deferWritten + nextOffset;
|
|
319
|
-
if (defer.bin) {
|
|
320
|
-
if (deferLength > MICRO_OPT_LEN) {
|
|
321
|
-
defer.bin.copy(buf, offset, 0, deferLength);
|
|
322
|
-
} else {
|
|
323
|
-
const bin = defer.bin;
|
|
324
|
-
for (let j = 0; j < deferLength; j++) {
|
|
325
|
-
buf[offset + j] = bin[j];
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
} else if (defer.str) {
|
|
329
|
-
if (deferLength > MICRO_OPT_LEN) {
|
|
330
|
-
buf.write(defer.str, offset, deferLength, 'utf8');
|
|
331
|
-
} else {
|
|
332
|
-
utf8Write(buf, offset, defer.str);
|
|
333
|
-
}
|
|
334
|
-
} else if (defer.float !== undefined) {
|
|
335
|
-
buf.writeDoubleBE(defer.float, offset);
|
|
336
|
-
}
|
|
337
|
-
deferIndex++;
|
|
338
|
-
deferWritten += deferLength;
|
|
339
|
-
if (defers[deferIndex]) {
|
|
340
|
-
nextOffset = defers[deferIndex].offset;
|
|
341
|
-
} else {
|
|
342
|
-
break;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return buf;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
encode_1 = encode;
|
|
350
|
-
return encode_1;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
var DecodeKeyCache_1;
|
|
354
|
-
var hasRequiredDecodeKeyCache;
|
|
355
|
-
|
|
356
|
-
function requireDecodeKeyCache () {
|
|
357
|
-
if (hasRequiredDecodeKeyCache) return DecodeKeyCache_1;
|
|
358
|
-
hasRequiredDecodeKeyCache = 1;
|
|
359
|
-
const DEFAULT_MAX_SIZE = process.env.NOTEPACK_DECODE_KEY_CACHE_MAX_SIZE || 1024;
|
|
360
|
-
const DEFAULT_MAX_LENGTH = process.env.NOTEPACK_DECODE_KEY_CACHE_MAX_LENGTH || 16;
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Store the buffer-to-string values in a tree
|
|
364
|
-
*/
|
|
365
|
-
class DecodeKeyCache {
|
|
366
|
-
constructor({ maxSize = DEFAULT_MAX_SIZE, maxLength = DEFAULT_MAX_LENGTH } = {}) {
|
|
367
|
-
this.size = 0;
|
|
368
|
-
this.maxSize = maxSize;
|
|
369
|
-
this.maxLength = maxLength;
|
|
370
|
-
this.cache = new Map();
|
|
371
|
-
for (let i = 1; i <= this.maxLength; i++) {
|
|
372
|
-
this.cache.set(i, new Map());
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
get(buffer, offset, length) {
|
|
377
|
-
if (length > this.maxLength) { return false; }
|
|
378
|
-
let node = this.cache.get(length);
|
|
379
|
-
for (let i = 0; i < length; i++) {
|
|
380
|
-
const byte = buffer[offset + i];
|
|
381
|
-
if (node.has(byte)) {
|
|
382
|
-
node = node.get(byte);
|
|
383
|
-
} else {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return node;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
set(buffer, offset, length, value) {
|
|
391
|
-
if (length > this.maxLength || this.size >= this.maxSize) { return; }
|
|
392
|
-
this.size++;
|
|
393
|
-
let node = this.cache.get(length);
|
|
394
|
-
for (let i = 0; i < length; i++) {
|
|
395
|
-
const byte = buffer[offset + i];
|
|
396
|
-
if (i === length - 1) {
|
|
397
|
-
node.set(byte, value);
|
|
398
|
-
} else if (node.has(byte)) {
|
|
399
|
-
node = node.get(byte);
|
|
400
|
-
} else {
|
|
401
|
-
const newNode = new Map();
|
|
402
|
-
node.set(byte, newNode);
|
|
403
|
-
node = newNode;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
DecodeKeyCache_1 = DecodeKeyCache;
|
|
410
|
-
return DecodeKeyCache_1;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
var decode_1;
|
|
414
|
-
var hasRequiredDecode;
|
|
415
|
-
|
|
416
|
-
function requireDecode () {
|
|
417
|
-
if (hasRequiredDecode) return decode_1;
|
|
418
|
-
hasRequiredDecode = 1;
|
|
419
|
-
|
|
420
|
-
const DecodeKeyCache = requireDecodeKeyCache();
|
|
421
|
-
const cache = new DecodeKeyCache();
|
|
422
|
-
|
|
423
|
-
function Decoder(buffer) {
|
|
424
|
-
this.offset = 0;
|
|
425
|
-
this.buffer = buffer;
|
|
426
|
-
this.useKeyCache = false;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
Decoder.prototype.array = function (length) {
|
|
430
|
-
const value = new Array(length);
|
|
431
|
-
for (let i = 0; i < length; i++) {
|
|
432
|
-
value[i] = this.parse();
|
|
433
|
-
}
|
|
434
|
-
return value;
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
Decoder.prototype.map = function (length) {
|
|
438
|
-
let key = '', value = {};
|
|
439
|
-
for (let i = 0; i < length; i++) {
|
|
440
|
-
this.useKeyCache = true;
|
|
441
|
-
key = this.parse();
|
|
442
|
-
this.useKeyCache = false;
|
|
443
|
-
value[key] = this.parse();
|
|
444
|
-
}
|
|
445
|
-
return value;
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
Decoder.prototype.str = function (length) {
|
|
449
|
-
if (this.useKeyCache) {
|
|
450
|
-
const valueFromCache = cache.get(this.buffer, this.offset, length);
|
|
451
|
-
if (valueFromCache) {
|
|
452
|
-
this.offset += length;
|
|
453
|
-
return valueFromCache;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
const value = this.buffer.toString('utf8', this.offset, this.offset + length);
|
|
457
|
-
if (this.useKeyCache) {
|
|
458
|
-
cache.set(this.buffer, this.offset, length, value);
|
|
459
|
-
}
|
|
460
|
-
this.offset += length;
|
|
461
|
-
return value;
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
Decoder.prototype.bin = function (length) {
|
|
465
|
-
const value = this.buffer.slice(this.offset, this.offset + length);
|
|
466
|
-
this.offset += length;
|
|
467
|
-
return value;
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
Decoder.prototype.arraybuffer = function (length) {
|
|
471
|
-
const buffer = new ArrayBuffer(length);
|
|
472
|
-
const view = new Uint8Array(buffer);
|
|
473
|
-
for (let j = 0; j < length; j++) {
|
|
474
|
-
view[j] = this.buffer[this.offset + j];
|
|
475
|
-
}
|
|
476
|
-
this.offset += length;
|
|
477
|
-
return buffer;
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
Decoder.prototype.parse = function () {
|
|
481
|
-
const prefix = this.buffer[this.offset++];
|
|
482
|
-
let value, length = 0, type = 0, hi = 0, lo = 0;
|
|
483
|
-
|
|
484
|
-
if (prefix < 0xc0) {
|
|
485
|
-
// positive fixint
|
|
486
|
-
if (prefix < 0x80) {
|
|
487
|
-
return prefix;
|
|
488
|
-
}
|
|
489
|
-
// fixmap
|
|
490
|
-
if (prefix < 0x90) {
|
|
491
|
-
return this.map(prefix & 0x0f);
|
|
492
|
-
}
|
|
493
|
-
// fixarray
|
|
494
|
-
if (prefix < 0xa0) {
|
|
495
|
-
return this.array(prefix & 0x0f);
|
|
496
|
-
}
|
|
497
|
-
// fixstr
|
|
498
|
-
return this.str(prefix & 0x1f);
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
// negative fixint
|
|
502
|
-
if (prefix > 0xdf) {
|
|
503
|
-
return (0xff - prefix + 1) * -1;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
switch (prefix) {
|
|
507
|
-
// nil
|
|
508
|
-
case 0xc0:
|
|
509
|
-
return null;
|
|
510
|
-
// false
|
|
511
|
-
case 0xc2:
|
|
512
|
-
return false;
|
|
513
|
-
// true
|
|
514
|
-
case 0xc3:
|
|
515
|
-
return true;
|
|
516
|
-
|
|
517
|
-
// bin
|
|
518
|
-
case 0xc4:
|
|
519
|
-
length = this.buffer.readUInt8(this.offset);
|
|
520
|
-
this.offset += 1;
|
|
521
|
-
return this.bin(length);
|
|
522
|
-
case 0xc5:
|
|
523
|
-
length = this.buffer.readUInt16BE(this.offset);
|
|
524
|
-
this.offset += 2;
|
|
525
|
-
return this.bin(length);
|
|
526
|
-
case 0xc6:
|
|
527
|
-
length = this.buffer.readUInt32BE(this.offset);
|
|
528
|
-
this.offset += 4;
|
|
529
|
-
return this.bin(length);
|
|
530
|
-
|
|
531
|
-
// ext
|
|
532
|
-
case 0xc7:
|
|
533
|
-
length = this.buffer.readUInt8(this.offset);
|
|
534
|
-
type = this.buffer.readInt8(this.offset + 1);
|
|
535
|
-
this.offset += 2;
|
|
536
|
-
if (type === 0) {
|
|
537
|
-
// custom ArrayBuffer encoding (kept for backward-compatibility)
|
|
538
|
-
return this.arraybuffer(length);
|
|
539
|
-
}
|
|
540
|
-
if (type === -1) {
|
|
541
|
-
// timestamp 96
|
|
542
|
-
const ns = this.buffer.readUInt32BE(this.offset);
|
|
543
|
-
hi = this.buffer.readInt32BE(this.offset + 4);
|
|
544
|
-
lo = this.buffer.readUInt32BE(this.offset + 8);
|
|
545
|
-
this.offset += 12;
|
|
546
|
-
return new Date((hi * 0x100000000 + lo) * 1e3 + ns / 1e6);
|
|
547
|
-
}
|
|
548
|
-
return [type, this.bin(length)];
|
|
549
|
-
case 0xc8:
|
|
550
|
-
length = this.buffer.readUInt16BE(this.offset);
|
|
551
|
-
type = this.buffer.readInt8(this.offset + 2);
|
|
552
|
-
this.offset += 3;
|
|
553
|
-
if (type === 0) {
|
|
554
|
-
// custom ArrayBuffer encoding (kept for backward-compatibility)
|
|
555
|
-
return this.arraybuffer(length);
|
|
556
|
-
}
|
|
557
|
-
return [type, this.bin(length)];
|
|
558
|
-
case 0xc9:
|
|
559
|
-
length = this.buffer.readUInt32BE(this.offset);
|
|
560
|
-
type = this.buffer.readInt8(this.offset + 4);
|
|
561
|
-
this.offset += 5;
|
|
562
|
-
if (type === 0) {
|
|
563
|
-
// custom ArrayBuffer encoding (kept for backward-compatibility)
|
|
564
|
-
return this.arraybuffer(length);
|
|
565
|
-
}
|
|
566
|
-
return [type, this.bin(length)];
|
|
567
|
-
|
|
568
|
-
// float
|
|
569
|
-
case 0xca:
|
|
570
|
-
value = this.buffer.readFloatBE(this.offset);
|
|
571
|
-
this.offset += 4;
|
|
572
|
-
return value;
|
|
573
|
-
case 0xcb:
|
|
574
|
-
value = this.buffer.readDoubleBE(this.offset);
|
|
575
|
-
this.offset += 8;
|
|
576
|
-
return value;
|
|
577
|
-
|
|
578
|
-
// uint
|
|
579
|
-
case 0xcc:
|
|
580
|
-
value = this.buffer.readUInt8(this.offset);
|
|
581
|
-
this.offset += 1;
|
|
582
|
-
return value;
|
|
583
|
-
case 0xcd:
|
|
584
|
-
value = this.buffer.readUInt16BE(this.offset);
|
|
585
|
-
this.offset += 2;
|
|
586
|
-
return value;
|
|
587
|
-
case 0xce:
|
|
588
|
-
value = this.buffer.readUInt32BE(this.offset);
|
|
589
|
-
this.offset += 4;
|
|
590
|
-
return value;
|
|
591
|
-
case 0xcf:
|
|
592
|
-
hi = this.buffer.readUInt32BE(this.offset) * Math.pow(2, 32);
|
|
593
|
-
lo = this.buffer.readUInt32BE(this.offset + 4);
|
|
594
|
-
this.offset += 8;
|
|
595
|
-
return hi + lo;
|
|
596
|
-
|
|
597
|
-
// int
|
|
598
|
-
case 0xd0:
|
|
599
|
-
value = this.buffer.readInt8(this.offset);
|
|
600
|
-
this.offset += 1;
|
|
601
|
-
return value;
|
|
602
|
-
case 0xd1:
|
|
603
|
-
value = this.buffer.readInt16BE(this.offset);
|
|
604
|
-
this.offset += 2;
|
|
605
|
-
return value;
|
|
606
|
-
case 0xd2:
|
|
607
|
-
value = this.buffer.readInt32BE(this.offset);
|
|
608
|
-
this.offset += 4;
|
|
609
|
-
return value;
|
|
610
|
-
case 0xd3:
|
|
611
|
-
hi = this.buffer.readInt32BE(this.offset) * Math.pow(2, 32);
|
|
612
|
-
lo = this.buffer.readUInt32BE(this.offset + 4);
|
|
613
|
-
this.offset += 8;
|
|
614
|
-
return hi + lo;
|
|
615
|
-
|
|
616
|
-
// fixext
|
|
617
|
-
case 0xd4:
|
|
618
|
-
type = this.buffer.readInt8(this.offset);
|
|
619
|
-
this.offset += 1;
|
|
620
|
-
if (type === 0x00) {
|
|
621
|
-
// custom encoding for 'undefined' (kept for backward-compatibility)
|
|
622
|
-
this.offset += 1;
|
|
623
|
-
return void 0;
|
|
624
|
-
}
|
|
625
|
-
return [type, this.bin(1)];
|
|
626
|
-
case 0xd5:
|
|
627
|
-
type = this.buffer.readInt8(this.offset);
|
|
628
|
-
this.offset += 1;
|
|
629
|
-
return [type, this.bin(2)];
|
|
630
|
-
case 0xd6:
|
|
631
|
-
type = this.buffer.readInt8(this.offset);
|
|
632
|
-
this.offset += 1;
|
|
633
|
-
if (type === -1) {
|
|
634
|
-
// timestamp 32
|
|
635
|
-
value = this.buffer.readUInt32BE(this.offset);
|
|
636
|
-
this.offset += 4;
|
|
637
|
-
return new Date(value * 1e3);
|
|
638
|
-
}
|
|
639
|
-
return [type, this.bin(4)];
|
|
640
|
-
case 0xd7:
|
|
641
|
-
type = this.buffer.readInt8(this.offset);
|
|
642
|
-
this.offset += 1;
|
|
643
|
-
if (type === 0x00) {
|
|
644
|
-
// custom date encoding (kept for backward-compatibility)
|
|
645
|
-
hi = this.buffer.readInt32BE(this.offset) * Math.pow(2, 32);
|
|
646
|
-
lo = this.buffer.readUInt32BE(this.offset + 4);
|
|
647
|
-
this.offset += 8;
|
|
648
|
-
return new Date(hi + lo);
|
|
649
|
-
}
|
|
650
|
-
if (type === -1) {
|
|
651
|
-
// timestamp 64
|
|
652
|
-
hi = this.buffer.readUInt32BE(this.offset);
|
|
653
|
-
lo = this.buffer.readUInt32BE(this.offset + 4);
|
|
654
|
-
this.offset += 8;
|
|
655
|
-
const s = (hi & 0x3) * 0x100000000 + lo;
|
|
656
|
-
return new Date(s * 1e3 + (hi >>> 2) / 1e6);
|
|
657
|
-
}
|
|
658
|
-
return [type, this.bin(8)];
|
|
659
|
-
case 0xd8:
|
|
660
|
-
type = this.buffer.readInt8(this.offset);
|
|
661
|
-
this.offset += 1;
|
|
662
|
-
return [type, this.bin(16)];
|
|
663
|
-
|
|
664
|
-
// str
|
|
665
|
-
case 0xd9:
|
|
666
|
-
length = this.buffer.readUInt8(this.offset);
|
|
667
|
-
this.offset += 1;
|
|
668
|
-
return this.str(length);
|
|
669
|
-
case 0xda:
|
|
670
|
-
length = this.buffer.readUInt16BE(this.offset);
|
|
671
|
-
this.offset += 2;
|
|
672
|
-
return this.str(length);
|
|
673
|
-
case 0xdb:
|
|
674
|
-
length = this.buffer.readUInt32BE(this.offset);
|
|
675
|
-
this.offset += 4;
|
|
676
|
-
return this.str(length);
|
|
677
|
-
|
|
678
|
-
// array
|
|
679
|
-
case 0xdc:
|
|
680
|
-
length = this.buffer.readUInt16BE(this.offset);
|
|
681
|
-
this.offset += 2;
|
|
682
|
-
return this.array(length);
|
|
683
|
-
case 0xdd:
|
|
684
|
-
length = this.buffer.readUInt32BE(this.offset);
|
|
685
|
-
this.offset += 4;
|
|
686
|
-
return this.array(length);
|
|
687
|
-
|
|
688
|
-
// map
|
|
689
|
-
case 0xde:
|
|
690
|
-
length = this.buffer.readUInt16BE(this.offset);
|
|
691
|
-
this.offset += 2;
|
|
692
|
-
return this.map(length);
|
|
693
|
-
case 0xdf:
|
|
694
|
-
length = this.buffer.readUInt32BE(this.offset);
|
|
695
|
-
this.offset += 4;
|
|
696
|
-
return this.map(length);
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
throw new Error('Could not parse');
|
|
700
|
-
};
|
|
701
|
-
|
|
702
|
-
function decode(buffer) {
|
|
703
|
-
const decoder = new Decoder(buffer);
|
|
704
|
-
const value = decoder.parse();
|
|
705
|
-
if (decoder.offset !== buffer.length) {
|
|
706
|
-
throw new Error((buffer.length - decoder.offset) + ' trailing bytes');
|
|
707
|
-
}
|
|
708
|
-
return value;
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
decode_1 = decode;
|
|
712
|
-
return decode_1;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
var hasRequiredLib;
|
|
716
|
-
|
|
717
|
-
function requireLib () {
|
|
718
|
-
if (hasRequiredLib) return lib;
|
|
719
|
-
hasRequiredLib = 1;
|
|
720
|
-
lib.encode = requireEncode();
|
|
721
|
-
lib.decode = requireDecode();
|
|
722
|
-
return lib;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
var libExports = requireLib();
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Main WilcoCrypt namespace.
|
|
729
|
-
*/
|
|
730
|
-
const wilcocrypt = {};
|
|
731
|
-
|
|
732
|
-
/**
|
|
733
|
-
* Internal WilcoCrypt utilities and constants.
|
|
734
|
-
*/
|
|
735
|
-
wilcocrypt._ = {};
|
|
736
|
-
|
|
737
|
-
/* =========================
|
|
738
|
-
Custom Error
|
|
739
|
-
========================= */
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Custom error class for all WilcoCrypt-specific errors.
|
|
743
|
-
*/
|
|
744
|
-
class WilcoCryptError extends Error {
|
|
745
|
-
/**
|
|
746
|
-
* @param {string} message - Human-readable error message
|
|
747
|
-
* @param {string} [code=WILCOCRYPT_ERROR] - Machine-readable error code
|
|
748
|
-
*/
|
|
749
|
-
constructor(message, code = 'WILCOCRYPT_ERROR') {
|
|
750
|
-
super(message);
|
|
751
|
-
this.name = 'WilcoCryptError';
|
|
752
|
-
this.code = code;
|
|
753
|
-
|
|
754
|
-
if (Error.captureStackTrace) {
|
|
755
|
-
Error.captureStackTrace(this, WilcoCryptError);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
wilcocrypt._.WilcoCryptError = WilcoCryptError;
|
|
761
|
-
|
|
762
|
-
/* =========================
|
|
763
|
-
Internal constants
|
|
764
|
-
========================= */
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Internal WilcoCrypt version.
|
|
768
|
-
* Must match exactly during decryption.
|
|
769
|
-
* @type {string}
|
|
770
|
-
*/
|
|
771
|
-
wilcocrypt._.VERSION = '2.1.0';
|
|
772
|
-
|
|
773
|
-
/**
|
|
774
|
-
* Minimum allowed password length.
|
|
775
|
-
* @type {number}
|
|
776
|
-
*/
|
|
777
|
-
wilcocrypt._.MIN_PASSWORD_LENGTH = 6;
|
|
778
|
-
|
|
779
|
-
/* =========================
|
|
780
|
-
Internal helpers
|
|
781
|
-
========================= */
|
|
782
|
-
|
|
783
|
-
/**
|
|
784
|
-
* Validates AES-256-GCM key and IV.
|
|
785
|
-
*
|
|
786
|
-
* @param {Buffer} key
|
|
787
|
-
* @param {Buffer} iv
|
|
788
|
-
* @throws {WilcoCryptError}
|
|
789
|
-
*/
|
|
790
|
-
wilcocrypt._.assertKeyAndIv = function (key, iv) {
|
|
791
|
-
if (!Buffer.isBuffer(key) || key.length !== 32) {
|
|
792
|
-
throw new WilcoCryptError(
|
|
793
|
-
'Invalid encryption key (expected 32-byte Buffer)',
|
|
794
|
-
'INVALID_KEY'
|
|
795
|
-
);
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
if (!Buffer.isBuffer(iv) || iv.length !== 12) {
|
|
799
|
-
throw new WilcoCryptError(
|
|
800
|
-
'Invalid IV (expected 12-byte Buffer for GCM)',
|
|
801
|
-
'INVALID_IV'
|
|
802
|
-
);
|
|
803
|
-
}
|
|
804
|
-
};
|
|
805
|
-
|
|
806
|
-
/**
|
|
807
|
-
* Validates password strength.
|
|
808
|
-
*
|
|
809
|
-
* @param {string} password
|
|
810
|
-
* @throws {WilcoCryptError}
|
|
811
|
-
*/
|
|
812
|
-
wilcocrypt._.assertPassword = function (password) {
|
|
813
|
-
if (typeof password !== 'string' || password.length < wilcocrypt._.MIN_PASSWORD_LENGTH) {
|
|
814
|
-
throw new WilcoCryptError(
|
|
815
|
-
`Password must be at least ${wilcocrypt._.MIN_PASSWORD_LENGTH} characters`,
|
|
816
|
-
'WEAK_PASSWORD'
|
|
817
|
-
);
|
|
818
|
-
}
|
|
819
|
-
};
|
|
820
|
-
|
|
821
|
-
/**
|
|
822
|
-
* Constant-time buffer comparison.
|
|
823
|
-
* Reserved for future extensions.
|
|
824
|
-
*
|
|
825
|
-
* @param {Buffer} a
|
|
826
|
-
* @param {Buffer} b
|
|
827
|
-
* @returns {boolean}
|
|
828
|
-
*/
|
|
829
|
-
wilcocrypt._.constantTimeEqual = function (a, b) {
|
|
830
|
-
if (a.length !== b.length) return false;
|
|
831
|
-
|
|
832
|
-
let result = 0;
|
|
833
|
-
for (let i = 0; i < a.length; i++) {
|
|
834
|
-
result |= a[i] ^ b[i];
|
|
835
|
-
}
|
|
836
|
-
return result === 0;
|
|
837
|
-
};
|
|
838
|
-
|
|
839
|
-
/* =========================
|
|
840
|
-
Crypto layer (internal)
|
|
841
|
-
========================= */
|
|
842
|
-
|
|
843
|
-
/**
|
|
844
|
-
* Encrypts raw data using AES-256-GCM.
|
|
845
|
-
*
|
|
846
|
-
* @param {Buffer} plainData
|
|
847
|
-
* @param {Buffer} key
|
|
848
|
-
* @param {Buffer} iv
|
|
849
|
-
* @returns {{ciphertext: Buffer, authTag: Buffer}}
|
|
850
|
-
*/
|
|
851
|
-
wilcocrypt._.encryptData = function (plainData, key, iv) {
|
|
852
|
-
wilcocrypt._.assertKeyAndIv(key, iv);
|
|
853
|
-
|
|
854
|
-
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
855
|
-
const encrypted = Buffer.concat([
|
|
856
|
-
cipher.update(plainData),
|
|
857
|
-
cipher.final()
|
|
858
|
-
]);
|
|
859
|
-
|
|
860
|
-
return {
|
|
861
|
-
ciphertext: encrypted,
|
|
862
|
-
authTag: cipher.getAuthTag()
|
|
863
|
-
};
|
|
864
|
-
};
|
|
865
|
-
|
|
866
|
-
/**
|
|
867
|
-
* Decrypts AES-256-GCM encrypted data.
|
|
868
|
-
*
|
|
869
|
-
* @param {string} cipherHex
|
|
870
|
-
* @param {string} authTagHex
|
|
871
|
-
* @param {Buffer} key
|
|
872
|
-
* @param {Buffer} iv
|
|
873
|
-
* @returns {Buffer}
|
|
874
|
-
*/
|
|
875
|
-
wilcocrypt._.decryptData = function (cipherHex, authTagHex, key, iv) {
|
|
876
|
-
wilcocrypt._.assertKeyAndIv(key, iv);
|
|
877
|
-
|
|
878
|
-
try {
|
|
879
|
-
const decipher = createDecipheriv('aes-256-gcm', key, iv);
|
|
880
|
-
decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
|
|
881
|
-
|
|
882
|
-
return Buffer.concat([
|
|
883
|
-
decipher.update(Buffer.from(cipherHex, 'hex')),
|
|
884
|
-
decipher.final()
|
|
885
|
-
]);
|
|
886
|
-
} catch {
|
|
887
|
-
throw new WilcoCryptError(
|
|
888
|
-
'Decryption failed (invalid password, corrupted data, or tampered file)',
|
|
889
|
-
'DECRYPTION_FAILED'
|
|
890
|
-
);
|
|
891
|
-
}
|
|
892
|
-
};
|
|
893
|
-
|
|
894
|
-
/* =========================
|
|
895
|
-
Public API
|
|
896
|
-
========================= */
|
|
897
|
-
|
|
898
|
-
wilcocrypt.encryptData = function (plaindata, password, gzip = true) {
|
|
899
|
-
wilcocrypt._.assertPassword(password);
|
|
900
|
-
|
|
901
|
-
let gzipData;
|
|
902
|
-
if (gzip) {
|
|
903
|
-
gzipData = gzipSync(plaindata);
|
|
904
|
-
} else {
|
|
905
|
-
gzipData = plaindata;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
const iv = randomBytes(12);
|
|
909
|
-
const salt = randomBytes(16);
|
|
910
|
-
|
|
911
|
-
const key = scryptSync(password, salt, 32);
|
|
912
|
-
|
|
913
|
-
const { ciphertext, authTag } = wilcocrypt._.encryptData(gzipData, key, iv);
|
|
914
|
-
|
|
915
|
-
const envelope = {
|
|
916
|
-
payload: ciphertext.toString('hex'),
|
|
917
|
-
authTag: authTag.toString('hex'),
|
|
918
|
-
salt: salt.toString('hex'),
|
|
919
|
-
iv: iv.toString('hex'),
|
|
920
|
-
version: wilcocrypt._.VERSION
|
|
921
|
-
};
|
|
922
|
-
|
|
923
|
-
return libExports.encode(envelope);
|
|
924
|
-
};
|
|
925
|
-
|
|
926
|
-
wilcocrypt.decryptData = function (encryptedData, password, gzip = true) {
|
|
927
|
-
wilcocrypt._.assertPassword(password);
|
|
928
|
-
|
|
929
|
-
let envelope;
|
|
930
|
-
try {
|
|
931
|
-
envelope = libExports.decode(encryptedData);
|
|
932
|
-
} catch {
|
|
933
|
-
throw new WilcoCryptError(
|
|
934
|
-
'Invalid encrypted data format (not MessagePack)',
|
|
935
|
-
'INVALID_FORMAT'
|
|
936
|
-
);
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
if (envelope.version !== wilcocrypt._.VERSION) {
|
|
940
|
-
throw new WilcoCryptError(
|
|
941
|
-
`Version mismatch (expected ${wilcocrypt._.VERSION}, got ${envelope.version})`,
|
|
942
|
-
'VERSION_MISMATCH'
|
|
943
|
-
);
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
const key = scryptSync(password, Buffer.from(envelope.salt, 'hex'), 32);
|
|
947
|
-
|
|
948
|
-
const decrypted = wilcocrypt._.decryptData(
|
|
949
|
-
envelope.payload,
|
|
950
|
-
envelope.authTag,
|
|
951
|
-
key,
|
|
952
|
-
Buffer.from(envelope.iv, 'hex')
|
|
953
|
-
);
|
|
954
|
-
|
|
955
|
-
try {
|
|
956
|
-
if (gzip) {
|
|
957
|
-
return gunzipSync(decrypted);
|
|
958
|
-
} else {
|
|
959
|
-
return decrypted;
|
|
960
|
-
}
|
|
961
|
-
} catch {
|
|
962
|
-
throw new WilcoCryptError(
|
|
963
|
-
'Decryption succeeded but decompression failed (data may be corrupted or not compressed)',
|
|
964
|
-
'DECOMPRESSION_FAILED'
|
|
965
|
-
);
|
|
966
|
-
}
|
|
967
|
-
};
|
|
968
|
-
|
|
969
|
-
wilcocrypt.encryptFile = function (filePath, password, gzip = true) {
|
|
970
|
-
const fileData = readFileSync(filePath);
|
|
971
|
-
const encryptedData = wilcocrypt.encryptData(fileData, password, gzip);
|
|
972
|
-
writeFileSync(`${filePath}.enc`, encryptedData);
|
|
973
|
-
};
|
|
974
|
-
|
|
975
|
-
wilcocrypt.decryptFile = function (filePath, password, gzip = true) {
|
|
976
|
-
if (!filePath.endsWith('.enc')) {
|
|
977
|
-
throw new WilcoCryptError(
|
|
978
|
-
'Invalid file extension (expected .enc)',
|
|
979
|
-
'INVALID_FILE_EXTENSION'
|
|
980
|
-
);
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
const encryptedData = readFileSync(filePath);
|
|
984
|
-
return wilcocrypt.decryptData(encryptedData, password, gzip);
|
|
985
|
-
};
|
|
986
|
-
|
|
987
|
-
export { wilcocrypt as default };
|