-
-
Save abhishekY495/48aae09c68881306e55e5b965fa07278 to your computer and use it in GitHub Desktop.
File Encryption
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 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