only encrypt html files and copy other files as-is (closes #181, closes #188)

pull/189/head
robinmoisson 2023-10-07 08:42:23 +02:00
rodzic 04d50a0ddc
commit eb4e087e87
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 9419716500078583
5 zmienionych plików z 83 dodań i 30 usunięć

3
.gitignore vendored
Wyświetl plik

@ -5,4 +5,5 @@ node_modules
.env .env
encrypted/ encrypted/
!example/encrypted/ !example/encrypted/
decrypted/ decrypted/
test/

Wyświetl plik

@ -270,16 +270,41 @@ function genFile(data, outputFilePath, templateFilePath) {
} }
exports.genFile = genFile; exports.genFile = genFile;
/**
* @param {string} path
* @param {string} fullRootDirectory
* @param {string} outputDirectory
* @returns {string}
*/
function getFullOutputPath(path, fullRootDirectory, outputDirectory) {
const relativePath = pathModule.relative(fullRootDirectory, path);
return outputDirectory + "/" + relativePath;
}
exports.getFullOutputPath = getFullOutputPath;
/**
* @param {string} inputFilePath
* @param {string} outputFilePath
*/
function copyFile(inputFilePath, outputFilePath) {
// create output directory if it does not exist
createDirectoryStructureForFile(outputFilePath);
try {
fs.copyFileSync(inputFilePath, outputFilePath, fs.constants.COPYFILE_FICLONE);
} catch (e) {
console.error(e);
exitWithError(`could not write file at path "${filePath}"`);
}
}
/** /**
* @param {string} filePath * @param {string} filePath
* @param {string} contents * @param {string} contents
*/ */
function writeFile(filePath, contents) { function writeFile(filePath, contents) {
// create output directory if it does not exist // create output directory if it does not exist
const dirname = pathModule.dirname(filePath); createDirectoryStructureForFile(filePath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
try { try {
fs.writeFileSync(filePath, contents); fs.writeFileSync(filePath, contents);
@ -290,6 +315,16 @@ function writeFile(filePath, contents) {
} }
exports.writeFile = writeFile; exports.writeFile = writeFile;
/**
* @param {string} filePath
*/
function createDirectoryStructureForFile(filePath) {
const dirname = pathModule.dirname(filePath);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
}
/** /**
* @param {string} templatePathParameter * @param {string} templatePathParameter
* @returns {boolean} * @returns {boolean}
@ -302,10 +337,11 @@ exports.isCustomPasswordTemplateDefault = isCustomPasswordTemplateDefault;
/** /**
* @param {string} path * @param {string} path
* @param {string} outputDirectory
* @param {string} rootDirectory * @param {string} rootDirectory
* @param {(fullPath: string, rootDirectoryFromArgument: string) => void} callback * @param {(fullPath: string, rootDirectoryFromArgument: string) => void} callback
*/ */
function recursivelyApplyCallbackToFiles(callback, path, rootDirectory = "") { function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, rootDirectory = "") {
const fullPath = pathModule.resolve(path); const fullPath = pathModule.resolve(path);
const fullRootDirectory = rootDirectory || pathModule.dirname(fullPath); const fullRootDirectory = rootDirectory || pathModule.dirname(fullPath);
@ -313,14 +349,22 @@ function recursivelyApplyCallbackToFiles(callback, path, rootDirectory = "") {
fs.readdirSync(fullPath).forEach((filePath) => { fs.readdirSync(fullPath).forEach((filePath) => {
const fullFilePath = `${fullPath}/${filePath}`; const fullFilePath = `${fullPath}/${filePath}`;
recursivelyApplyCallbackToFiles(callback, fullFilePath, fullRootDirectory); recursivelyApplyCallbackToHtmlFiles(callback, fullFilePath, outputDirectory, fullRootDirectory);
}); });
return; return;
} }
callback(fullPath, fullRootDirectory); // apply the callback if it's an HTML file
if (fullPath.endsWith(".html")) {
callback(fullPath, fullRootDirectory);
}
// else just copy the file as is
else {
const fullOutputPath = getFullOutputPath(fullPath, fullRootDirectory, outputDirectory);
copyFile(fullPath, fullOutputPath);
}
} }
exports.recursivelyApplyCallbackToFiles = recursivelyApplyCallbackToFiles; exports.recursivelyApplyCallbackToHtmlFiles = recursivelyApplyCallbackToHtmlFiles;
function parseCommandLineArguments() { function parseCommandLineArguments() {
return ( return (

Wyświetl plik

@ -30,10 +30,11 @@ const {
getValidatedSalt, getValidatedSalt,
isOptionSetByUser, isOptionSetByUser,
parseCommandLineArguments, parseCommandLineArguments,
recursivelyApplyCallbackToFiles, recursivelyApplyCallbackToHtmlFiles,
validatePassword, validatePassword,
writeConfig, writeConfig,
writeFile, writeFile,
getFullOutputPath,
} = require("./helpers.js"); } = require("./helpers.js");
// parse arguments // parse arguments
@ -106,9 +107,13 @@ async function runStatiCrypt() {
const outputDirectory = isOutputDirectoryDefault ? "decrypted" : namedArgs.directory; const outputDirectory = isOutputDirectoryDefault ? "decrypted" : namedArgs.directory;
positionalArguments.forEach((path) => { positionalArguments.forEach((path) => {
recursivelyApplyCallbackToFiles((fullPath, fullRootDirectory) => { recursivelyApplyCallbackToHtmlFiles(
decodeAndGenerateFile(fullPath, fullRootDirectory, hashedPassword, outputDirectory); (fullPath, fullRootDirectory) => {
}, path); decodeAndGenerateFile(fullPath, fullRootDirectory, hashedPassword, outputDirectory);
},
path,
namedArgs.directory
);
}); });
return; return;
@ -139,21 +144,25 @@ async function runStatiCrypt() {
// encode all the files // encode all the files
positionalArguments.forEach((path) => { positionalArguments.forEach((path) => {
recursivelyApplyCallbackToFiles((fullPath, fullRootDirectory) => { recursivelyApplyCallbackToHtmlFiles(
encodeAndGenerateFile( (fullPath, fullRootDirectory) => {
fullPath, encodeAndGenerateFile(
fullRootDirectory, fullPath,
hashedPassword, fullRootDirectory,
salt, hashedPassword,
baseTemplateData, salt,
isRememberEnabled, baseTemplateData,
namedArgs isRememberEnabled,
); namedArgs
}, path); );
},
path,
namedArgs.directory
);
}); });
} }
async function decodeAndGenerateFile(path, rootDirectoryFromArguments, hashedPassword, outputDirectory) { async function decodeAndGenerateFile(path, fullRootDirectory, hashedPassword, outputDirectory) {
// get the file content // get the file content
const encryptedFileContent = getFileContent(path); const encryptedFileContent = getFileContent(path);
@ -172,8 +181,7 @@ async function decodeAndGenerateFile(path, rootDirectoryFromArguments, hashedPas
return console.log(`ERROR: could not decrypt ${path}`); return console.log(`ERROR: could not decrypt ${path}`);
} }
const relativePath = pathModule.relative(rootDirectoryFromArguments, path); const outputFilepath = getFullOutputPath(path, fullRootDirectory, outputDirectory);
const outputFilepath = outputDirectory + "/" + relativePath;
writeFile(outputFilepath, decoded); writeFile(outputFilepath, decoded);
} }

Wyświetl plik

@ -768,7 +768,7 @@ exports.init = init;
})()); })());
const templateError = "Bad password!", const templateError = "Bad password!",
isRememberEnabled = true, isRememberEnabled = true,
staticryptConfig = {"staticryptEncryptedMsgUniqueVariableName":"2271533ff6ba1c6223131c45fe772d77252bb175b03931aaabb9a4be7450f21edd6e55ab868304a493ffb7b5cb4d42ea21c0572063343d472699afd770cb115b6934a767bb7167c6c2737ca06671349e8ebf30bdd24f21f864c522197de1f223399a46c83694428ec32733f9c908697f240c597bea92679e607fe7c1021b2b5795eaf87dc65d981585d6b280ed953a3d744614230c709e09be866f6dfbecc169a31425efed49fc9dd6372b6a3148d9a9612dd3e9a44be08199c89a948a8609b7","isRememberEnabled":true,"rememberDurationInDays":0,"staticryptSaltUniqueVariableName":"b93bbaf35459951c47721d1f3eaeb5b9"}; staticryptConfig = {"staticryptEncryptedMsgUniqueVariableName":"da67de28fb1fec9f04aaf615aa0e80a9675a43473e21b51a1dee72cb6153728dde06e9c242c6de08ab4608d18e73c9f922466a4695fe60e8f684f7d5d7b54149d7749bc2e626e39844d6757289f17696ea7992d1c87b49f76903c57d6506bb5aee0f0cdf4183bb20f71ad0f719c5a3ee338a486c70a1d433c00fd4384c2ec65471c436dc4cf0c4ba13a2189db1ee335f113652439c1feb2c02bf237ddada4ae46bd404f3f8417cc6b27d2e7960024afe2323edb19e8f38c699b9f4ce0db36d37","isRememberEnabled":true,"rememberDurationInDays":0,"staticryptSaltUniqueVariableName":"b93bbaf35459951c47721d1f3eaeb5b9"};
// you can edit these values to customize some of the behavior of StatiCrypt // you can edit these values to customize some of the behavior of StatiCrypt
const templateConfig = { const templateConfig = {

Wyświetl plik

@ -1,7 +1,7 @@
{ {
"name": "staticrypt", "name": "staticrypt",
"version": "3.3.1", "version": "3.3.2",
"description": "Based on the [crypto-js](https://github.com/brix/crypto-js) library, StatiCrypt uses AES-256 to encrypt your input with your long password and put it in a HTML file with a password prompt that can decrypted in-browser (client side).", "description": "Baed on the [crypto-js](https://github.com/brix/crypto-js) library, StatiCrypt uses AES-256 to encrypt your input with your long password and put it in a HTML file with a password prompt that can decrypted in-browser (client side).",
"main": "index.js", "main": "index.js",
"files": [ "files": [
"/cli", "/cli",