-
Notifications
You must be signed in to change notification settings - Fork 7
/
cipherbase.js
68 lines (66 loc) · 1.49 KB
/
cipherbase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var Transform = require('readable-stream').Transform;
var inherits = require('inherits');
module.exports = CipherBase;
inherits(CipherBase, Transform);
function CipherBase(digest) {
if (digest) {
this.digest = finalFunc;
} else {
this.final = finalFunc;
}
}
[
'_readableState',
'_writableState',
'_transformState'
].forEach(function(prop) {
Object.defineProperty(CipherBase.prototype, prop, {
get: function() {
Transform.call(this);
return this[prop];
},
set: function(val) {
Object.defineProperty(this, prop, {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: true
});
});
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
if (typeof data === 'string') {
data = new Buffer(data, inputEnc);
}
var outData = this._update(data) || new Buffer('');
if (outputEnc) {
outData = outData.toString(outputEnc);
}
if (this.digest) {
return this;
}
return outData;
};
CipherBase.prototype._transform = function (data, _, next) {
this.push(this._update(data));
next();
};
CipherBase.prototype._flush = function (next) {
try {
this.push(this._final());
} catch(e) {
return next(e);
}
next();
};
function finalFunc (outputEnc) {
var outData = this._final() || new Buffer('');
if (outputEnc) {
outData = outData.toString(outputEnc);
}
return outData;
};
CipherBase.prototype._final = function () {};