AI销管
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.

mode-ctr.js 1.4 KiB

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