Skip to content

Instantly share code, notes, and snippets.

@fronterior
Created June 16, 2022 02:33
Show Gist options
  • Save fronterior/0be8d7eb07a7e95ae903cb82594fe297 to your computer and use it in GitHub Desktop.
Save fronterior/0be8d7eb07a7e95ae903cb82594fe297 to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const getCipherKey = require('./getCipherKey');
function decrypt({ file, password }) {
// First, get the initialization vector from the file.
const readInitVect = fs.createReadStream(file, { end: 15 });
let initVect;
readInitVect.on('data', (chunk) => {
initVect = chunk;
});
// Once we’ve got the initialization vector, we can decrypt the file.
readInitVect.on('close', () => {
const cipherKey = getCipherKey(password);
const readStream = fs.createReadStream(file, { start: 16 });
const decipher = crypto.createDecipheriv('aes256', cipherKey, initVect);
const unzip = zlib.createUnzip();
const writeStream = fs.createWriteStream(file + '.unenc');
readStream
.pipe(decipher)
.pipe(unzip)
.pipe(writeStream);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment