Skip to content

Instantly share code, notes, and snippets.

@abhishekY495
Created November 11, 2022 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishekY495/48aae09c68881306e55e5b965fa07278 to your computer and use it in GitHub Desktop.
Save abhishekY495/48aae09c68881306e55e5b965fa07278 to your computer and use it in GitHub Desktop.
File Encryption
const path = require("path");
const fs = require("fs");
const { pipeline } = require("stream/promises");
const { Transform } = require("stream");
const { app } = require("./encryption.js");
const encHighWaterMark = 1024 * 1024 * 100;
async function encryptFile(fileLocation, password) {
const fileReadStream = fs.createReadStream(fileLocation, {
highWaterMark: encHighWaterMark,
});
const filePath = path.parse(fileLocation).dir;
const fileName = path.parse(fileLocation).name;
const fileExt = path.parse(fileLocation).ext;
const newEncFile = filePath + "\\" + fileName + "__ENC" + fileExt;
const fileWriteStream = fs.createWriteStream(newEncFile);
await pipeline(
fileReadStream,
new Transform({
transform(chunk, encoding, callback) {
const encryptedData = app.encrypt(chunk, password);
callback(null, encryptedData);
},
}),
fileWriteStream
);
}
module.exports.encryptFile = encryptFile;
module.exports.encHighWaterMark = encHighWaterMark;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment