streamCipher.js 713 B

12345678910111213141516171819202122232425
  1. var aes = require('./aes')
  2. var Transform = require('cipher-base')
  3. var inherits = require('inherits')
  4. inherits(StreamCipher, Transform)
  5. module.exports = StreamCipher
  6. function StreamCipher (mode, key, iv, decrypt) {
  7. if (!(this instanceof StreamCipher)) {
  8. return new StreamCipher(mode, key, iv)
  9. }
  10. Transform.call(this)
  11. this._cipher = new aes.AES(key)
  12. this._prev = new Buffer(iv.length)
  13. this._cache = new Buffer('')
  14. this._secCache = new Buffer('')
  15. this._decrypt = decrypt
  16. iv.copy(this._prev)
  17. this._mode = mode
  18. }
  19. StreamCipher.prototype._update = function (chunk) {
  20. return this._mode.encrypt(this, chunk, this._decrypt)
  21. }
  22. StreamCipher.prototype._final = function () {
  23. this._cipher.scrub()
  24. }