Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Forked from chris-rock/crypto-stream.js
Created April 24, 2022 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/072f5af7a9dab597b3711a80f330eca9 to your computer and use it in GitHub Desktop.
Save BananaAcid/072f5af7a9dab597b3711a80f330eca9 to your computer and use it in GitHub Desktop.
Encrypt and decrypt streams
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption of buffers
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
var fs = require('fs');
var zlib = require('zlib');
// input file
var r = fs.createReadStream('file.txt');
// zip content
var zip = zlib.createGzip();
// encrypt content
var encrypt = crypto.createCipher(algorithm, password);
// decrypt content
var decrypt = crypto.createDecipher(algorithm, password)
// unzip content
var unzip = zlib.createGunzip();
// write file
var w = fs.createWriteStream('file.out.txt');
// start pipe
r.pipe(zip).pipe(encrypt).pipe(decrypt).pipe(unzip).pipe(w);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment