From 94a84c5837fcd9a7481772ccff9800698a40e990 Mon Sep 17 00:00:00 2001 From: AaronCoplan Date: Thu, 14 Dec 2017 17:38:34 -0500 Subject: [PATCH] Fully working encryption via command line. --- cli/index.js | 85 ++++++++++++++++++- cli/password_template.html | 167 +++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 cli/password_template.html diff --git a/cli/index.js b/cli/index.js index 821b22b..a865ed5 100644 --- a/cli/index.js +++ b/cli/index.js @@ -1,21 +1,102 @@ +'use strict'; + var CryptoJS = require("crypto-js"); var FileSystem = require("fs"); +var https = require("https"); + +const SCRIPT_URL = 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js'; +const SCRIPT_TAG = ''; const argv = process.argv; const argc = process.argv.length; -if(argc != 4){ +if(argc < 4 || argc > 7){ console.log("Failure: invalid argument length!"); process.exit(1); } const htmlFilepath = argv[2]; +const outputFilePath = htmlFilepath + ".encrypted"; + const password = argv[3]; +var pageTitle = "Protected Page"; +if(argc >= 5){ + pageTitle = argv[4]; +} + +var instructions = ""; +if(argc >= 6){ + instructions = argv[5]; +} + +var embed = true; +if(argc >= 7){ + embed = (argv[6] == 'true'); +} + try{ var contents = FileSystem.readFileSync(htmlFilepath, 'utf8'); }catch(e){ console.log("Failure: file does not exist!"); process.exit(1); } -console.log(contents); + +var encrypted = CryptoJS.AES.encrypt(contents, password); +var hmac = CryptoJS.HmacSHA256(encrypted.toString(), CryptoJS.SHA256(password)).toString(); +var encryptedMessage = hmac + encrypted; + +var data = { + title: pageTitle, + instructions: instructions, + encrypted: encryptedMessage, + crypto_tag: SCRIPT_TAG, + embed: embed, + outputFilePath: outputFilePath +}; + +if(data.embed){ + https.get(SCRIPT_URL, (resp) => { + let txt = ''; + + resp.on('data', (chunk) => { + txt += chunk; + }); + + resp.on('end', () => { + data["crypto_tag"] = ''; + console.log(data); + genFile(data); + }); + + }).on('error', (err) => { + console.log("Failure: could not fetch embedded script"); + process.exit(1); + }); +}else{ + genFile(data); +} + +function genFile(data){ + try{ + var templateContents = FileSystem.readFileSync('./password_template.html', 'utf8'); + }catch(e){ + console.log("Failure: could not read template!"); + process.exit(1); + } + + var renderedTemplate = render(templateContents, data); + + try{ + FileSystem.writeFileSync(data.outputFilePath, renderedTemplate); + }catch(e){ + console.log("Failure: could not generate output file!"); + process.exit(1); + } +} + +function render(tpl, data){ + return tpl.replace(/{(.*?)}/g, function (_, key) { + return data && data[key] || ''; + }); +} diff --git a/cli/password_template.html b/cli/password_template.html new file mode 100644 index 0000000..0aab422 --- /dev/null +++ b/cli/password_template.html @@ -0,0 +1,167 @@ + + + + + {title} + + + + + + + + + + + + + +
+
+
+

{title}

+

{instructions}

+
+ +
+ +
+ + + +
+
+ +
+ + + +{crypto_tag} + + + +