blob: 1877fa0907f32555f029febb60efa40e45fa78e5 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 08:59:031var aes = require('./aes')
2var Buffer = require('safe-buffer').Buffer
3var Transform = require('cipher-base')
4var inherits = require('inherits')
5
6function StreamCipher (mode, key, iv, decrypt) {
7 Transform.call(this)
8
9 this._cipher = new aes.AES(key)
10 this._prev = Buffer.from(iv)
11 this._cache = Buffer.allocUnsafe(0)
12 this._secCache = Buffer.allocUnsafe(0)
13 this._decrypt = decrypt
14 this._mode = mode
15}
16
17inherits(StreamCipher, Transform)
18
19StreamCipher.prototype._update = function (chunk) {
20 return this._mode.encrypt(this, chunk, this._decrypt)
21}
22
23StreamCipher.prototype._final = function () {
24 this._cipher.scrub()
25}
26
27module.exports = StreamCipher