x64-core.js 8.6 KB

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