x64-core.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {
  2. Base,
  3. WordArray,
  4. } from './core.js';
  5. const X32WordArray = WordArray;
  6. /**
  7. * A 64-bit word.
  8. */
  9. export class X64Word extends Base {
  10. /**
  11. * Initializes a newly created 64-bit word.
  12. *
  13. * @param {number} high The high 32 bits.
  14. * @param {number} low The low 32 bits.
  15. *
  16. * @example
  17. *
  18. * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
  19. */
  20. constructor(high, low) {
  21. super();
  22. this.high = high;
  23. this.low = low;
  24. }
  25. }
  26. /**
  27. * An array of 64-bit words.
  28. *
  29. * @property {Array} words The array of CryptoJS.x64.Word objects.
  30. * @property {number} sigBytes The number of significant bytes in this word array.
  31. */
  32. export class X64WordArray extends Base {
  33. /**
  34. * Initializes a newly created word array.
  35. *
  36. * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
  37. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  38. *
  39. * @example
  40. *
  41. * var wordArray = CryptoJS.x64.WordArray.create();
  42. *
  43. * var wordArray = CryptoJS.x64.WordArray.create([
  44. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  45. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  46. * ]);
  47. *
  48. * var wordArray = CryptoJS.x64.WordArray.create([
  49. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  50. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  51. * ], 10);
  52. */
  53. constructor(words = [], sigBytes = words.length * 8) {
  54. super();
  55. this.words = words;
  56. this.sigBytes = sigBytes;
  57. }
  58. /**
  59. * Converts this 64-bit word array to a 32-bit word array.
  60. *
  61. * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
  62. *
  63. * @example
  64. *
  65. * var x32WordArray = x64WordArray.toX32();
  66. */
  67. toX32() {
  68. // Shortcuts
  69. const x64Words = this.words;
  70. const x64WordsLength = x64Words.length;
  71. // Convert
  72. const x32Words = [];
  73. for (let i = 0; i < x64WordsLength; i += 1) {
  74. const x64Word = x64Words[i];
  75. x32Words.push(x64Word.high);
  76. x32Words.push(x64Word.low);
  77. }
  78. return X32WordArray.create(x32Words, this.sigBytes);
  79. }
  80. /**
  81. * Creates a copy of this word array.
  82. *
  83. * @return {X64WordArray} The clone.
  84. *
  85. * @example
  86. *
  87. * var clone = x64WordArray.clone();
  88. */
  89. clone() {
  90. const clone = super.clone.call(this);
  91. // Clone "words" array
  92. clone.words = this.words.slice(0);
  93. const { words } = clone;
  94. // Clone each X64Word object
  95. const wordsLength = words.length;
  96. for (let i = 0; i < wordsLength; i += 1) {
  97. words[i] = words[i].clone();
  98. }
  99. return clone;
  100. }
  101. }