Skip to content

Instantly share code, notes, and snippets.

@siteslave
Forked from bbstilson/decrypt.js
Created September 30, 2020 07:54
Show Gist options
  • Save siteslave/196fa064c919b204bf1d3bd274221e3f to your computer and use it in GitHub Desktop.
Save siteslave/196fa064c919b204bf1d3bd274221e3f 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