AI销管
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function () {
  16. // Shortcuts
  17. var C = CryptoJS;
  18. var C_lib = C.lib;
  19. var StreamCipher = C_lib.StreamCipher;
  20. var C_algo = C.algo;
  21. // Reusable objects
  22. var S = [];
  23. var C_ = [];
  24. var G = [];
  25. /**
  26. * Rabbit stream cipher algorithm
  27. */
  28. var Rabbit = C_algo.Rabbit = StreamCipher.extend({
  29. _doReset: function () {
  30. // Shortcuts
  31. var K = this._key.words;
  32. var iv = this.cfg.iv;
  33. // Swap endian
  34. for (var i = 0; i < 4; i++) {
  35. K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
  36. (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
  37. }
  38. // Generate initial state values
  39. var X = this._X = [
  40. K[0], (K[3] << 16) | (K[2] >>> 16),
  41. K[1], (K[0] << 16) | (K[3] >>> 16),
  42. K[2], (K[1] << 16) | (K[0] >>> 16),
  43. K[3], (K[2] << 16) | (K[1] >>> 16)
  44. ];
  45. // Generate initial counter values
  46. var C = this._C = [
  47. (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
  48. (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
  49. (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
  50. (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
  51. ];
  52. // Carry bit
  53. this._b = 0;
  54. // Iterate the system four times
  55. for (var i = 0; i < 4; i++) {
  56. nextState.call(this);
  57. }
  58. // Modify the counters
  59. for (var i = 0; i < 8; i++) {
  60. C[i] ^= X[(i + 4) & 7];
  61. }
  62. // IV setup
  63. if (iv) {
  64. // Shortcuts
  65. var IV = iv.words;
  66. var IV_0 = IV[0];
  67. var IV_1 = IV[1];
  68. // Generate four subvectors
  69. var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
  70. var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
  71. var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
  72. var i3 = (i2 << 16) | (i0 & 0x0000ffff);
  73. // Modify counter values
  74. C[0] ^= i0;
  75. C[1] ^= i1;
  76. C[2] ^= i2;
  77. C[3] ^= i3;
  78. C[4] ^= i0;
  79. C[5] ^= i1;
  80. C[6] ^= i2;
  81. C[7] ^= i3;
  82. // Iterate the system four times
  83. for (var i = 0; i < 4; i++) {
  84. nextState.call(this);
  85. }
  86. }
  87. },
  88. _doProcessBlock: function (M, offset) {
  89. // Shortcut
  90. var X = this._X;
  91. // Iterate the system
  92. nextState.call(this);
  93. // Generate four keystream words
  94. S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
  95. S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
  96. S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
  97. S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
  98. for (var i = 0; i < 4; i++) {
  99. // Swap endian
  100. S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
  101. (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
  102. // Encrypt
  103. M[offset + i] ^= S[i];
  104. }
  105. },
  106. blockSize: 128/32,
  107. ivSize: 64/32
  108. });
  109. function nextState() {
  110. // Shortcuts
  111. var X = this._X;
  112. var C = this._C;
  113. // Save old counter values
  114. for (var i = 0; i < 8; i++) {
  115. C_[i] = C[i];
  116. }
  117. // Calculate new counter values
  118. C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
  119. C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
  120. C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
  121. C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
  122. C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
  123. C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
  124. C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
  125. C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
  126. this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
  127. // Calculate the g-values
  128. for (var i = 0; i < 8; i++) {
  129. var gx = X[i] + C[i];
  130. // Construct high and low argument for squaring
  131. var ga = gx & 0xffff;
  132. var gb = gx >>> 16;
  133. // Calculate high and low result of squaring
  134. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
  135. var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
  136. // High XOR low
  137. G[i] = gh ^ gl;
  138. }
  139. // Calculate new state values
  140. X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
  141. X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
  142. X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
  143. X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
  144. X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
  145. X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
  146. X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
  147. X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
  148. }
  149. /**
  150. * Shortcut functions to the cipher's object interface.
  151. *
  152. * @example
  153. *
  154. * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
  155. * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
  156. */
  157. C.Rabbit = StreamCipher._createHelper(Rabbit);
  158. }());
  159. return CryptoJS.Rabbit;
  160. }));