x64-core.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function (undefined) {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var Base = C_lib.Base;
  18. var X32WordArray = C_lib.WordArray;
  19. /**
  20. * x64 namespace.
  21. */
  22. var C_x64 = C.x64 = {};
  23. /**
  24. * A 64-bit word.
  25. */
  26. var X64Word = C_x64.Word = Base.extend({
  27. /**
  28. * Initializes a newly created 64-bit word.
  29. *
  30. * @param {number} high The high 32 bits.
  31. * @param {number} low The low 32 bits.
  32. *
  33. * @example
  34. *
  35. * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
  36. */
  37. init: function (high, low) {
  38. this.high = high;
  39. this.low = low;
  40. }
  41. /**
  42. * Bitwise NOTs this word.
  43. *
  44. * @return {X64Word} A new x64-Word object after negating.
  45. *
  46. * @example
  47. *
  48. * var negated = x64Word.not();
  49. */
  50. // not: function () {
  51. // var high = ~this.high;
  52. // var low = ~this.low;
  53. // return X64Word.create(high, low);
  54. // },
  55. /**
  56. * Bitwise ANDs this word with the passed word.
  57. *
  58. * @param {X64Word} word The x64-Word to AND with this word.
  59. *
  60. * @return {X64Word} A new x64-Word object after ANDing.
  61. *
  62. * @example
  63. *
  64. * var anded = x64Word.and(anotherX64Word);
  65. */
  66. // and: function (word) {
  67. // var high = this.high & word.high;
  68. // var low = this.low & word.low;
  69. // return X64Word.create(high, low);
  70. // },
  71. /**
  72. * Bitwise ORs this word with the passed word.
  73. *
  74. * @param {X64Word} word The x64-Word to OR with this word.
  75. *
  76. * @return {X64Word} A new x64-Word object after ORing.
  77. *
  78. * @example
  79. *
  80. * var ored = x64Word.or(anotherX64Word);
  81. */
  82. // or: function (word) {
  83. // var high = this.high | word.high;
  84. // var low = this.low | word.low;
  85. // return X64Word.create(high, low);
  86. // },
  87. /**
  88. * Bitwise XORs this word with the passed word.
  89. *
  90. * @param {X64Word} word The x64-Word to XOR with this word.
  91. *
  92. * @return {X64Word} A new x64-Word object after XORing.
  93. *
  94. * @example
  95. *
  96. * var xored = x64Word.xor(anotherX64Word);
  97. */
  98. // xor: function (word) {
  99. // var high = this.high ^ word.high;
  100. // var low = this.low ^ word.low;
  101. // return X64Word.create(high, low);
  102. // },
  103. /**
  104. * Shifts this word n bits to the left.
  105. *
  106. * @param {number} n The number of bits to shift.
  107. *
  108. * @return {X64Word} A new x64-Word object after shifting.
  109. *
  110. * @example
  111. *
  112. * var shifted = x64Word.shiftL(25);
  113. */
  114. // shiftL: function (n) {
  115. // if (n < 32) {
  116. // var high = (this.high << n) | (this.low >>> (32 - n));
  117. // var low = this.low << n;
  118. // } else {
  119. // var high = this.low << (n - 32);
  120. // var low = 0;
  121. // }
  122. // return X64Word.create(high, low);
  123. // },
  124. /**
  125. * Shifts this word n bits to the right.
  126. *
  127. * @param {number} n The number of bits to shift.
  128. *
  129. * @return {X64Word} A new x64-Word object after shifting.
  130. *
  131. * @example
  132. *
  133. * var shifted = x64Word.shiftR(7);
  134. */
  135. // shiftR: function (n) {
  136. // if (n < 32) {
  137. // var low = (this.low >>> n) | (this.high << (32 - n));
  138. // var high = this.high >>> n;
  139. // } else {
  140. // var low = this.high >>> (n - 32);
  141. // var high = 0;
  142. // }
  143. // return X64Word.create(high, low);
  144. // },
  145. /**
  146. * Rotates this word n bits to the left.
  147. *
  148. * @param {number} n The number of bits to rotate.
  149. *
  150. * @return {X64Word} A new x64-Word object after rotating.
  151. *
  152. * @example
  153. *
  154. * var rotated = x64Word.rotL(25);
  155. */
  156. // rotL: function (n) {
  157. // return this.shiftL(n).or(this.shiftR(64 - n));
  158. // },
  159. /**
  160. * Rotates this word n bits to the right.
  161. *
  162. * @param {number} n The number of bits to rotate.
  163. *
  164. * @return {X64Word} A new x64-Word object after rotating.
  165. *
  166. * @example
  167. *
  168. * var rotated = x64Word.rotR(7);
  169. */
  170. // rotR: function (n) {
  171. // return this.shiftR(n).or(this.shiftL(64 - n));
  172. // },
  173. /**
  174. * Adds this word with the passed word.
  175. *
  176. * @param {X64Word} word The x64-Word to add with this word.
  177. *
  178. * @return {X64Word} A new x64-Word object after adding.
  179. *
  180. * @example
  181. *
  182. * var added = x64Word.add(anotherX64Word);
  183. */
  184. // add: function (word) {
  185. // var low = (this.low + word.low) | 0;
  186. // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0;
  187. // var high = (this.high + word.high + carry) | 0;
  188. // return X64Word.create(high, low);
  189. // }
  190. });
  191. /**
  192. * An array of 64-bit words.
  193. *
  194. * @property {Array} words The array of CryptoJS.x64.Word objects.
  195. * @property {number} sigBytes The number of significant bytes in this word array.
  196. */
  197. var X64WordArray = C_x64.WordArray = Base.extend({
  198. /**
  199. * Initializes a newly created word array.
  200. *
  201. * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
  202. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  203. *
  204. * @example
  205. *
  206. * var wordArray = CryptoJS.x64.WordArray.create();
  207. *
  208. * var wordArray = CryptoJS.x64.WordArray.create([
  209. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  210. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  211. * ]);
  212. *
  213. * var wordArray = CryptoJS.x64.WordArray.create([
  214. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  215. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  216. * ], 10);
  217. */
  218. init: function (words, sigBytes) {
  219. words = this.words = words || [];
  220. if (sigBytes != undefined) {
  221. this.sigBytes = sigBytes;
  222. } else {
  223. this.sigBytes = words.length * 8;
  224. }
  225. },
  226. /**
  227. * Converts this 64-bit word array to a 32-bit word array.
  228. *
  229. * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
  230. *
  231. * @example
  232. *
  233. * var x32WordArray = x64WordArray.toX32();
  234. */
  235. toX32: function () {
  236. // Shortcuts
  237. var x64Words = this.words;
  238. var x64WordsLength = x64Words.length;
  239. // Convert
  240. var x32Words = [];
  241. for (var i = 0; i < x64WordsLength; i++) {
  242. var x64Word = x64Words[i];
  243. x32Words.push(x64Word.high);
  244. x32Words.push(x64Word.low);
  245. }
  246. return X32WordArray.create(x32Words, this.sigBytes);
  247. },
  248. /**
  249. * Creates a copy of this word array.
  250. *
  251. * @return {X64WordArray} The clone.
  252. *
  253. * @example
  254. *
  255. * var clone = x64WordArray.clone();
  256. */
  257. clone: function () {
  258. var clone = Base.clone.call(this);
  259. // Clone "words" array
  260. var words = clone.words = this.words.slice(0);
  261. // Clone each X64Word object
  262. var wordsLength = words.length;
  263. for (var i = 0; i < wordsLength; i++) {
  264. words[i] = words[i].clone();
  265. }
  266. return clone;
  267. }
  268. });
  269. }());
  270. return CryptoJS;
  271. }));