mode-ofb.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./cipher-core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core", "./cipher-core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. /**
  14. * Output Feedback block mode.
  15. */
  16. CryptoJS.mode.OFB = (function () {
  17. var OFB = CryptoJS.lib.BlockCipherMode.extend();
  18. var Encryptor = OFB.Encryptor = OFB.extend({
  19. processBlock: function (words, offset) {
  20. // Shortcuts
  21. var cipher = this._cipher
  22. var blockSize = cipher.blockSize;
  23. var iv = this._iv;
  24. var keystream = this._keystream;
  25. // Generate keystream
  26. if (iv) {
  27. keystream = this._keystream = iv.slice(0);
  28. // Remove IV for subsequent blocks
  29. this._iv = undefined;
  30. }
  31. cipher.encryptBlock(keystream, 0);
  32. // Encrypt
  33. for (var i = 0; i < blockSize; i++) {
  34. words[offset + i] ^= keystream[i];
  35. }
  36. }
  37. });
  38. OFB.Decryptor = Encryptor;
  39. return OFB;
  40. }());
  41. return CryptoJS.mode.OFB;
  42. }));