From f5f9c51ea0a84973b369e1d87e342847e334a1f5 Mon Sep 17 00:00:00 2001 From: William Zeni Date: Sun, 16 Jul 2023 01:05:51 +0200 Subject: [PATCH 1/9] hashing files, need to store tree directory before --- src/index.js | 5 +++-- src/warshield.js | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 68a4a47..a5ed370 100644 --- a/src/index.js +++ b/src/index.js @@ -79,8 +79,9 @@ async function encrypt(file, { verbose, trace, tmp }) { } const start = process.hrtime(); - - const encryption = warshield.encryptRecursive(file, key, tmp); + warshield.hideFilesName(path.join(__dirname, ".."),file); + sha = warshield.hideName(file); + const encryption = warshield.encryptRecursive(sha, key, tmp); encryption.on('crawl-found', filename => { if (verbose) { diff --git a/src/warshield.js b/src/warshield.js index 2a19063..c47f5c8 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -19,6 +19,26 @@ function* arrayLoop(arr, action) { } } +function hideName(name){ + return crypto.createHash('sha256').update(name).digest('hex'); +} + +function hideFilesName(directory, file){ + var filedir = path.join(directory, file); + var shaname = hideName(file); + var shadir = path.join(directory, shaname) + fs.renameSync(filedir, shadir); + var stat = fs.statSync(shadir); + + if(stat.isDirectory()){ + var files=fs.readdirSync(shadir); + for(var i in files){ + hideFilesName(shadir, files[i]); + } + } +} + + /** * Hash a key to make it valid for WarShield's encryption algorithm * @@ -92,7 +112,7 @@ function encryptFile(file, key, tmp) { // Create cipher stream const cipher = encryptStream(derivedKey); cipher.stream.on('error', ERROR); - + console.log(file); const source_rs = fs.createReadStream(file).on('error', ERROR); const target_ws = fs.createWriteStream(targetpath).on('error', ERROR); @@ -364,7 +384,7 @@ function decryptRecursive(file, key, tmp) { */ function getFiles(directory) { const em = new EventEmitter(); - + console.log(directory); fs.readdir(directory, (err, files) => { if (err) { em.emit('failed', directory, err); @@ -401,6 +421,7 @@ function getFiles(directory) { end(); }); } else { + console.log(filepath); em.emit('found', filepath); end(); } @@ -419,4 +440,6 @@ module.exports = { decryptFile, encryptRecursive, decryptRecursive, + hideFilesName, + hideName } \ No newline at end of file From 3a33926e665c8934833285c6e7267d1380d54df9 Mon Sep 17 00:00:00 2001 From: William Zeni Date: Sun, 16 Jul 2023 22:27:44 +0200 Subject: [PATCH 2/9] hiding files and store folders and files metadata --- src/index.js | 12 ++++++++++-- src/warshield.js | 5 +++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index a5ed370..6c9e52b 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const Spinner = require('./Spinner'); const os = require('os'); const path = require('path'); const mkdirp = require('mkdirp'); +const fs= require('fs'); function clearline() { process.stdout.write('\u001b[1G\u001b[2K'); @@ -79,10 +80,17 @@ async function encrypt(file, { verbose, trace, tmp }) { } const start = process.hrtime(); - warshield.hideFilesName(path.join(__dirname, ".."),file); + var encryption = warshield.encryptRecursive(file, key, tmp); + + //Hide filenames + var infoObj = {}; //saved folders data sha = warshield.hideName(file); - const encryption = warshield.encryptRecursive(sha, key, tmp); + warshield.hideFilesName(path.join(__dirname, ".."),file, infoObj);//encrypt names and store hashes in dictonary + var metadatadir = path.join(__dirname, "..", sha, "metadata.json"); + fs.writeFileSync(metadatadir, JSON.stringify(infoObj, null, 2)); //write names on jsonfile + encryption= warshield.encryptRecursive(metadatadir, key, tmp);//crypt metadatafile + encryption.on('crawl-found', filename => { if (verbose) { display_verbose("Added file", filename); diff --git a/src/warshield.js b/src/warshield.js index c47f5c8..007fea1 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -23,17 +23,18 @@ function hideName(name){ return crypto.createHash('sha256').update(name).digest('hex'); } -function hideFilesName(directory, file){ +function hideFilesName(directory, file, infoObj={}){ var filedir = path.join(directory, file); var shaname = hideName(file); var shadir = path.join(directory, shaname) fs.renameSync(filedir, shadir); var stat = fs.statSync(shadir); + infoObj[shaname]=file; if(stat.isDirectory()){ var files=fs.readdirSync(shadir); for(var i in files){ - hideFilesName(shadir, files[i]); + hideFilesName(shadir, files[i], infoObj); } } } From ce7b40cfcdd2d5ce8af4d879907e8b4562e6c29c Mon Sep 17 00:00:00 2001 From: William Zeni Date: Thu, 3 Aug 2023 23:39:25 +0200 Subject: [PATCH 3/9] Revert "hiding files and store folders and files metadata" This reverts commit 3a33926e665c8934833285c6e7267d1380d54df9. --- src/index.js | 12 ++---------- src/warshield.js | 5 ++--- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 6c9e52b..a5ed370 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,6 @@ const Spinner = require('./Spinner'); const os = require('os'); const path = require('path'); const mkdirp = require('mkdirp'); -const fs= require('fs'); function clearline() { process.stdout.write('\u001b[1G\u001b[2K'); @@ -80,17 +79,10 @@ async function encrypt(file, { verbose, trace, tmp }) { } const start = process.hrtime(); - var encryption = warshield.encryptRecursive(file, key, tmp); - - //Hide filenames - var infoObj = {}; //saved folders data + warshield.hideFilesName(path.join(__dirname, ".."),file); sha = warshield.hideName(file); - warshield.hideFilesName(path.join(__dirname, ".."),file, infoObj);//encrypt names and store hashes in dictonary - var metadatadir = path.join(__dirname, "..", sha, "metadata.json"); - fs.writeFileSync(metadatadir, JSON.stringify(infoObj, null, 2)); //write names on jsonfile - encryption= warshield.encryptRecursive(metadatadir, key, tmp);//crypt metadatafile + const encryption = warshield.encryptRecursive(sha, key, tmp); - encryption.on('crawl-found', filename => { if (verbose) { display_verbose("Added file", filename); diff --git a/src/warshield.js b/src/warshield.js index 007fea1..c47f5c8 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -23,18 +23,17 @@ function hideName(name){ return crypto.createHash('sha256').update(name).digest('hex'); } -function hideFilesName(directory, file, infoObj={}){ +function hideFilesName(directory, file){ var filedir = path.join(directory, file); var shaname = hideName(file); var shadir = path.join(directory, shaname) fs.renameSync(filedir, shadir); var stat = fs.statSync(shadir); - infoObj[shaname]=file; if(stat.isDirectory()){ var files=fs.readdirSync(shadir); for(var i in files){ - hideFilesName(shadir, files[i], infoObj); + hideFilesName(shadir, files[i]); } } } From 20b4fddddc40cc724f621cf7fd7271b378957b5a Mon Sep 17 00:00:00 2001 From: William Zeni Date: Thu, 3 Aug 2023 23:39:37 +0200 Subject: [PATCH 4/9] Revert "hashing files, need to store tree directory before" This reverts commit f5f9c51ea0a84973b369e1d87e342847e334a1f5. --- src/index.js | 5 ++--- src/warshield.js | 27 ++------------------------- 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/src/index.js b/src/index.js index a5ed370..68a4a47 100644 --- a/src/index.js +++ b/src/index.js @@ -79,9 +79,8 @@ async function encrypt(file, { verbose, trace, tmp }) { } const start = process.hrtime(); - warshield.hideFilesName(path.join(__dirname, ".."),file); - sha = warshield.hideName(file); - const encryption = warshield.encryptRecursive(sha, key, tmp); + + const encryption = warshield.encryptRecursive(file, key, tmp); encryption.on('crawl-found', filename => { if (verbose) { diff --git a/src/warshield.js b/src/warshield.js index c47f5c8..2a19063 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -19,26 +19,6 @@ function* arrayLoop(arr, action) { } } -function hideName(name){ - return crypto.createHash('sha256').update(name).digest('hex'); -} - -function hideFilesName(directory, file){ - var filedir = path.join(directory, file); - var shaname = hideName(file); - var shadir = path.join(directory, shaname) - fs.renameSync(filedir, shadir); - var stat = fs.statSync(shadir); - - if(stat.isDirectory()){ - var files=fs.readdirSync(shadir); - for(var i in files){ - hideFilesName(shadir, files[i]); - } - } -} - - /** * Hash a key to make it valid for WarShield's encryption algorithm * @@ -112,7 +92,7 @@ function encryptFile(file, key, tmp) { // Create cipher stream const cipher = encryptStream(derivedKey); cipher.stream.on('error', ERROR); - console.log(file); + const source_rs = fs.createReadStream(file).on('error', ERROR); const target_ws = fs.createWriteStream(targetpath).on('error', ERROR); @@ -384,7 +364,7 @@ function decryptRecursive(file, key, tmp) { */ function getFiles(directory) { const em = new EventEmitter(); - console.log(directory); + fs.readdir(directory, (err, files) => { if (err) { em.emit('failed', directory, err); @@ -421,7 +401,6 @@ function getFiles(directory) { end(); }); } else { - console.log(filepath); em.emit('found', filepath); end(); } @@ -440,6 +419,4 @@ module.exports = { decryptFile, encryptRecursive, decryptRecursive, - hideFilesName, - hideName } \ No newline at end of file From dad5d5457311cd74cb2f834c5cb1e4b2b5bd3013 Mon Sep 17 00:00:00 2001 From: William Zeni Date: Fri, 4 Aug 2023 15:04:53 +0200 Subject: [PATCH 5/9] added sha filenames option for encryption --- src/UserInterface.js | 28 ++++++++++++++++++++----- src/index.js | 45 ++++++++++++++++++++++++++------------- src/warshield.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 19 deletions(-) diff --git a/src/UserInterface.js b/src/UserInterface.js index 9e9cb3b..0b024f3 100644 --- a/src/UserInterface.js +++ b/src/UserInterface.js @@ -23,6 +23,19 @@ function showHeader() { }); } +async function askHideNames() { + showHeader(); + return inquirer.prompt({ + name: 'hide', + type: 'list', + message: 'Do you also want to encrypt your files name?', + choices: ['Yes', 'No'], + default: 'Yes' + }).then((input) => { + return input.hide; + }) +} + async function askMode() { showHeader(); return inquirer.prompt({ @@ -42,7 +55,7 @@ async function askOptions() { name: 'options', type: 'checkbox', message: 'Choose options you want to enable:', - choices: ['verbose', 'trace'], + choices: ['verbose', 'trace errors'], }).then((input) => { return input.options; }) @@ -62,18 +75,23 @@ async function askFile() { } async function showUI() { - const options = { parent: { verbose: false, trace: false, tmp: false } }; + const options = {verbose: false, trace: false, tmp: false, hide: true }; const mode = await askMode(); const enabledOptions = await askOptions(); - options.parent.verbose = enabledOptions.includes('verbose'); - options.parent.trace = enabledOptions.includes('trace'); + options.verbose = enabledOptions.includes('verbose'); + options.trace = enabledOptions.includes('trace errors'); const file = await askFile(); - showHeader(); if (mode === MODE_DECRYPT) { + showHeader(); await decrypt(file, options) } else { + const hide = await askHideNames(); + if(hide === 'No'){ + options.hide=false; + } + showHeader() await encrypt(file, options) } } diff --git a/src/index.js b/src/index.js index 68a4a47..d8fb855 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const Spinner = require('./Spinner'); const os = require('os'); const path = require('path'); const mkdirp = require('mkdirp'); +const optsArg = require('mkdirp/lib/opts-arg'); function clearline() { process.stdout.write('\u001b[1G\u001b[2K'); @@ -51,7 +52,11 @@ function display_verbose(message, filename, trace = false, error = "") { stream.write('\r\n'); } -async function encrypt(file, { verbose, trace, tmp }) { +async function encrypt(file, {verbose, trace, tmp, hide}) { + /*const verbose = options.verbose; + const trace = options.trace; + const tmp = options.tmp; + const hide = options.hide; */ try { const key = await ask_password(true); @@ -127,24 +132,36 @@ async function encrypt(file, { verbose, trace, tmp }) { }); encryption.on('end', () => { - const diff = process.hrtime(start); - if (spinner) { - spinner.stop(); - } + function finish(){ + const diff = process.hrtime(start); + if (spinner) { + spinner.stop(); + } - if (verbose) { - process.stdout.write('\r\n'); - } else { - clearline(); + if (verbose) { + process.stdout.write('\r\n'); + } else { + clearline(); + } + + console.log(`Finished encrypting files!`); + console.log(`Elapsed time: ${((diff[0] * 1e9 + diff[1]) / 1e9).toFixed(2)}s!`); + console.log(`Total encrypted files: ${done}`); + console.log(`Failed: ${failed} (read-only or access denied files)`); + + process.exit(); } - console.log(`Finished encrypting files!`); - console.log(`Elapsed time: ${((diff[0] * 1e9 + diff[1]) / 1e9).toFixed(2)}s!`); - console.log(`Total encrypted files: ${done}`); - console.log(`Failed: ${failed} (read-only or access denied files)`); + if(hide){ + var metadata=warshield.encryptNames(file) + warshield.encryptFile(metadata, key, tmp).then(()=>{ + finish() + }); + }else{ + finish(); + } - process.exit(); }); } catch (e) { console.error(e.message); diff --git a/src/warshield.js b/src/warshield.js index 2a19063..eb9dd7c 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -7,6 +7,7 @@ const path = require('path'); const ENCRYPTION_ALGORITHM = 'aes-256-gcm'; const MIN_ROUNDS = 3000; const MAX_ROUNDS = 9000; +const METADATA_FILE = 'metadata.json' /** * @param {any[]} arr @@ -411,6 +412,54 @@ function getFiles(directory) { return em; } +/** + * Computes the sha256 of a name using a random salt each time. + * @param {string} name + * @returns {string} + */ +function shaFileName(name){ + var salt = crypto.randomBytes(64); + return crypto.createHash('sha256').update(name+salt).digest('hex'); +} + +function hideNames(filePath, obj={}){ + var file = path.basename(filePath); + var dir = path.dirname(filePath); + var shaName = shaFileName(file); + var newPath = path.join(dir, shaName) + obj[shaName] = file; + obj[file] = shaName; + fs.renameSync(filePath, newPath); + var stat = fs.statSync(newPath); + if(stat.isDirectory()){ + var files = fs.readdirSync(newPath); + for(var x of files){ + var newfilePath= path.join(newPath, x); + hideNames(newfilePath, obj); + } + } +} + +/**Sync function to encrypt names. It works just for directories. For a single file it will not work for consistency problems. + * @parm {string} filePath +*/ +function encryptNames(filePath){ + var metadata_filenames={}; + + hideNames(filePath, metadata_filenames); + var newPath = metadata_filenames[filePath]; + if(fs.statSync(newPath).isDirectory()){ + var metadataPath = path.join(newPath, METADATA_FILE); + fs.writeFileSync(metadataPath, JSON.stringify(metadata_filenames, null, 2)); + }else{ + var metadataPath = METADATA_FILE; + fs.writeFileSync(METADATA_FILE, JSON.stringify(metadata_filenames, null, 2)); + } + + return metadataPath; +} + + module.exports = { generateKey, encryptStream, @@ -419,4 +468,5 @@ module.exports = { decryptFile, encryptRecursive, decryptRecursive, + encryptNames } \ No newline at end of file From 5fa306c8f42b79bbc791a67efb609f4f8682765c Mon Sep 17 00:00:00 2001 From: William Zeni Date: Fri, 4 Aug 2023 17:19:42 +0200 Subject: [PATCH 6/9] added decryption with names --- src/index.js | 47 ++++++++++++++++--------------- src/warshield.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/index.js b/src/index.js index d8fb855..84fff15 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const Spinner = require('./Spinner'); const os = require('os'); const path = require('path'); const mkdirp = require('mkdirp'); -const optsArg = require('mkdirp/lib/opts-arg'); +const fs = require('fs'); function clearline() { process.stdout.write('\u001b[1G\u001b[2K'); @@ -53,10 +53,7 @@ function display_verbose(message, filename, trace = false, error = "") { } async function encrypt(file, {verbose, trace, tmp, hide}) { - /*const verbose = options.verbose; - const trace = options.trace; - const tmp = options.tmp; - const hide = options.hide; */ + try { const key = await ask_password(true); @@ -196,14 +193,14 @@ async function decrypt(file, { verbose, trace, tmp }) { process.stdout.write(' Done!\n'); process.stdout.write('Starting decrypting files...\n'); } - + const start = process.hrtime(); - - const decryption = warshield.decryptRecursive(file, key, tmp); + const newfile = await warshield.decryptNames(file, key, tmp); + const decryption = warshield.decryptRecursive(newfile, key, tmp); decryption.on('crawl-found', filename => { if (verbose) { - display_verbose("Failed adding", filename); + display_verbose("Added file", filename); } else { spinner.query = `Crawling files... ${filename}`; } @@ -246,24 +243,28 @@ async function decrypt(file, { verbose, trace, tmp }) { }); decryption.on('end', () => { - const diff = process.hrtime(start); + function finish(){ + const diff = process.hrtime(start); - if (spinner) { - spinner.stop(); - } + if (spinner) { + spinner.stop(); + } - if (verbose) { - process.stdout.write('\r\n'); - } else { - clearline(); - } + if (verbose) { + process.stdout.write('\r\n'); + } else { + clearline(); + } - console.log(`Finished decrypting files!`); - console.log(`Elapsed time: ${((diff[0] * 1e9 + diff[1]) / 1e9).toFixed(2)}s!`); - console.log(`Total decrypted files: ${done}`); - console.log(`Failed: ${failed} (read-only, access denied or non-encrypted files)`); + console.log(`Finished decrypting files!`); + console.log(`Elapsed time: ${((diff[0] * 1e9 + diff[1]) / 1e9).toFixed(2)}s!`); + console.log(`Total decrypted files: ${done}`); + console.log(`Failed: ${failed} (read-only, access denied or non-encrypted files)`); - process.exit(); + process.exit(); + } + + finish(); }); } catch (e) { console.error(e.message); diff --git a/src/warshield.js b/src/warshield.js index eb9dd7c..e2c36f8 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -422,6 +422,12 @@ function shaFileName(name){ return crypto.createHash('sha256').update(name+salt).digest('hex'); } +/** + * Rename all the subdirectories of a given path with the name sha256 value, + * and fill the obj with the bijective corrispondence (sha -> name, name -> sha) + * @param {string} filePath + * @param {Object} obj + */ function hideNames(filePath, obj={}){ var file = path.basename(filePath); var dir = path.dirname(filePath); @@ -440,8 +446,10 @@ function hideNames(filePath, obj={}){ } } -/**Sync function to encrypt names. It works just for directories. For a single file it will not work for consistency problems. - * @parm {string} filePath +/** + * Sync function to encrypt names. + * @param {string} filePath + * @returns {string} metadataPath */ function encryptNames(filePath){ var metadata_filenames={}; @@ -459,6 +467,63 @@ function encryptNames(filePath){ return metadataPath; } +/** + * Rename all the subdirectories of a given path with previously stored data in the json metadata obj + * @param {string} filePath + * @param {Object} obj + */ +function showNames(filePath, obj={}){ + var file = path.basename(filePath); + var dir = path.dirname(filePath); + var realname = obj[file]; + var newPath = path.join(dir, realname) + + fs.renameSync(filePath, newPath); + var stat = fs.statSync(newPath); + if(stat.isDirectory()){ + var files = fs.readdirSync(newPath); + for(var x of files){ + var newfilePath= path.join(newPath, x); + showNames(newfilePath, obj); + } + } +} + +/** + * Function to decrypt files name. It returns the new file path (the one restored). + * If it doesn't find the metadata file it returns the file path passed. To restore the files name, + * it decrypts the found metadata file and deletes it. If somenthing wrong happens during the metadata file decryption + * it returns the original file path. + * @param {string} filePath + * @param {string} key + * @param {string} tmp + * @returns {string} + */ +async function decryptNames(filePath, key, tmp){ + var stat = fs.statSync(filePath); + var metadata = METADATA_FILE; + + if(stat.isDirectory()){ + metadata = path.join(filePath, METADATA_FILE); + } + + if(fs.existsSync(metadata)){ + try{ + await decryptFile(metadata, key, tmp); + const infoObj = JSON.parse(fs.readFileSync(metadata)); + fs.unlinkSync(metadata); + showNames(filePath, infoObj) + return infoObj[filePath]; + }catch(err){ + return filePath; + } + + }else{ + return filePath + } + +} + module.exports = { generateKey, @@ -468,5 +533,7 @@ module.exports = { decryptFile, encryptRecursive, decryptRecursive, - encryptNames + encryptNames, + decryptNames, + } \ No newline at end of file From bf9f42e43de3ccf775cf60abe13b65c1ae8959f5 Mon Sep 17 00:00:00 2001 From: William Zeni Date: Sat, 12 Aug 2023 20:05:49 +0200 Subject: [PATCH 7/9] implemented cli usage --- cli.js | 1 + src/warshield.js | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index 94ff17c..8edb99c 100755 --- a/cli.js +++ b/cli.js @@ -13,6 +13,7 @@ program .option('-v, --verbose', 'enable verbosity') .option('-t, --trace', 'enable stacktrace') .option('-p, --tmp ', 'change temporary directory') + .option('-h, --hide', 'enable filenames encryption. A metadata file will be created in the same directory. Do not loose the metadata file if you want to be able to restore the names. The decription of names is automatic') .description('encrypt a file or all files in a directory') .action(encrypt); diff --git a/src/warshield.js b/src/warshield.js index e2c36f8..40d8e0e 100644 --- a/src/warshield.js +++ b/src/warshield.js @@ -453,9 +453,11 @@ function hideNames(filePath, obj={}){ */ function encryptNames(filePath){ var metadata_filenames={}; + var dirPath = path.dirname(filePath); + var oldName = path.basename(filePath) hideNames(filePath, metadata_filenames); - var newPath = metadata_filenames[filePath]; + var newPath = path.join(dirPath, metadata_filenames[oldName]); if(fs.statSync(newPath).isDirectory()){ var metadataPath = path.join(newPath, METADATA_FILE); fs.writeFileSync(metadataPath, JSON.stringify(metadata_filenames, null, 2)); @@ -501,7 +503,7 @@ function showNames(filePath, obj={}){ */ async function decryptNames(filePath, key, tmp){ var stat = fs.statSync(filePath); - var metadata = METADATA_FILE; + var metadata = path.join(path.dirname(filePath), METADATA_FILE); if(stat.isDirectory()){ metadata = path.join(filePath, METADATA_FILE); @@ -512,8 +514,9 @@ async function decryptNames(filePath, key, tmp){ await decryptFile(metadata, key, tmp); const infoObj = JSON.parse(fs.readFileSync(metadata)); fs.unlinkSync(metadata); - showNames(filePath, infoObj) - return infoObj[filePath]; + showNames(filePath, infoObj); + var newPath= path.join(path.dirname(filePath), infoObj[path.basename(filePath)]) + return newPath; }catch(err){ return filePath; } From 2aedc52cb300c0a476593fe24cf8b2179714e075 Mon Sep 17 00:00:00 2001 From: William Zeni Date: Wed, 13 Sep 2023 10:33:20 +0200 Subject: [PATCH 8/9] added completion percentage --- src/index.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 84fff15..b11d745 100644 --- a/src/index.js +++ b/src/index.js @@ -57,6 +57,8 @@ async function encrypt(file, {verbose, trace, tmp, hide}) { try { const key = await ask_password(true); + let added = 0; + let current = 0; let done = 0; let failed = 0; @@ -85,6 +87,7 @@ async function encrypt(file, {verbose, trace, tmp, hide}) { const encryption = warshield.encryptRecursive(file, key, tmp); encryption.on('crawl-found', filename => { + added++; if (verbose) { display_verbose("Added file", filename); } else { @@ -110,21 +113,23 @@ async function encrypt(file, {verbose, trace, tmp, hide}) { encryption.on('done', filename => { done++; - + current++; if (verbose) { - display_verbose("\x1b[32mDone encrypting", filename); + display_verbose(`\x1b[32mDone encrypting [${current}/${added}]`, filename); } else { - spinner.query = `Encrypting files... ${filename}`; + let percent = parseInt((current/added)*100) + spinner.query = `Encrypting files ${percent}% : ${filename}`; } }); encryption.on('failed', (filename, err) => { failed++; - + current++; if (verbose) { - display_verbose("\x1b[31mFailed encrypting", filename, trace, err); + display_verbose(`\x1b[31mFailed encrypting [${current}/${added}]`, filename, trace, err); } else { - spinner.query = `Encrypting files... ${filename}`; + let percent = parseInt((current/added)*100) + spinner.query = `Encrypting files ${percent}% : ${filename}`; } }); @@ -172,6 +177,8 @@ async function decrypt(file, { verbose, trace, tmp }) { let done = 0; let failed = 0; + let added = 0; + let current = 0; if (!verbose) { var spinner = new Spinner("Starting decryption..."); @@ -199,6 +206,7 @@ async function decrypt(file, { verbose, trace, tmp }) { const decryption = warshield.decryptRecursive(newfile, key, tmp); decryption.on('crawl-found', filename => { + added++; if (verbose) { display_verbose("Added file", filename); } else { @@ -224,21 +232,23 @@ async function decrypt(file, { verbose, trace, tmp }) { decryption.on('done', filename => { done++; - + current++; if (verbose) { - display_verbose("\x1b[32mDone decrypting", filename); + display_verbose(`\x1b[32mDone decrypting [${current}/${added}]`, filename); } else { - spinner.query = `Decrypting files... ${filename}`; + let percent = parseInt((current/added)*100) + spinner.query = `Decrypting files ${percent}% : ${filename}`; } }); decryption.on('failed', (filename, err) => { failed++; - + current++; if (verbose) { - display_verbose("\x1b[31mFailed decrypting", filename, trace, err); + display_verbose(`\x1b[32mFailed decrypting [${current}/${added}]`, filename, trace, err); } else { - spinner.query = `Decrypting files... ${filename}`; + let percent = parseInt((current/added)*100) + spinner.query = `Decrypting files ${percent}% : ${filename}`; } }); From 9b4525e9b1e059df156f47161b91faa44268894c Mon Sep 17 00:00:00 2001 From: William Zeni Date: Wed, 13 Sep 2023 16:51:01 +0200 Subject: [PATCH 9/9] edit on cli --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 8edb99c..3c5bad2 100755 --- a/cli.js +++ b/cli.js @@ -13,7 +13,7 @@ program .option('-v, --verbose', 'enable verbosity') .option('-t, --trace', 'enable stacktrace') .option('-p, --tmp ', 'change temporary directory') - .option('-h, --hide', 'enable filenames encryption. A metadata file will be created in the same directory. Do not loose the metadata file if you want to be able to restore the names. The decription of names is automatic') + .option('-H, --hide', 'enable filenames encryption. A metadata file will be created in the same directory. Do not loose the metadata file if you want to be able to restore the names. The decription of names is automatic') .description('encrypt a file or all files in a directory') .action(encrypt);