-
-
Save siteslave/196fa064c919b204bf1d3bd274221e3f to your computer and use it in GitHub Desktop.
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 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