Created
June 16, 2022 02:33
-
-
Save fronterior/0db51080fda8c694027e78ebc08f4a83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 crypto = require('crypto'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const zlib = require('zlib'); | |
const AppendInitVect = require('./appendInitVect'); | |
const getCipherKey = require('./getCipherKey'); | |
function encrypt({ file, password }) { | |
// Generate a secure, pseudo random initialization vector. | |
const initVect = crypto.randomBytes(16); | |
// Generate a cipher key from the password. | |
const CIPHER_KEY = getCipherKey(password); | |
const readStream = fs.createReadStream(file); | |
const gzip = zlib.createGzip(); | |
const cipher = crypto.createCipheriv('aes256', CIPHER_KEY, initVect); | |
const appendInitVect = new AppendInitVect(initVect); | |
// Create a write stream with a different file extension. | |
const writeStream = fs.createWriteStream(path.join(file + ".enc")); | |
readStream | |
.pipe(gzip) | |
.pipe(cipher) | |
.pipe(appendInitVect) | |
.pipe(writeStream); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment