Word64Array.js
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Hex } from "./encoder/Hex";
import { Word32Array } from "./Word32Array";
export class Word64 {
constructor(high, low) {
this.high = high;
this.low = low;
}
clone() {
return new Word64(this.high, this.low);
}
}
/**
* An array of 64bit words
*/
export class Word64Array {
/**
* Initializes a newly created word array.
*
* @param {Array} words (Optional) An array of 64-bit words.
* @param {number} nSignificantBytes (Optional) The number of significant bytes in the words.
*
* @example
* var wordArray = new Word64Array();
* var wordArray = new Word64Array([new Word64(0x00010203, 0x04050607)]);
* var wordArray = new Word46Array([new Word64(0x00010203, 0x04050607)], 6);
*/
constructor(words, nSignificantBytes) {
this._words = words || [];
this._nSignificantBytes = typeof nSignificantBytes === "number" ? nSignificantBytes : this._words.length * 8;
}
get nSigBytes() {
return this._nSignificantBytes;
}
/**
* Set significant bytes
* @param {number} n - significant bytes
*/
set nSigBytes(n) {
this._nSignificantBytes = n;
}
/**
* Get raw reference of internal words.
* Modification of this raw array will affect internal words.
*/
get words() {
return this._words;
}
/**
* Converts this 64-bit word array to a 32-bit word array.
*
* @return {Word32Array} This word array's data as a 32-bit word array.
*
* @example
*
* var x32WordArray = x64WordArray.toX32();
*/
to32() {
const words32 = [];
for (let i = 0; i < this._words.length; i++) {
const word64 = this._words[i];
words32.push(word64.high);
words32.push(word64.low);
}
return new Word32Array(words32, this._nSignificantBytes);
}
/**
* Converts this word array to a string.
*
* @param {IEncoder?} encoder The encoding strategy to use. Default: CryptoJS.enc.Hex
* @return {string} The stringified word array.
* @example
* var string = wordArray + '';
* var string = wordArray.toString();
* var string = wordArray.toString(Utf8);
*/
toString(encoder) {
if (!encoder) {
return Hex.stringify(this.to32());
}
return encoder.stringify(this.to32());
}
/**
* Creates a copy of this word array.
*
* @return {Word64Array} The clone.
* @example
* var clone = wordArray.clone();
*/
clone() {
const words = this._words.slice();
for (let i = 0; i < words.length; i++) {
words[i] = words[i].clone();
}
return new Word64Array(words, this._nSignificantBytes);
}
}