ede.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var assert = require('minimalistic-assert');
  3. var inherits = require('inherits');
  4. var des = require('../des');
  5. var Cipher = des.Cipher;
  6. var DES = des.DES;
  7. function EDEState(type, key) {
  8. assert.equal(key.length, 24, 'Invalid key length');
  9. var k1 = key.slice(0, 8);
  10. var k2 = key.slice(8, 16);
  11. var k3 = key.slice(16, 24);
  12. if (type === 'encrypt') {
  13. this.ciphers = [
  14. DES.create({ type: 'encrypt', key: k1 }),
  15. DES.create({ type: 'decrypt', key: k2 }),
  16. DES.create({ type: 'encrypt', key: k3 })
  17. ];
  18. } else {
  19. this.ciphers = [
  20. DES.create({ type: 'decrypt', key: k3 }),
  21. DES.create({ type: 'encrypt', key: k2 }),
  22. DES.create({ type: 'decrypt', key: k1 })
  23. ];
  24. }
  25. }
  26. function EDE(options) {
  27. Cipher.call(this, options);
  28. var state = new EDEState(this.type, this.options.key);
  29. this._edeState = state;
  30. }
  31. inherits(EDE, Cipher);
  32. module.exports = EDE;
  33. EDE.create = function create(options) {
  34. return new EDE(options);
  35. };
  36. EDE.prototype._update = function _update(inp, inOff, out, outOff) {
  37. var state = this._edeState;
  38. state.ciphers[0]._update(inp, inOff, out, outOff);
  39. state.ciphers[1]._update(out, outOff, out, outOff);
  40. state.ciphers[2]._update(out, outOff, out, outOff);
  41. };
  42. EDE.prototype._pad = DES.prototype._pad;
  43. EDE.prototype._unpad = DES.prototype._unpad;