const { Transform } = require('stream');
const crypto = require('crypto');
class EncryptTransformStream extends Transform {
constructor(algorithm, password) {
super();
this.algorithm = algorithm;
this.password = password;
this.cipher = crypto.createCipher(algorithm, password);
}
_transform(chunk, encoding, callback) {
this.push(this.cipher.update(chunk, encoding));
callback();
}
_flush(callback) {
this.push(this.cipher.final());
callback();
}
}
const encryptStream = new EncryptTransformStream('aes-256-cbc', 'myPassword');
encryptStream.on('data', (data) => {
console.log(data.toString('hex'));
});