UNPKG

895 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 try {
25 this.push(this._final());
26 } catch(e) {
27 return next(e);
28 }
29 next();
30};
31CipherBase.prototype.final = function (outputEnc) {
32 var outData = this._final() || new Buffer('');
33 if (outputEnc) {
34 outData = outData.toString(outputEnc);
35 }
36 return outData;
37};
\No newline at end of file