2017-08-14 05:01:11 +02:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2017-05-03 15:35:00 +02:00
|
|
|
var Transform = require('stream').Transform
|
|
|
|
var StringDecoder = require('string_decoder').StringDecoder
|
2017-08-14 05:01:11 +02:00
|
|
|
var inherits = require('inherits')
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
function CipherBase (hashMode) {
|
|
|
|
Transform.call(this)
|
|
|
|
this.hashMode = typeof hashMode === 'string'
|
|
|
|
if (this.hashMode) {
|
|
|
|
this[hashMode] = this._finalOrDigest
|
|
|
|
} else {
|
|
|
|
this.final = this._finalOrDigest
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (this._final) {
|
|
|
|
this.__final = this._final
|
|
|
|
this._final = null
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
this._decoder = null
|
|
|
|
this._encoding = null
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
inherits(CipherBase, Transform)
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
|
|
|
|
if (typeof data === 'string') {
|
2017-08-14 05:01:11 +02:00
|
|
|
data = Buffer.from(data, inputEnc)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
var outData = this._update(data)
|
2017-08-14 05:01:11 +02:00
|
|
|
if (this.hashMode) return this
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
if (outputEnc) {
|
|
|
|
outData = this._toString(outData, outputEnc)
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
return outData
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherBase.prototype.setAutoPadding = function () {}
|
|
|
|
CipherBase.prototype.getAuthTag = function () {
|
|
|
|
throw new Error('trying to get auth tag in unsupported state')
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherBase.prototype.setAuthTag = function () {
|
|
|
|
throw new Error('trying to set auth tag in unsupported state')
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherBase.prototype.setAAD = function () {
|
|
|
|
throw new Error('trying to set aad in unsupported state')
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherBase.prototype._transform = function (data, _, next) {
|
|
|
|
var err
|
|
|
|
try {
|
|
|
|
if (this.hashMode) {
|
|
|
|
this._update(data)
|
|
|
|
} else {
|
|
|
|
this.push(this._update(data))
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
err = e
|
|
|
|
} finally {
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CipherBase.prototype._flush = function (done) {
|
|
|
|
var err
|
|
|
|
try {
|
2017-08-14 05:01:11 +02:00
|
|
|
this.push(this.__final())
|
2017-05-03 15:35:00 +02:00
|
|
|
} catch (e) {
|
|
|
|
err = e
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
done(err)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
CipherBase.prototype._finalOrDigest = function (outputEnc) {
|
2017-08-14 05:01:11 +02:00
|
|
|
var outData = this.__final() || Buffer.alloc(0)
|
2017-05-03 15:35:00 +02:00
|
|
|
if (outputEnc) {
|
|
|
|
outData = this._toString(outData, outputEnc, true)
|
|
|
|
}
|
|
|
|
return outData
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherBase.prototype._toString = function (value, enc, fin) {
|
|
|
|
if (!this._decoder) {
|
|
|
|
this._decoder = new StringDecoder(enc)
|
|
|
|
this._encoding = enc
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
if (this._encoding !== enc) throw new Error('can\'t switch encodings')
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
var out = this._decoder.write(value)
|
|
|
|
if (fin) {
|
|
|
|
out += this._decoder.end()
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
return out
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
module.exports = CipherBase
|