2017-12-15 21:37:02 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-12-14 22:38:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-14 22:00:11 +00:00
|
|
|
var CryptoJS = require("crypto-js");
|
|
|
|
var FileSystem = require("fs");
|
2017-12-27 03:35:24 +00:00
|
|
|
const Yargs = require('yargs');
|
2017-12-14 22:38:34 +00:00
|
|
|
|
|
|
|
const SCRIPT_URL = 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js';
|
|
|
|
const SCRIPT_TAG = '<script src="' + SCRIPT_URL + '" integrity="sha384-lp4k1VRKPU9eBnPePjnJ9M2RF3i7PC30gXs70+elCVfgwLwx1tv5+ctxdtwxqZa7" crossorigin="anonymous"></script>';
|
2017-12-14 22:00:11 +00:00
|
|
|
|
2017-12-27 03:35:24 +00:00
|
|
|
const namedArgs = Yargs
|
|
|
|
.usage('staticrypt <input> <password> options')
|
|
|
|
.demandCommand(2)
|
|
|
|
.default({'output': null, 'title': null, 'instructions': null, 'embed': null})
|
|
|
|
.alias('o', 'output')
|
|
|
|
.alias('t', 'title')
|
|
|
|
.alias('i', 'instructions')
|
|
|
|
.alias('e', 'embed')
|
|
|
|
.argv;
|
|
|
|
|
|
|
|
if(namedArgs._.length != 2){
|
|
|
|
Yargs.showHelp();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const input = namedArgs._[0];
|
|
|
|
const password = namedArgs._[1];
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2017-12-14 22:00:11 +00:00
|
|
|
try{
|
2017-12-27 03:35:24 +00:00
|
|
|
var contents = FileSystem.readFileSync(input, 'utf8');
|
2017-12-14 22:00:11 +00:00
|
|
|
}catch(e){
|
2017-12-15 21:26:20 +00:00
|
|
|
console.log("Failure: input file does not exist!");
|
2017-12-14 22:00:11 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2017-12-27 03:35:24 +00:00
|
|
|
var encrypted = CryptoJS.AES.encrypt(contents, password);
|
|
|
|
var hmac = CryptoJS.HmacSHA256(encrypted.toString(), CryptoJS.SHA256(password).toString()).toString();
|
2017-12-14 22:38:34 +00:00
|
|
|
var encryptedMessage = hmac + encrypted;
|
|
|
|
|
|
|
|
var data = {
|
2017-12-14 23:07:05 +00:00
|
|
|
title: namedArgs.title != null ? namedArgs.title : "Protected Page",
|
|
|
|
instructions: namedArgs.instructions != null ? namedArgs.instructions : "",
|
2017-12-14 22:38:34 +00:00
|
|
|
encrypted: encryptedMessage,
|
|
|
|
crypto_tag: SCRIPT_TAG,
|
2017-12-14 23:07:05 +00:00
|
|
|
embed: namedArgs.embed != null ? namedArgs.embed : false,
|
2017-12-27 03:35:24 +00:00
|
|
|
outputFilePath: namedArgs.output != null ? namedArgs.output : input.replace(/\.html$/, '') + "_encrypted.html"
|
2017-12-14 22:38:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(data.embed){
|
2017-12-19 01:32:27 +00:00
|
|
|
try{
|
|
|
|
var embedContents = FileSystem.readFileSync('crypto-js.min.js', 'utf8');
|
2017-12-21 03:33:59 +00:00
|
|
|
data["crypto_tag"] = '<script>' + embedContents + '</script>';
|
2017-12-19 01:32:27 +00:00
|
|
|
genFile(data);
|
|
|
|
}catch(e){
|
|
|
|
console.log("Failure: embed file does not exist!");
|
2017-12-14 22:38:34 +00:00
|
|
|
process.exit(1);
|
2017-12-19 01:32:27 +00:00
|
|
|
}
|
2017-12-14 22:38:34 +00:00
|
|
|
}else{
|
|
|
|
genFile(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function genFile(data){
|
|
|
|
try{
|
2017-12-27 03:35:24 +00:00
|
|
|
var templateContents = FileSystem.readFileSync(__dirname + '/password_template.html', 'utf8');
|
2017-12-14 22:38:34 +00:00
|
|
|
}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] || '';
|
|
|
|
});
|
|
|
|
}
|