From 2ac86d0bc84006761189be3bdfbc326058d4fc29 Mon Sep 17 00:00:00 2001 From: Ron Gross Date: Sun, 19 Feb 2017 16:42:36 +0200 Subject: [PATCH] Add support for passing password as a command line argument --- index.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index d3ef34c..5caad53 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,28 @@ var fs = require("fs"); var prompt = require("prompt"); var path = require("path"); +function runCrypto(fn, from, to, password, finished) { + from = fs.createReadStream(from); + to = fs.createWriteStream(to); + fn = fn("cast5-cbc", password); + + from.pipe(fn).pipe(to); + from.on("end", function () { + console.log("done"); + }); +} + module.exports = function(fn) { var from = path.join(process.cwd(), process.argv[2]); var to = path.join(process.cwd(), process.argv[3]); + var password = process.argv[4]; + + if (password) { + runCrypto(fn, from, to, password); + return; + } + + // Password isn't given in the command line, read it from prompt prompt.start(); @@ -22,15 +41,9 @@ module.exports = function(fn) { if (err) { console.log(err); } else { - from = fs.createReadStream(from); - to = fs.createWriteStream(to); - fn = fn("cast5-cbc", result.password); + prompt.stop(); - from.pipe(fn).pipe(to); - from.on("end", function () { - console.log("done"); - prompt.stop(); - }); + runCrypto(fn, from, to, result.password); } }); };