UNPKG

888 BJavaScriptView Raw
1var Transform = require('stream').Transform;
2var inherits = require('inherits');
3
4module.exports = CipherBase;
5inherits(CipherBase, Transform);
6function CipherBase() {
7 Transform.call(this);
8}
9CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
10 if (typeof data === 'string') {
11 data = new Buffer(data, inputEnc);
12 }
13 var outData = this._update(data);
14 if (outputEnc) {
15 outData = outData.toString(outputEnc);
16 }
17 return outData;
18};
19CipherBase.prototype._transform = function (data, _, next) {
20 this.push(this._update(data));
21 next();
22};
23CipherBase.prototype._flush = function (next) {
24 var chunk = this._final();
25 if (chunk) {
26 this.push(chunk);
27 }
28 next();
29};
30CipherBase.prototype.final = function (outputEnc) {
31 var outData = this._final() || new Buffer('');
32 if (outputEnc) {
33 outData = outData.toString(outputEnc);
34 }
35 return outData;
36};
\No newline at end of file