-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptopack.js
executable file
·759 lines (670 loc) · 23.9 KB
/
cryptopack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#!/usr/bin/env node
var path = require('path');
var fse = require('fs-extra');
var chalk = require('chalk');
var bytes = require('bytes');
var utils = require('./lib/utils');
var cryptor = require('./lib/cryptor');
var log = require('single-line-log').stdout;
var cryptoUtils = require('./lib/crypto-utils');
var CryptoPack = require('./lib/crypto-pack');
var VERSION = '0.0.3';
function printUsage() {
console.log('cryptopack - version ' + VERSION + ', cryptor - version ' + cryptor.getVersion());
console.log('');
console.log('Usage:');
console.log(' cryptopack create [OPTIONS] <input-dir> [output-pack | output-dir]');
console.log(' cryptopack extract [OPTIONS] <input-pack> [output-dir] [entries...]');
console.log(' cryptopack list [OPTIONS] <input-pack> [entries...]');
console.log(' cryptopack index [OPTIONS] <input-pack>');
console.log(' cryptopack browse [OPTIONS] <input-pack>');
console.log(' cryptopack mount [OPTIONS] <input-pack> <mount-point>');
console.log('');
console.log(' cryptopack config');
console.log(' cryptopack config --set-passphrase');
console.log(' cryptopack config --set-salt');
console.log(' cryptopack config --clear-encryption-key');
console.log(' cryptopack gen-enc-key');
console.log('');
console.log('OPTIONS:');
console.log('');
console.log(' --force -f : force replace or update existing pack file');
console.log(' --verbose -v : verbose');
console.log(' --progress : show progress');
console.log('');
console.log(' --recursive -r : scan input directory recursively (default: yes)');
console.log(' --no-recursive -n : only scan input directory (not recursively)');
console.log('');
console.log(' --default -d : use default encryption key (if exists)');
console.log(' --enc-key=STRING : use custom encryption key');
console.log(' --salt=STRING : use custom salt');
console.log('');
console.log(' --min-size=<NUMBER>[GB,MB,KB] : scan for files with minimum size (default: not set)');
console.log(' --max-size=<NUMBER>[GB,MB,KB] : scan for files with maximum size (default: not set)');
console.log('');
console.log(' --exclude-dir=<STRING> : exclude directories contain this string');
console.log(' --exclude-file=<STRING> : exclude files contain this string');
console.log('');
}
if (process.argv.length < 3 || process.argv.indexOf('--help') >= 0) {
printUsage();
process.exit();
}
var command = process.argv[2];
var argv = [];
var options = { recursive: true };
for (var i = 3; i < process.argv.length; i++) {
if (process.argv[i] == '--default' || process.argv[i] == '-d') {
options.default = true;
} else if (process.argv[i] == '--ignore-errors') {
options.ignore_errors = true;
} else if (process.argv[i] == '--stop-if-errors' || process.argv[i] == '-e') {
options.ignore_errors = false;
} else if (process.argv[i] == '--recursive' || process.argv[i] == '-r') {
options.recursive = true;
} else if (process.argv[i] == '--no-recursive' || process.argv[i] == '-n') {
options.recursive = false;
} else if (process.argv[i] == '--force' || process.argv[i] == '-f') {
options.force = true;
} else if (process.argv[i] == '--verbose' || process.argv[i] == '-v') {
options.verbose = true;
} else if (process.argv[i].indexOf('--exclude-dir=') == 0) {
var dir = process.argv[i].split('=')[1];
options.exclude_dirs = options.exclude_dirs || [];
if (options.exclude_dirs.indexOf(dir) == -1) options.exclude_dirs.push(dir);
} else if (process.argv[i].indexOf('--exclude-file=') == 0) {
var file = process.argv[i].split('=')[1];
options.exclude_files = options.exclude_files || [];
if (options.exclude_files.indexOf(file) == -1) options.exclude_files.push(file);
} else if (process.argv[i].indexOf('--') == 0) {
var arg = process.argv[i];
if (arg.indexOf("=") > 0) {
var arg_kv = arg.split('=');
arg = arg_kv[0];
arg = arg.replace('--','');
arg = utils.replaceAll(arg, '-', '_');
options[arg] = arg_kv[1];
} else {
arg = arg.replace('--','');
arg = utils.replaceAll(arg, '-', '_');
options[arg] = true;
}
} else {
argv.push(process.argv[i]);
}
}
if (typeof options.ignore_errors == 'undefined') {
options.ignore_errors = true;
}
if (options.version) {
console.log('cryptopack - version ' + VERSION + ', cryptor - version ' + cryptor.getVersion());
process.exit();
}
// ---
if (options.min_size) {
var min_size = utils.parseSize(options.min_size);
if (isNaN(min_size)) {
console.log('Invalid min size parameter');
process.exit();
}
options.min_file_size = min_size;
console.log('Min. file size:', bytes(min_size));
}
if (options.max_size) {
var max_size = utils.parseSize(options.max_size);
if (isNaN(max_size)) {
console.log('Invalid max size parameter');
process.exit();
}
options.max_file_size = max_size;
console.log('Max. file size:', bytes(max_size));
}
// ---
var config = {};
var config_dir = path.join(utils.getUserHome(), '.jul11co', 'crypto-tools');
fse.ensureDirSync(config_dir);
var config_file = path.join(config_dir, 'config.json');
if (utils.fileExists(config_file)) {
config = utils.loadFromJsonFile(config_file);
}
var crypto_salt = options.salt || config.salt || 'jul11co-crypto-tools';
var getEncryptionKey = function(opts, callback) {
if (typeof opts == 'function') {
callback = opts;
opts = {};
}
if (options.default && config.enc_key) {
console.log('Use default encryption key from config:', config_file);
return callback(null, config.enc_key);
} else if (options.enc_key) {
console.log('Use encryption key from arguments');
return callback(null, options.enc_key);
} else if (options.passphrase) {
console.log('Use passphrase from arguments');
var ENC_KEY = cryptor.generateEncryptionKey(options.passphrase, crypto_salt);
return callback(null, ENC_KEY);
} else {
cryptoUtils.getInputPassphrase(opts, function(err, passphrase) {
if (err) {
return callback(err);
}
var ENC_KEY = cryptor.generateEncryptionKey(passphrase, crypto_salt);
return callback(null, ENC_KEY);
});
}
}
/////
if (command == 'config') {
if (options.set_passphrase) {
getEncryptionKey({verify: true}, function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
config.enc_key = enc_key;
utils.saveToJsonFile(config, config_file);
console.log('Config saved.');
process.exit();
});
} else if (options.clear_encryption_key) {
delete config.enc_key;
utils.saveToJsonFile(config, config_file);
console.log('Config saved.');
process.exit();
} else if (options.set_salt) {
cryptoUtils.getInputSalt(function(err, salt) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
config.salt = salt;
utils.saveToJsonFile(config, config_file);
console.log('Config saved.');
process.exit();
});
} else {
console.log(config);
process.exit();
}
} else if (command == 'gen-enc-key') {
getEncryptionKey({verify: true}, function(err, enc_key) {
if (err) {
// console.log(err);
process.exit();
}
console.log('Encryption key:', enc_key);
});
} else if (command == 'create') {
if (argv.length < 1) {
printUsage();
process.exit();
}
var INPUT_DIR = path.resolve(argv[0]);
if (!utils.directoryExists(INPUT_DIR)) {
console.log('Directory not found:', INPUT_DIR);
process.exit();
}
console.log('Input directory: ' + INPUT_DIR
+ (options.recursive ? chalk.magenta(' (recursive)') : chalk.yellow(' (without recursive)')));
options.input_dir = INPUT_DIR;
var default_output_pack = path.join(path.dirname(INPUT_DIR), path.basename(INPUT_DIR) + '.cryptopack');
var OUTPUT_PACK = default_output_pack;
if (argv[1] && utils.directoryExists(path.resolve(argv[1]))) {
var output_dir = path.resolve(argv[1]);
OUTPUT_PACK = path.join(output_dir, path.basename(INPUT_DIR) + '.cryptopack');
} else if (argv[1]) {
OUTPUT_PACK = path.resolve(argv[1]);
}
if (!options.force && utils.fileExists(OUTPUT_PACK)) {
console.log(chalk.red('Cryptopack exists:'), OUTPUT_PACK);
console.log(chalk.grey('Hint: Add --force or -f to update existing cryptopack.'));
process.exit();
}
console.log('Cryptopack: ' + OUTPUT_PACK);
getEncryptionKey({verify: true}, function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
var cryptopack = new CryptoPack(OUTPUT_PACK, enc_key);
options.onFileEncrypt = function(original_file, encrypted_file, progress) {
log(chalk.magenta('Encrypt:'), progress.current + '/' + progress.total,
path.relative(INPUT_DIR, original_file.path), chalk.magenta(bytes(original_file.size)));
}
options.onFileEncrypted = function(original_file, encrypted_file, progress) {
log(chalk.green('Encrypted:'), progress.current + '/' + progress.total,
path.relative(INPUT_DIR, original_file.path), chalk.magenta(bytes(original_file.size)));
}
options.onFileEncryptFailed = function(err, original_file, encrypted_file, progress) {
log(chalk.red('Encrypt failed:'), progress.current + '/' + progress.total,
original_file.path, chalk.magenta(bytes(original_file.size)), err.message);
}
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
process.on('SIGINT', function() {
console.log("\nCaught interrupt signal");
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
cryptopack.pack(INPUT_DIR, options, function(err, result) {
if (err) {
console.log('Encrypt folder error!');
console.log(err);
} else if (result) {
console.log('----');
console.log('Directory:', result.dirs.length + ' director' + ((result.dirs.length != 1) ? 'ies.': 'y.'));
console.log('Total:', result.files.length + ' file' + ((result.files.length != 1) ? 's.': '.'));
// console.log(result.processed.length + ' file' + ((result.processed.length != 1) ? 's': '')
// + ' (' + bytes(result.total_size) + ') processed.');
if (result.encrypted && result.encrypted.length) {
console.log('Encrypted:', chalk.magenta(result.encrypted.length
+ ' file' + ((result.encrypted.length != 1) ? 's': '')
+ ' (' + bytes(result.encrypted_size) + ').'));
}
if (result.errors && result.errors.length) {
console.log('----');
console.log(chalk.red(result.errors.length + ' errors.'));
result.errors.forEach(function(error) {
console.log(error);
});
}
}
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
} else if (result) {
if (result.updated) {
console.log('Updating existing cryptopack... OK');
console.log('Cryptopack updated:', OUTPUT_PACK,
chalk.magenta(result.stats ? bytes(result.stats['size']) : ''));
} else if (result.created) {
console.log('Creating new cryptopack... OK');
console.log('Cryptopack created:', OUTPUT_PACK,
chalk.magenta(result.stats ? bytes(result.stats['size']) : ''));
}
}
process.exit();
})
});
});
});
} else if (command == 'extract') {
if (argv.length < 1) {
printUsage();
process.exit();
}
var INPUT_PACK = path.resolve(argv[0]);
if (!utils.fileExists(INPUT_PACK)) {
console.log(chalk.red('Cryptopack not found:'), INPUT_PACK);
process.exit();
}
console.log('Cryptopack: ' + INPUT_PACK);
var default_output_dir = path.join(path.dirname(INPUT_PACK), path.basename(INPUT_PACK, path.extname(INPUT_PACK)));
var OUTPUT_DIR = argv[1] ? path.resolve(argv[1]) : default_output_dir;
if (!options.force && utils.directoryExists(OUTPUT_DIR)) {
console.log(chalk.red('Directory exists:'), OUTPUT_DIR);
console.log(chalk.grey('Hint: Add --force or -f to merge/replace files in existing directory.'));
process.exit();
}
console.log('Extract to: ' + OUTPUT_DIR);
options.output_dir = OUTPUT_DIR;
var entries_to_extract = [];
if (argv.length > 2) {
for (var i = 2; i < argv.length; i++) {
entries_to_extract.push(argv[i]);
}
}
if (entries_to_extract.length) {
options.extract_entries = entries_to_extract;
}
options.read_only = true;
getEncryptionKey(function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
var cryptopack = new CryptoPack(INPUT_PACK, enc_key);
if (options.progress) {
options.onEntry = function(entry) {
console.log((entry.type || 'File')[0], entry.path, chalk.magenta(bytes(entry.size)));
}
}
options.onFileDecrypt = function(decrypted_file, encrypted_file, progress) {
log(chalk.magenta('Decrypt:'), progress.current + '/' + progress.total,
decrypted_file.path, chalk.magenta(bytes(decrypted_file.size)));
}
options.onFileDecrypted = function(decrypted_file, encrypted_file, progress) {
log(chalk.green('Decrypted:'), progress.current + '/' + progress.total,
decrypted_file.path, chalk.magenta(bytes(decrypted_file.size)));
}
options.onFileDecryptFailed = function(err, decrypted_file, encrypted_file, progress) {
log(chalk.red('Decrypted failed:'), progress.current + '/' + progress.total,
decrypted_file.path, chalk.magenta(bytes(decrypted_file.size)));
}
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
process.on('SIGINT', function() {
console.log("\nCaught interrupt signal");
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
cryptopack.extract(OUTPUT_DIR, options, function(err, result) {
if (err) {
console.log('Extract crypto pack failed!');
console.log(err);
} else if (result) {
console.log('----');
console.log('Total:', result.files.length + ' file' + ((result.files.length != 1) ? 's.': '.'));
// console.log(result.processed.length + ' file' + ((result.processed.length != 1) ? 's': '')
// + ' (' + bytes(result.total_size) + ') processed.');
if (result.decrypted && result.decrypted.length) {
console.log('Decrypted:', chalk.magenta(result.decrypted.length
+ ' file' + ((result.decrypted.length != 1) ? 's': '')
+ ' (' + bytes(result.decrypted_size) + ').'));
}
if (result.errors && result.errors.length) {
console.log('----');
console.log(chalk.red(result.errors.length + ' errors.'));
result.errors.forEach(function(error) {
console.log(error);
});
}
}
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
});
});
} else if (command == 'list') {
if (argv.length < 1) {
printUsage();
process.exit();
}
var INPUT_PACK = path.resolve(argv[0]);
if (!utils.fileExists(INPUT_PACK)) {
console.log(chalk.red('Cryptopack not found:'), INPUT_PACK);
process.exit();
}
console.log('Cryptopack: ' + INPUT_PACK);
var entries_to_list = [];
if (argv.length > 1) {
for (var i = 1; i < argv.length; i++) {
entries_to_list.push(argv[i]);
}
}
if (entries_to_list.length) {
options.list_entries = entries_to_list;
}
getEncryptionKey(function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
var cryptopack = new CryptoPack(INPUT_PACK, enc_key);
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
process.on('SIGINT', function() {
console.log("\nCaught interrupt signal");
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
options.onFileInfo = function(file_info, index) {
console.log(utils.padLeft(''+count, 6)+'.',
chalk.magenta(utils.padLeft(bytes(file_info.s), 8)), file_info.p);
}
cryptopack.list(options, function(err, result) {
if (err) {
console.log('List files from crypto pack failed!');
console.log(err);
} if (result) {
console.log('----');
console.log('Total files:', result.count);
console.log('Total size:', bytes(result.total_size));
if (result.largest_size>0) {
console.log('Largest file:', chalk.magenta(bytes(result.largest_file.size)), result.largest_file.path);
}
}
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
});
});
} else if (command == 'browse') {
if (argv.length < 1) {
printUsage();
process.exit();
}
var INPUT_PACK = path.resolve(argv[0]);
if (!utils.fileExists(INPUT_PACK)) {
console.log(chalk.red('Cryptopack not found:'), INPUT_PACK);
process.exit();
}
console.log('Cryptopack: ' + INPUT_PACK);
options.read_only = true;
getEncryptionKey(function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
var cryptopack = new CryptoPack(INPUT_PACK, enc_key);
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
process.on('SIGINT', function() {
console.log("\nCaught interrupt signal");
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
cryptopack.browse(options, function(err, listen_port) {
if (err) {
console.log('Browse crypto pack failed!');
console.log(err);
// Unload cryptopack
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
} else {
console.log('Crypto pack browser started. Listening on http://localhost:' + listen_port);
process.on('SIGINT', function () {
console.log("\nCaught interrupt signal");
// Unload cryptopack
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
});
});
}
});
});
});
} else if (command == 'mount') {
if (argv.length < 2) {
printUsage();
process.exit();
}
var INPUT_PACK = path.resolve(argv[0]);
if (!utils.fileExists(INPUT_PACK)) {
console.log(chalk.red('Cryptopack not found:'), INPUT_PACK);
process.exit();
}
console.log('Cryptopack: ' + INPUT_PACK);
var MOUNT_POINT = path.resolve(argv[1]);
options.mount_point = MOUNT_POINT;
console.log('Mount point: ' + MOUNT_POINT);
options.read_only = true;
getEncryptionKey(function(err, enc_key) {
if (err) {
// console.log(err);
console.log('');
process.exit();
}
var cryptopack = new CryptoPack(INPUT_PACK, enc_key);
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
process.on('SIGINT', function() {
console.log("\nCaught interrupt signal");
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
})
});
cryptopack.mount(MOUNT_POINT, options, function(err, mount_point) {
if (err) {
console.log('Mount crypto pack failed!');
console.log(err);
// Unload cryptopack
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
});
} else if (mount_point) {
console.log('Crypto pack mounted on ' + mount_point.path);
process.on('SIGINT', function () {
console.log("\nCaught interrupt signal");
// Unmount cryptopack
mount_point.unmount(function(err) {
if (err) {
console.log('Unmount failed!', mount_point.path)
} else {
console.log('Unmounted: ' + mount_point.path);
}
// Unload cryptopack
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
});
});
});
}
});
});
});
} else if (command == 'index') {
if (argv.length < 1) {
printUsage();
process.exit();
}
var INPUT_PACK = path.resolve(argv[0]);
if (!utils.fileExists(INPUT_PACK)) {
console.log(chalk.red('Cryptopack not found:'), INPUT_PACK);
process.exit();
}
console.log('Cryptopack: ' + INPUT_PACK);
if (options.progress) {
options.onEntry = function(entry) {
console.log((entry.type || 'File')[0], entry.name || entry.path, chalk.magenta(bytes(entry.size)));
}
}
options.index_file = argv[1];
var cryptopack = new CryptoPack(INPUT_PACK);
cryptopack.load(options, function(err) {
if (err) {
console.log('Load crypto pack failed!');
// console.log(err);
console.log(chalk.red(err.message));
process.exit();
}
cryptopack.index(options, function(err, result) {
if (err) {
console.log(err);
} else if (result) {
if (result.index_stats) {
console.log('Entries count:', result.index_stats.entriesCount);
console.log('Total size:', bytes(result.index_stats.totalSize));
}
if (result.index_file_stats) {
console.log('Index file created:', idx_file, bytes(stat['size']));
} else {
console.log('Cannot generate index file!', idx_file);
}
}
cryptopack.unload(function(err) {
if (err) {
console.log('Unload crypto pack failed!');
console.log(err);
}
process.exit();
});
});
});
} else {
printUsage();
process.exit();
}