Last active
October 19, 2022 05:44
-
-
Save adrianseeley/5f54f985bc5c59ea36a270117f7c329b to your computer and use it in GitHub Desktop.
File Crypto Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const args = process.argv.slice(2); | |
const crypto = require('crypto'); | |
const algorithm = 'aes-256-ctr'; | |
const password = 'seelflix'; | |
const fs = require('fs'); | |
const path = require('path'); | |
function read_dir (dir) { | |
return fs.statSync(dir).isDirectory() ? Array.prototype.concat(...fs.readdirSync(dir).map(f => read_dir(path.join(dir, f)))) : dir; | |
}; | |
const process_file = (infile, outfile, cb) => { | |
const read = fs.createReadStream(infile); | |
const encrypt = crypto.createCipher(algorithm, password); | |
const write = fs.createWriteStream(outfile + ".enc"); | |
const stream = read.pipe(encrypt).pipe(write); | |
stream.on("error", (err) => { | |
throw err; | |
}); | |
stream.on("finish", cb); | |
}; | |
const unprocess_file = (infile, outfile, cb) => { | |
const read = fs.createReadStream(infile); | |
const decrypt = crypto.createDecipher(algorithm, password); | |
const write = fs.createWriteStream(outfile.slice(0, outfile.length - 4)); | |
const stream = read.pipe(decrypt).pipe(write); | |
stream.on("error", (err) => { | |
throw err; | |
}); | |
stream.on("finish", cb); | |
}; | |
const process_dir = (indir, outdir, cb, _files) => { | |
if (!_files) { | |
_files = read_dir(indir); | |
} | |
const infile = _files.splice(0, 1)[0]; | |
console.log(infile); | |
const filename = path.basename(infile); | |
process_file(infile, path.join(outdir, filename), () => { | |
if (_files.length === 0) { | |
return cb(); | |
} else { | |
return process_dir(indir, outdir, cb, _files); | |
} | |
}); | |
}; | |
const unprocess_dir = (indir, outdir, cb, _files) => { | |
if (!_files) { | |
_files = read_dir(indir); | |
} | |
const infile = _files.splice(0, 1)[0]; | |
console.log(infile); | |
const filename = path.basename(infile); | |
unprocess_file(infile, path.join(outdir, filename), () => { | |
if (_files.length === 0) { | |
return cb(); | |
} else { | |
return unprocess_dir(indir, outdir, cb, _files); | |
} | |
}); | |
}; | |
const func = args[0]; | |
if (func === "e") { | |
console.log("encrypting") | |
const indir = args[1]; | |
const outdir = args[2]; | |
console.log("indir: " + indir); | |
console.log("outdir: " + outdir); | |
fs.mkdirSync(outdir, {recursive: true}); | |
process_dir(indir, outdir, () => { | |
console.log("done"); | |
}); | |
} else if (func === "d") { | |
console.log("decrypting") | |
const indir = args[1]; | |
const outdir = args[2]; | |
console.log("indir: " + indir); | |
console.log("outdir: " + outdir); | |
fs.mkdirSync(outdir, {recursive: true}); | |
unprocess_dir(indir, outdir, () => { | |
console.log("done"); | |
}); | |
} else { | |
console.log("use either e or d"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment