-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathcrypto-js-wasm.js
7609 lines (6103 loc) · 618 KB
/
crypto-js-wasm.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
@license
crypto-js-wasm v1.1.1
(c) 2022-2024 peteralfredlee
https://github.com/originjs/crypto-js-wasm
Released under the MulanPSL2 License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('pako')) :
typeof define === 'function' && define.amd ? define(['pako'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.CryptoJSWasm = factory(global.pako));
})(this, (function (pako) { 'use strict';
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var pako__default = /*#__PURE__*/_interopDefaultLegacy(pako);
/**
* Latin1 encoding strategy.
*/
const Latin1 = {
/**
* Converts a word array to a Latin1 string.
*
* @param {WordArray} wordArray The word array.
*
* @return {string} The Latin1 string.
*
* @static
*
* @example
*
* const latin1String = CryptoJSW.enc.Latin1.stringify(wordArray);
*/
stringify(wordArray) {
// Shortcuts
const {
words,
sigBytes
} = wordArray; // Convert
let latin1Chars = '';
for (let i = 0; i < sigBytes; i++) {
const byte = words[i >>> 2] >>> 24 - i % 4 * 8 & 0xff;
latin1Chars += String.fromCharCode(byte);
}
return latin1Chars;
},
/**
* Converts a Latin1 string to a word array.
*
* @param {string} latin1Str The Latin1 string.
*
* @return {WordArray} The word array.
*
* @static
*
* @example
*
* const wordArray = CryptoJSW.enc.Latin1.parse(latin1String);
*/
parse(latin1Str) {
// Shortcut
const latin1StrLength = latin1Str.length; // Convert
const words = [];
let word = 0; // const words = new Array(latin1StrLength >>> 2);
for (let i = 0; i < latin1StrLength - latin1StrLength % 4; i++) {
word |= (latin1Str.charCodeAt(i) & 0xff) << 24 - i % 4 * 8;
if (i % 4 == 3) {
words[i >>> 2] = word;
word = 0;
}
}
for (let i = latin1StrLength - latin1StrLength % 4; i < latin1StrLength; i++) {
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << 24 - i % 4 * 8;
}
return new WordArray(words, latin1StrLength);
}
};
/**
* UTF-8 encoding strategy.
*/
const Utf8 = {
/**
* Converts a word array to a UTF-8 string.
*
* @param {WordArray} wordArray The word array.
*
* @return {string} The UTF-8 string.
*
* @static
*
* @example
*
* const utf8String = CryptoJSW.enc.Utf8.stringify(wordArray);
*/
stringify(wordArray) {
try {
return decodeURIComponent(escape(Latin1.stringify(wordArray)));
} catch (e) {
throw new Error('Malformed UTF-8 data');
}
},
/**
* Converts a UTF-8 string to a word array.
*
* @param {string} utf8Str The UTF-8 string.
*
* @return {WordArray} The word array.
*
* @static
*
* @example
*
* const wordArray = CryptoJSW.enc.Utf8.parse(utf8String);
*/
parse(utf8Str) {
return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
}
};
/**
* Hex encoding strategy.
*/
const Hex = {
/**
* Converts a word array to a hex string.
*
* @param {WordArray} wordArray The word array.
*
* @return {string} The hex string.
*
* @static
*
* @example
*
* const hexString = CryptoJSW.enc.Hex.stringify(wordArray);
*/
stringify(wordArray) {
// Shortcuts
const {
words,
sigBytes
} = wordArray; // Convert
const hexChars = [];
for (let i = 0; i < sigBytes; i++) {
const bite = words[i >>> 2] >>> 24 - i % 4 * 8 & 0xff;
hexChars.push((bite >>> 4).toString(16));
hexChars.push((bite & 0x0f).toString(16));
}
return hexChars.join('');
},
/**
* Converts a hex string to a word array.
*
* @param {string} hexStr The hex string.
*
* @return {WordArray} The word array.
*
* @static
*
* @example
*
* const wordArray = CryptoJSW.enc.Hex.parse(hexString);
*/
parse(hexStr) {
// Shortcut
const hexStrLength = hexStr.length; // Convert
const words = [];
for (let i = 0; i < hexStrLength; i += 2) {
words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << 24 - i % 8 * 4;
}
return new WordArray(words, hexStrLength / 2);
}
};
/**
* Determine if a value is a String
*
* @param {Object} val The value to test
* @returns {Boolean} True if value is a String, otherwise false
*/
const isString = val => typeof val === 'string';
/**
* Check if the input parameter is valid
*
* @param parameter {any} input parameter
* @param parameterDesc {string} description input parameter
* @param validTypes {string|array} valid types of parameter
* @param validValues {array} valid values of parameter
* @throws if parameter is invalid
*/
const parameterCheck = (parameter, parameterDesc, validTypes, ...validValues) => {
let isValid = true;
if (validTypes !== undefined) {
if (typeof validTypes === 'string' && typeof parameter !== validTypes) {
isValid = false;
}
if (validTypes.indexOf(typeof parameter) < 0) {
isValid = false;
}
}
if (validValues !== undefined && validValues.length > 0) {
if (validValues.indexOf(parameter) < 0) {
isValid = false;
}
}
if (!isValid) {
throw TypeError(`The input value ${parameter} of ${parameterDesc} is invalid! The type should be ${validTypes}, and the values should be ${validValues}.`);
}
};
/* eslint-disable no-use-before-define */
let crypto; // Native crypto from window (Browser)
if (typeof window !== 'undefined' && window.crypto) {
crypto = window.crypto;
} // Native crypto in web worker (Browser)
if (typeof self !== 'undefined' && self.crypto) {
crypto = self.crypto;
} // Native crypto from worker
// eslint-disable-next-line no-undef
if (typeof globalThis !== 'undefined' && globalThis.crypto) {
// eslint-disable-next-line no-undef
crypto = globalThis.crypto;
} // Native (experimental IE 11) crypto from window (Browser)
if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
crypto = window.msCrypto;
} // Native crypto from global (NodeJS)
if (!crypto && typeof global !== 'undefined' && global.crypto) {
crypto = global.crypto;
} // Native crypto import via require (NodeJS)
if (!crypto && typeof require === 'function') {
try {
crypto = require('crypto'); // eslint-disable-next-line no-empty
} catch (err) {}
}
const cryptoSecureRandomInt = () => {
if (crypto) {
// Use getRandomValues method (Browser)
if (typeof crypto.getRandomValues === 'function') {
try {
return crypto.getRandomValues(new Uint32Array(1))[0]; // eslint-disable-next-line no-empty
} catch (err) {}
} // Use randomBytes method (NodeJS)
if (typeof crypto.randomBytes === 'function') {
try {
return crypto.randomBytes(4).readInt32LE(); // eslint-disable-next-line no-empty
} catch (err) {}
}
}
throw new Error('Native crypto module could not be used to get secure random number.');
};
class Base {
/**
* Copies properties into this object.
*
* @param {Object} properties The properties to mix in.
*
* @example
*
* MyType.mixIn({
* field: 'value'
* });
*/
mixIn(properties) {
return Object.assign(this, properties);
}
/**
* Creates a copy of this object.
*
* @return {Object} The clone.
*
* @example
*
* let clone = instance.clone();
*/
clone() {
const clone = new this.constructor();
Object.assign(clone, this);
return clone;
}
/**
* Get a new instance of this class.
* Arguments to create() will be passed to constructor.
*
* @return {Object} The new object.
*
* @static
*
* @example
*
* var instance = MyType.create();
*/
static create(...args) {
return new this(...args);
}
}
/**
* An array of 32-bit words.
*
* @property {Array} words The array of 32-bit words.
* @property {number} sigBytes The number of significant bytes in this word array.
*/
class WordArray extends Base {
/**
* Initializes a newly created word array.
*
* @param {Array} words (Optional) An array of 32-bit words.
* @param {number} sigBytes (Optional) The number of significant bytes in the words.
*
* @example
*
* let wordArray = new WordArray();
* let wordArray = new WordArray([0x00010203, 0x04050607]);
* let wordArray = new WordArray([0x00010203, 0x04050607], 6);
*/
constructor(words = [], sigBytes = words.length * 4) {
super();
let typedArray = words; // Convert buffers to uint8
if (typedArray instanceof ArrayBuffer) {
typedArray = new Uint8Array(typedArray);
} // Convert other array views to uint8
if (typedArray instanceof Int8Array || typedArray instanceof Uint8ClampedArray || typedArray instanceof Int16Array || typedArray instanceof Uint16Array || typedArray instanceof Int32Array || typedArray instanceof Uint32Array || typedArray instanceof Float32Array || typedArray instanceof Float64Array) {
typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
} // Handle Uint8Array
if (typedArray instanceof Uint8Array) {
// Shortcut
const typedArrayByteLength = typedArray.byteLength; // Extract bytes
const _words = [];
for (let i = 0; i < typedArrayByteLength; i++) {
_words[i >>> 2] |= typedArray[i] << 24 - i % 4 * 8;
} // Initialize this word array
this.words = _words;
this.sigBytes = typedArrayByteLength;
} else {
// Else call normal init
this.words = words;
this.sigBytes = sigBytes;
}
}
/**
* Creates and initializes a word array
* A compatibility method for crypto-js
*
* @param {Array} words (Optional) An array of 32-bit words.
* @param {number} sigBytes (Optional) The number of significant bytes in the words.
*/
static create(words = [], sigBytes = words.length * 4) {
return new WordArray(words, sigBytes);
}
/**
* Creates a word array filled with random bytes.
*
* @param {number} nBytes The number of random bytes to generate.
*
* @return {WordArray} The random word array.
*
* @static
*
* @example
*
* const wordArray = CryptoJSW.lib.WordArray.random(16);
*/
static random(nBytes) {
const words = [];
for (var i = 0; i < nBytes; i += 4) {
words.push(cryptoSecureRandomInt());
}
return new WordArray(words, nBytes);
}
/**
* Converts this word array to a string.
*
* @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJSW.enc.Hex
*
* @return {string} The stringified word array.
*
* @example
*
* const string = wordArray + '';
* const string = wordArray.toString();
* const string = wordArray.toString(CryptoJSW.enc.Utf8);
*/
toString(encoder = Hex) {
return encoder.stringify(this);
}
/**
* Concatenates a word array to this word array.
*
* @param {WordArray} wordArray The word array to append.
*
* @return {WordArray} This word array.
*
* @example
*
* wordArray1.concat(wordArray2);
*/
concat(wordArray) {
// Shortcuts
const thisWords = this.words;
const thatWords = wordArray.words;
const thisSigBytes = this.sigBytes;
const thatSigBytes = wordArray.sigBytes; // Clamp excess bits
this.clamp(); // Concat
if (thisSigBytes % 4) {
// Copy one byte at a time
for (let i = 0; i < thatSigBytes; i++) {
const thatByte = thatWords[i >>> 2] >>> 24 - i % 4 * 8 & 0xff;
thisWords[thisSigBytes + i >>> 2] |= thatByte << 24 - (thisSigBytes + i) % 4 * 8;
}
} else {
// Copy one word at a time
for (let i = 0; i < thatSigBytes; i += 4) {
thisWords[thisSigBytes + i >>> 2] = thatWords[i >>> 2];
}
}
this.sigBytes += thatSigBytes; // Chainable
return this;
}
/**
* Removes insignificant bits.
*
* @example
*
* wordArray.clamp();
*/
clamp() {
// Shortcuts
const {
words,
sigBytes
} = this; // Clamp
words[sigBytes >>> 2] &= 0xffffffff << 32 - sigBytes % 4 * 8;
words.length = Math.ceil(sigBytes / 4);
}
/**
* Creates a copy of this word array.
*
* @return {WordArray} The clone.
*
* @example
*
* let clone = wordArray.clone();
*/
clone() {
const clone = super.clone.call(this);
clone.words = this.words.slice(0);
return clone;
}
}
/**
* Abstract buffered block algorithm template.
*
* The property blockSize must be implemented in a concrete subtype.
*
* @property {number} _minBufferSize
*
* The number of blocks that should be kept unprocessed in the buffer. Default: 0
*/
class BufferedBlockAlgorithm extends Base {
constructor() {
super();
this._minBufferSize = 0;
}
/**
* Resets this block algorithm's data buffer to its initial state.
*
* @example
*
* bufferedBlockAlgorithm.reset();
*/
reset() {
// Initial values
this._data = new WordArray();
this._nDataBytes = 0;
}
/**
* Adds new data to this block algorithm's buffer.
*
* @param {WordArray|string} data
*
* The data to append. Strings are converted to a WordArray using UTF-8.
*
* @example
*
* bufferedBlockAlgorithm._append('data');
* bufferedBlockAlgorithm._append(wordArray);
*/
_append(data) {
let m_data = data; // Convert string to WordArray, else assume WordArray already
if (isString(m_data)) {
m_data = Utf8.parse(m_data);
} // Append
this._data.concat(m_data);
this._nDataBytes += m_data.sigBytes;
}
/**
* Processes available data blocks.
*
* This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
*
* @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
*
* @return {WordArray} The processed data.
*
* @example
*
* let processedData = bufferedBlockAlgorithm._process();
* let processedData = bufferedBlockAlgorithm._process(!!'flush');
*/
_process(doFlush) {
let processedWords; // Shortcuts
const {
_data: data,
blockSize
} = this;
const dataWords = data.words;
const dataSigBytes = data.sigBytes;
const blockSizeBytes = blockSize * 4; // Count blocks ready
let nBlocksReady = dataSigBytes / blockSizeBytes;
if (doFlush) {
// Round up to include partial blocks
nBlocksReady = Math.ceil(nBlocksReady);
} else {
// Round down to include only full blocks,
// less the number of blocks that must remain in the buffer
nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
} // Count words ready
const nWordsReady = nBlocksReady * blockSize; // Count bytes ready
const nBytesReady = Math.min(nWordsReady * 4, dataSigBytes); // Process blocks
if (nWordsReady) {
for (let offset = 0; offset < nWordsReady; offset += blockSize) {
// Perform concrete-algorithm logic
this._doProcessBlock(dataWords, offset);
} // Remove processed words
processedWords = dataWords.splice(0, nWordsReady);
data.sigBytes -= nBytesReady;
} // Return processed words
return new WordArray(processedWords, nBytesReady);
}
/**
* Creates a copy of this object.
*
* @return {Object} The clone.
*
* @example
*
* let clone = bufferedBlockAlgorithm.clone();
*/
clone() {
const clone = super.clone.call(this);
clone._data = this._data.clone();
return clone;
}
}
/**
* HMAC algorithm.
*/
class HMAC extends Base {
/**
* Initializes a newly created HMAC.
*
* @param {Hasher} SubHasher The hash algorithm to use.
* @param {WordArray|string} key The secret key.
*
* @example
*
* const hmacHasher = new HMAC(CryptoJSW.algo.SHA256, key);
*/
constructor(SubHasher, key) {
super();
const hasher = new SubHasher();
this._hasher = hasher; // Convert string to WordArray, else assume WordArray already
let _key = key;
if (isString(_key)) {
_key = Utf8.parse(_key);
} // Shortcuts
const hasherBlockSize = hasher.blockSize;
const hasherBlockSizeBytes = hasherBlockSize * 4; // Allow arbitrary length keys
if (_key.sigBytes > hasherBlockSizeBytes) {
_key = hasher.finalize(key);
} // Clamp excess bits
_key.clamp(); // Clone key for inner and outer pads
const oKey = _key.clone();
this._oKey = oKey;
const iKey = _key.clone();
this._iKey = iKey; // Shortcuts
const oKeyWords = oKey.words;
const iKeyWords = iKey.words; // XOR keys with pad constants
for (let i = 0; i < hasherBlockSize; i++) {
oKeyWords[i] ^= 0x5c5c5c5c;
iKeyWords[i] ^= 0x36363636;
}
oKey.sigBytes = hasherBlockSizeBytes;
iKey.sigBytes = hasherBlockSizeBytes; // Set initial values
this.reset();
}
/**
* Resets this HMAC to its initial state.
*
* @example
*
* hmacHasher.reset();
*/
reset() {
// Shortcut
const hasher = this._hasher; // Reset
hasher.reset();
hasher.update(this._iKey);
}
/**
* Updates this HMAC with a message.
*
* @param {WordArray|string} messageUpdate The message to append.
*
* @return {HMAC} This HMAC instance.
*
* @example
*
* hmacHasher.update('message');
* hmacHasher.update(wordArray);
*/
update(messageUpdate) {
this._hasher.update(messageUpdate); // Chainable
return this;
}
/**
* Finalizes the HMAC computation.
* Note that the finalize operation is effectively a destructive, read-once operation.
*
* @param {WordArray|string} messageUpdate (Optional) A final message update.
*
* @return {WordArray} The HMAC.
*
* @example
*
* let hmac = hmacHasher.finalize();
* let hmac = hmacHasher.finalize('message');
* let hmac = hmacHasher.finalize(wordArray);
*/
finalize(messageUpdate) {
// Shortcut
const hasher = this._hasher; // Compute HMAC
const innerHash = hasher.finalize(messageUpdate);
hasher.reset();
const hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
return hmac;
}
}
/**
* Abstract hasher template.
*
* @property {number} blockSize
*
* The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
*/
class Hasher extends BufferedBlockAlgorithm {
constructor(cfg) {
super();
this.blockSize = 512 / 32;
/**
* Configuration options.
*/
this.cfg = Object.assign(new Base(), cfg); // Set initial values
this.reset();
}
/**
* Creates a shortcut function to a hasher's object interface.
*
* @param {Hasher} SubHasher The hasher to create a helper for.
*
* @return {Function} The shortcut function.
*
* @static
*
* @example
*
* let SHA256 = CryptoJSW.lib.Hasher._createHelper(CryptoJSW.algo.SHA256);
*/
static _createHelper(SubHasher) {
let result = (message, cfg) => new SubHasher(cfg).finalize(message);
result.loadWasm = async () => {
if (!SubHasher.wasm) {
await SubHasher.loadWasm();
}
};
result.outputSize = SubHasher.outputSize;
return result;
}
/**
* Creates a shortcut function to the HMAC's object interface.
*
* @param {Hasher} SubHasher The hasher to use in this HMAC helper.
*
* @return {Function} The shortcut function.
*
* @static
*
* @example
*
* const HmacSHA256 = CryptoJSW.lib.Hasher._createHmacHelper(CryptoJSW.algo.SHA256);
*/
static _createHmacHelper(SubHasher) {
let result = (message, key) => new HMAC(SubHasher, key).finalize(message);
result.loadWasm = async () => {
if (!SubHasher.wasm) {
await SubHasher.loadWasm();
}
};
return result;
}
/**
* Resets this hasher to its initial state.
*
* @example
*
* hasher.reset();
*/
reset() {
// Reset data buffer
super.reset.call(this); // Perform concrete-hasher logic
this._doReset();
}
/**
* Updates this hasher with a message.
*
* @param {WordArray|string} messageUpdate The message to append.
*
* @return {Hasher} This hasher.
*
* @example
*
* hasher.update('message');
* hasher.update(wordArray);
*/
update(messageUpdate) {
// Append
this._append(messageUpdate); // Update the hash
this._process(); // Chainable
return this;
}
/**
* Finalizes the hash computation.
* Note that the finalize operation is effectively a destructive, read-once operation.
*
* @param {WordArray|string} messageUpdate (Optional) A final message update.
*
* @return {WordArray} The hash.
*
* @example
*
* let hash = hasher.finalize();
* let hash = hasher.finalize('message');
* let hash = hasher.finalize(wordArray);
*/
finalize(messageUpdate) {
// Final message update
if (messageUpdate) {
this._append(messageUpdate);
} // Perform concrete-hasher logic
const hash = this._doFinalize();
return hash;
}
}
const X32WordArray = WordArray;
/**
* A 64-bit word.
*/
class X64Word extends Base {
/**
* Initializes a newly created 64-bit word.
*
* @param {number} high The high 32 bits.
* @param {number} low The low 32 bits.
*
* @example
*
* let x64Word = new X64Word(0x00010203, 0x04050607);
*/
constructor(high, low) {
super();
this.high = high;
this.low = low;
}
}
/**
* An array of 64-bit words.
*
* @property {Array} words The array of CryptoJSW.x64.Word objects.
* @property {number} sigBytes The number of significant bytes in this word array.
*/
class X64WordArray extends Base {
/**
* Initializes a newly created word array.
*
* @param {Array} words (Optional) An array of CryptoJSW.x64.Word objects.
* @param {number} sigBytes (Optional) The number of significant bytes in the words.
*
* @example
*
* let wordArray = new X64WordArray();
*
* let wordArray = new X64WordArray([
* new x64Word(0x00010203, 0x04050607),
* new x64Word(0x18191a1b, 0x1c1d1e1f)
* ]);
*
* let wordArray = new X64WordArray([
* new x64Word(0x00010203, 0x04050607),
* new x64Word(0x18191a1b, 0x1c1d1e1f)
* ], 10);
*/
constructor(words = [], sigBytes = words.length * 8) {
super();
this.words = words;
this.sigBytes = sigBytes;
}
/**
* Converts this 64-bit word array to a 32-bit word array.
*
* @return {CryptoJSW.lib.WordArray} This word array's data as a 32-bit word array.
*
* @example
*
* let x32WordArray = x64WordArray.toX32();
*/
toX32() {
// Shortcuts
const x64Words = this.words;
const x64WordsLength = x64Words.length; // Convert
const x32Words = [];