2017-12-15 21:37:02 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-11-05 10:58:21 +00:00
|
|
|
"use strict";
|
2017-12-14 22:38:34 +00:00
|
|
|
|
2022-04-23 10:00:18 +00:00
|
|
|
const fs = require("fs");
|
2022-11-21 21:46:45 +00:00
|
|
|
|
|
|
|
// parse .env file into process.env
|
|
|
|
require('dotenv').config();
|
|
|
|
|
2023-03-29 14:12:44 +00:00
|
|
|
const cryptoEngine = require("../lib/cryptoEngine.js");
|
2023-03-29 08:50:31 +00:00
|
|
|
const codec = require("../lib/codec.js");
|
2023-03-30 13:55:08 +00:00
|
|
|
const { generateRandomSalt } = cryptoEngine;
|
2023-03-29 14:12:44 +00:00
|
|
|
const { encode } = codec.init(cryptoEngine);
|
2023-03-30 13:55:08 +00:00
|
|
|
const { parseCommandLineArguments, buildStaticryptJS, isOptionSetByUser, genFile, getPassword, getFileContent, getSalt,
|
|
|
|
getValidatedSalt,
|
|
|
|
getValidatedPassword, getConfig
|
|
|
|
} = require("./helpers.js");
|
2017-12-14 22:00:11 +00:00
|
|
|
|
2023-03-01 17:46:48 +00:00
|
|
|
// parse arguments
|
|
|
|
const yargs = parseCommandLineArguments();
|
2022-02-27 18:37:17 +00:00
|
|
|
const namedArgs = yargs.argv;
|
2022-02-10 08:22:32 +00:00
|
|
|
|
2023-03-29 08:50:31 +00:00
|
|
|
async function runStatiCrypt() {
|
2023-03-30 16:43:40 +00:00
|
|
|
const hasSaltFlag = isOptionSetByUser("s", yargs);
|
|
|
|
const hasShareFlag = isOptionSetByUser("share", yargs);
|
|
|
|
|
2023-03-30 16:45:47 +00:00
|
|
|
const positionalArguments = namedArgs._;
|
|
|
|
|
2023-03-30 16:43:40 +00:00
|
|
|
// validate the number of arguments
|
|
|
|
if (!hasShareFlag && !hasSaltFlag) {
|
|
|
|
if (positionalArguments.length === 0) {
|
|
|
|
yargs.showHelp();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 08:50:31 +00:00
|
|
|
// if the 's' flag is passed without parameter, generate a salt, display & exit
|
2023-03-30 16:43:40 +00:00
|
|
|
if (hasSaltFlag && !namedArgs.salt) {
|
2023-03-29 08:50:31 +00:00
|
|
|
console.log(generateRandomSalt());
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2023-03-30 13:55:08 +00:00
|
|
|
// get config file
|
|
|
|
const config = getConfig(namedArgs.config);
|
|
|
|
|
|
|
|
// get the salt & password
|
|
|
|
const salt = getValidatedSalt(namedArgs, config);
|
|
|
|
const password = await getValidatedPassword(namedArgs.password, namedArgs.short);
|
|
|
|
|
|
|
|
// display the share link with the hashed password if the --share flag is set
|
2023-03-30 16:43:40 +00:00
|
|
|
if (hasShareFlag) {
|
2023-03-30 13:55:08 +00:00
|
|
|
const url = namedArgs.share || "";
|
|
|
|
|
|
|
|
const hashedPassword = await cryptoEngine.hashPassphrase(password, salt);
|
|
|
|
|
|
|
|
console.log(url + "#staticrypt_pwd=" + hashedPassword);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write salt to config file
|
2023-03-29 08:50:31 +00:00
|
|
|
const isUsingconfigFile = namedArgs.config.toLowerCase() !== "false";
|
|
|
|
const configPath = "./" + namedArgs.config;
|
|
|
|
if (isUsingconfigFile && config.salt !== salt) {
|
|
|
|
config.salt = salt;
|
|
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the file content
|
2023-03-30 13:55:08 +00:00
|
|
|
const inputFilepath = positionalArguments[0].toString();
|
2023-03-29 08:50:31 +00:00
|
|
|
const contents = getFileContent(inputFilepath);
|
|
|
|
|
|
|
|
// encrypt input
|
2023-03-30 10:03:18 +00:00
|
|
|
const encryptedMsg = await encode(contents, password, salt);
|
|
|
|
|
|
|
|
const isRememberEnabled = namedArgs.remember !== "false";
|
2023-03-29 14:12:44 +00:00
|
|
|
|
|
|
|
const data = {
|
2023-03-30 10:03:18 +00:00
|
|
|
is_remember_enabled: JSON.stringify(isRememberEnabled),
|
|
|
|
js_staticrypt: buildStaticryptJS(),
|
|
|
|
staticrypt_config: {
|
|
|
|
encryptedMsg,
|
|
|
|
isRememberEnabled,
|
|
|
|
rememberDurationInDays: namedArgs.remember,
|
|
|
|
salt,
|
|
|
|
},
|
|
|
|
template_button: namedArgs.templateButton,
|
|
|
|
template_error: namedArgs.templateError,
|
|
|
|
template_instructions: namedArgs.templateInstructions,
|
|
|
|
template_placeholder: namedArgs.templatePlaceholder,
|
|
|
|
template_remember: namedArgs.templateRemember,
|
|
|
|
template_title: namedArgs.templateTitle,
|
2023-03-29 14:12:44 +00:00
|
|
|
};
|
|
|
|
|
2023-03-30 13:55:08 +00:00
|
|
|
const outputFilepath = namedArgs.directory.replace(/\/+$/, '') + "/" + inputFilepath;
|
2023-03-29 14:12:44 +00:00
|
|
|
|
2023-03-29 15:15:06 +00:00
|
|
|
genFile(data, outputFilepath, namedArgs.template);
|
2017-12-14 22:38:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 08:50:31 +00:00
|
|
|
runStatiCrypt();
|