You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

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