|
|
|
1
|
+;
|
|
|
|
2
|
+(function (root, factory) {
|
|
|
|
3
|
+ if (typeof exports === "object") {
|
|
|
|
4
|
+ // CommonJS
|
|
|
|
5
|
+ module.exports = exports = factory();
|
|
|
|
6
|
+ } else if (typeof define === "function" && define.amd) {
|
|
|
|
7
|
+ // AMD
|
|
|
|
8
|
+ define([], factory);
|
|
|
|
9
|
+ } else {
|
|
|
|
10
|
+ // Global (browser)
|
|
|
|
11
|
+ root.CryptoJS = factory();
|
|
|
|
12
|
+ }
|
|
|
|
13
|
+}(this, function () {
|
|
|
|
14
|
+
|
|
|
|
15
|
+ /**
|
|
|
|
16
|
+ * CryptoJS core components.
|
|
|
|
17
|
+ */
|
|
|
|
18
|
+ var CryptoJS = CryptoJS || (function (Math, undefined) {
|
|
|
|
19
|
+ /*
|
|
|
|
20
|
+ * Local polyfil of Object.create
|
|
|
|
21
|
+ */
|
|
|
|
22
|
+ var create = Object.create || (function () {
|
|
|
|
23
|
+ function F() {};
|
|
|
|
24
|
+
|
|
|
|
25
|
+ return function (obj) {
|
|
|
|
26
|
+ var subtype;
|
|
|
|
27
|
+
|
|
|
|
28
|
+ F.prototype = obj;
|
|
|
|
29
|
+
|
|
|
|
30
|
+ subtype = new F();
|
|
|
|
31
|
+
|
|
|
|
32
|
+ F.prototype = null;
|
|
|
|
33
|
+
|
|
|
|
34
|
+ return subtype;
|
|
|
|
35
|
+ };
|
|
|
|
36
|
+ }())
|
|
|
|
37
|
+
|
|
|
|
38
|
+ /**
|
|
|
|
39
|
+ * CryptoJS namespace.
|
|
|
|
40
|
+ */
|
|
|
|
41
|
+ var C = {};
|
|
|
|
42
|
+
|
|
|
|
43
|
+ /**
|
|
|
|
44
|
+ * Library namespace.
|
|
|
|
45
|
+ */
|
|
|
|
46
|
+ var C_lib = C.lib = {};
|
|
|
|
47
|
+
|
|
|
|
48
|
+ /**
|
|
|
|
49
|
+ * Base object for prototypal inheritance.
|
|
|
|
50
|
+ */
|
|
|
|
51
|
+ var Base = C_lib.Base = (function () {
|
|
|
|
52
|
+
|
|
|
|
53
|
+
|
|
|
|
54
|
+ return {
|
|
|
|
55
|
+ /**
|
|
|
|
56
|
+ * Creates a new object that inherits from this object.
|
|
|
|
57
|
+ *
|
|
|
|
58
|
+ * @param {Object} overrides Properties to copy into the new object.
|
|
|
|
59
|
+ *
|
|
|
|
60
|
+ * @return {Object} The new object.
|
|
|
|
61
|
+ *
|
|
|
|
62
|
+ * @static
|
|
|
|
63
|
+ *
|
|
|
|
64
|
+ * @example
|
|
|
|
65
|
+ *
|
|
|
|
66
|
+ * var MyType = CryptoJS.lib.Base.extend({
|
|
|
|
67
|
+ * field: 'value',
|
|
|
|
68
|
+ *
|
|
|
|
69
|
+ * method: function () {
|
|
|
|
70
|
+ * }
|
|
|
|
71
|
+ * });
|
|
|
|
72
|
+ */
|
|
|
|
73
|
+ extend: function (overrides) {
|
|
|
|
74
|
+ // Spawn
|
|
|
|
75
|
+ var subtype = create(this);
|
|
|
|
76
|
+
|
|
|
|
77
|
+ // Augment
|
|
|
|
78
|
+ if (overrides) {
|
|
|
|
79
|
+ subtype.mixIn(overrides);
|
|
|
|
80
|
+ }
|
|
|
|
81
|
+
|
|
|
|
82
|
+ // Create default initializer
|
|
|
|
83
|
+ if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
|
|
|
|
84
|
+ subtype.init = function () {
|
|
|
|
85
|
+ subtype.$super.init.apply(this, arguments);
|
|
|
|
86
|
+ };
|
|
|
|
87
|
+ }
|
|
|
|
88
|
+
|
|
|
|
89
|
+ // Initializer's prototype is the subtype object
|
|
|
|
90
|
+ subtype.init.prototype = subtype;
|
|
|
|
91
|
+
|
|
|
|
92
|
+ // Reference supertype
|
|
|
|
93
|
+ subtype.$super = this;
|
|
|
|
94
|
+
|
|
|
|
95
|
+ return subtype;
|
|
|
|
96
|
+ },
|
|
|
|
97
|
+
|
|
|
|
98
|
+ /**
|
|
|
|
99
|
+ * Extends this object and runs the init method.
|
|
|
|
100
|
+ * Arguments to create() will be passed to init().
|
|
|
|
101
|
+ *
|
|
|
|
102
|
+ * @return {Object} The new object.
|
|
|
|
103
|
+ *
|
|
|
|
104
|
+ * @static
|
|
|
|
105
|
+ *
|
|
|
|
106
|
+ * @example
|
|
|
|
107
|
+ *
|
|
|
|
108
|
+ * var instance = MyType.create();
|
|
|
|
109
|
+ */
|
|
|
|
110
|
+ create: function () {
|
|
|
|
111
|
+ var instance = this.extend();
|
|
|
|
112
|
+ instance.init.apply(instance, arguments);
|
|
|
|
113
|
+
|
|
|
|
114
|
+ return instance;
|
|
|
|
115
|
+ },
|
|
|
|
116
|
+
|
|
|
|
117
|
+ /**
|
|
|
|
118
|
+ * Initializes a newly created object.
|
|
|
|
119
|
+ * Override this method to add some logic when your objects are created.
|
|
|
|
120
|
+ *
|
|
|
|
121
|
+ * @example
|
|
|
|
122
|
+ *
|
|
|
|
123
|
+ * var MyType = CryptoJS.lib.Base.extend({
|
|
|
|
124
|
+ * init: function () {
|
|
|
|
125
|
+ * // ...
|
|
|
|
126
|
+ * }
|
|
|
|
127
|
+ * });
|
|
|
|
128
|
+ */
|
|
|
|
129
|
+ init: function () {},
|
|
|
|
130
|
+
|
|
|
|
131
|
+ /**
|
|
|
|
132
|
+ * Copies properties into this object.
|
|
|
|
133
|
+ *
|
|
|
|
134
|
+ * @param {Object} properties The properties to mix in.
|
|
|
|
135
|
+ *
|
|
|
|
136
|
+ * @example
|
|
|
|
137
|
+ *
|
|
|
|
138
|
+ * MyType.mixIn({
|
|
|
|
139
|
+ * field: 'value'
|
|
|
|
140
|
+ * });
|
|
|
|
141
|
+ */
|
|
|
|
142
|
+ mixIn: function (properties) {
|
|
|
|
143
|
+ for (var propertyName in properties) {
|
|
|
|
144
|
+ if (properties.hasOwnProperty(propertyName)) {
|
|
|
|
145
|
+ this[propertyName] = properties[propertyName];
|
|
|
|
146
|
+ }
|
|
|
|
147
|
+ }
|
|
|
|
148
|
+
|
|
|
|
149
|
+ // IE won't copy toString using the loop above
|
|
|
|
150
|
+ if (properties.hasOwnProperty('toString')) {
|
|
|
|
151
|
+ this.toString = properties.toString;
|
|
|
|
152
|
+ }
|
|
|
|
153
|
+ },
|
|
|
|
154
|
+
|
|
|
|
155
|
+ /**
|
|
|
|
156
|
+ * Creates a copy of this object.
|
|
|
|
157
|
+ *
|
|
|
|
158
|
+ * @return {Object} The clone.
|
|
|
|
159
|
+ *
|
|
|
|
160
|
+ * @example
|
|
|
|
161
|
+ *
|
|
|
|
162
|
+ * var clone = instance.clone();
|
|
|
|
163
|
+ */
|
|
|
|
164
|
+ clone: function () {
|
|
|
|
165
|
+ return this.init.prototype.extend(this);
|
|
|
|
166
|
+ }
|
|
|
|
167
|
+ };
|
|
|
|
168
|
+ }());
|
|
|
|
169
|
+
|
|
|
|
170
|
+ /**
|
|
|
|
171
|
+ * An array of 32-bit words.
|
|
|
|
172
|
+ *
|
|
|
|
173
|
+ * @property {Array} words The array of 32-bit words.
|
|
|
|
174
|
+ * @property {number} sigBytes The number of significant bytes in this word array.
|
|
|
|
175
|
+ */
|
|
|
|
176
|
+ var WordArray = C_lib.WordArray = Base.extend({
|
|
|
|
177
|
+ /**
|
|
|
|
178
|
+ * Initializes a newly created word array.
|
|
|
|
179
|
+ *
|
|
|
|
180
|
+ * @param {Array} words (Optional) An array of 32-bit words.
|
|
|
|
181
|
+ * @param {number} sigBytes (Optional) The number of significant bytes in the words.
|
|
|
|
182
|
+ *
|
|
|
|
183
|
+ * @example
|
|
|
|
184
|
+ *
|
|
|
|
185
|
+ * var wordArray = CryptoJS.lib.WordArray.create();
|
|
|
|
186
|
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
|
|
|
|
187
|
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
|
|
|
|
188
|
+ */
|
|
|
|
189
|
+ init: function (words, sigBytes) {
|
|
|
|
190
|
+ words = this.words = words || [];
|
|
|
|
191
|
+
|
|
|
|
192
|
+ if (sigBytes != undefined) {
|
|
|
|
193
|
+ this.sigBytes = sigBytes;
|
|
|
|
194
|
+ } else {
|
|
|
|
195
|
+ this.sigBytes = words.length * 4;
|
|
|
|
196
|
+ }
|
|
|
|
197
|
+ },
|
|
|
|
198
|
+
|
|
|
|
199
|
+ /**
|
|
|
|
200
|
+ * Converts this word array to a string.
|
|
|
|
201
|
+ *
|
|
|
|
202
|
+ * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
|
|
|
|
203
|
+ *
|
|
|
|
204
|
+ * @return {string} The stringified word array.
|
|
|
|
205
|
+ *
|
|
|
|
206
|
+ * @example
|
|
|
|
207
|
+ *
|
|
|
|
208
|
+ * var string = wordArray + '';
|
|
|
|
209
|
+ * var string = wordArray.toString();
|
|
|
|
210
|
+ * var string = wordArray.toString(CryptoJS.enc.Utf8);
|
|
|
|
211
|
+ */
|
|
|
|
212
|
+ toString: function (encoder) {
|
|
|
|
213
|
+ return (encoder || Hex).stringify(this);
|
|
|
|
214
|
+ },
|
|
|
|
215
|
+
|
|
|
|
216
|
+ /**
|
|
|
|
217
|
+ * Concatenates a word array to this word array.
|
|
|
|
218
|
+ *
|
|
|
|
219
|
+ * @param {WordArray} wordArray The word array to append.
|
|
|
|
220
|
+ *
|
|
|
|
221
|
+ * @return {WordArray} This word array.
|
|
|
|
222
|
+ *
|
|
|
|
223
|
+ * @example
|
|
|
|
224
|
+ *
|
|
|
|
225
|
+ * wordArray1.concat(wordArray2);
|
|
|
|
226
|
+ */
|
|
|
|
227
|
+ concat: function (wordArray) {
|
|
|
|
228
|
+ // Shortcuts
|
|
|
|
229
|
+ var thisWords = this.words;
|
|
|
|
230
|
+ var thatWords = wordArray.words;
|
|
|
|
231
|
+ var thisSigBytes = this.sigBytes;
|
|
|
|
232
|
+ var thatSigBytes = wordArray.sigBytes;
|
|
|
|
233
|
+
|
|
|
|
234
|
+ // Clamp excess bits
|
|
|
|
235
|
+ this.clamp();
|
|
|
|
236
|
+
|
|
|
|
237
|
+ // Concat
|
|
|
|
238
|
+ if (thisSigBytes % 4) {
|
|
|
|
239
|
+ // Copy one byte at a time
|
|
|
|
240
|
+ for (var i = 0; i < thatSigBytes; i++) {
|
|
|
|
241
|
+ var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
|
|
242
|
+ thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
|
|
|
|
243
|
+ }
|
|
|
|
244
|
+ } else {
|
|
|
|
245
|
+ // Copy one word at a time
|
|
|
|
246
|
+ for (var i = 0; i < thatSigBytes; i += 4) {
|
|
|
|
247
|
+ thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
|
|
|
|
248
|
+ }
|
|
|
|
249
|
+ }
|
|
|
|
250
|
+ this.sigBytes += thatSigBytes;
|
|
|
|
251
|
+
|
|
|
|
252
|
+ // Chainable
|
|
|
|
253
|
+ return this;
|
|
|
|
254
|
+ },
|
|
|
|
255
|
+
|
|
|
|
256
|
+ /**
|
|
|
|
257
|
+ * Removes insignificant bits.
|
|
|
|
258
|
+ *
|
|
|
|
259
|
+ * @example
|
|
|
|
260
|
+ *
|
|
|
|
261
|
+ * wordArray.clamp();
|
|
|
|
262
|
+ */
|
|
|
|
263
|
+ clamp: function () {
|
|
|
|
264
|
+ // Shortcuts
|
|
|
|
265
|
+ var words = this.words;
|
|
|
|
266
|
+ var sigBytes = this.sigBytes;
|
|
|
|
267
|
+
|
|
|
|
268
|
+ // Clamp
|
|
|
|
269
|
+ words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
|
|
|
|
270
|
+ words.length = Math.ceil(sigBytes / 4);
|
|
|
|
271
|
+ },
|
|
|
|
272
|
+
|
|
|
|
273
|
+ /**
|
|
|
|
274
|
+ * Creates a copy of this word array.
|
|
|
|
275
|
+ *
|
|
|
|
276
|
+ * @return {WordArray} The clone.
|
|
|
|
277
|
+ *
|
|
|
|
278
|
+ * @example
|
|
|
|
279
|
+ *
|
|
|
|
280
|
+ * var clone = wordArray.clone();
|
|
|
|
281
|
+ */
|
|
|
|
282
|
+ clone: function () {
|
|
|
|
283
|
+ var clone = Base.clone.call(this);
|
|
|
|
284
|
+ clone.words = this.words.slice(0);
|
|
|
|
285
|
+
|
|
|
|
286
|
+ return clone;
|
|
|
|
287
|
+ },
|
|
|
|
288
|
+
|
|
|
|
289
|
+ /**
|
|
|
|
290
|
+ * Creates a word array filled with random bytes.
|
|
|
|
291
|
+ *
|
|
|
|
292
|
+ * @param {number} nBytes The number of random bytes to generate.
|
|
|
|
293
|
+ *
|
|
|
|
294
|
+ * @return {WordArray} The random word array.
|
|
|
|
295
|
+ *
|
|
|
|
296
|
+ * @static
|
|
|
|
297
|
+ *
|
|
|
|
298
|
+ * @example
|
|
|
|
299
|
+ *
|
|
|
|
300
|
+ * var wordArray = CryptoJS.lib.WordArray.random(16);
|
|
|
|
301
|
+ */
|
|
|
|
302
|
+ random: function (nBytes) {
|
|
|
|
303
|
+ var words = [];
|
|
|
|
304
|
+
|
|
|
|
305
|
+ var r = (function (m_w) {
|
|
|
|
306
|
+ var m_w = m_w;
|
|
|
|
307
|
+ var m_z = 0x3ade68b1;
|
|
|
|
308
|
+ var mask = 0xffffffff;
|
|
|
|
309
|
+
|
|
|
|
310
|
+ return function () {
|
|
|
|
311
|
+ m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
|
|
|
|
312
|
+ m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
|
|
|
|
313
|
+ var result = ((m_z << 0x10) + m_w) & mask;
|
|
|
|
314
|
+ result /= 0x100000000;
|
|
|
|
315
|
+ result += 0.5;
|
|
|
|
316
|
+ return result * (Math.random() > .5 ? 1 : -1);
|
|
|
|
317
|
+ }
|
|
|
|
318
|
+ });
|
|
|
|
319
|
+
|
|
|
|
320
|
+ for (var i = 0, rcache; i < nBytes; i += 4) {
|
|
|
|
321
|
+ var _r = r((rcache || Math.random()) * 0x100000000);
|
|
|
|
322
|
+
|
|
|
|
323
|
+ rcache = _r() * 0x3ade67b7;
|
|
|
|
324
|
+ words.push((_r() * 0x100000000) | 0);
|
|
|
|
325
|
+ }
|
|
|
|
326
|
+
|
|
|
|
327
|
+ return new WordArray.init(words, nBytes);
|
|
|
|
328
|
+ }
|
|
|
|
329
|
+ });
|
|
|
|
330
|
+
|
|
|
|
331
|
+ /**
|
|
|
|
332
|
+ * Encoder namespace.
|
|
|
|
333
|
+ */
|
|
|
|
334
|
+ var C_enc = C.enc = {};
|
|
|
|
335
|
+
|
|
|
|
336
|
+ /**
|
|
|
|
337
|
+ * Hex encoding strategy.
|
|
|
|
338
|
+ */
|
|
|
|
339
|
+ var Hex = C_enc.Hex = {
|
|
|
|
340
|
+ /**
|
|
|
|
341
|
+ * Converts a word array to a hex string.
|
|
|
|
342
|
+ *
|
|
|
|
343
|
+ * @param {WordArray} wordArray The word array.
|
|
|
|
344
|
+ *
|
|
|
|
345
|
+ * @return {string} The hex string.
|
|
|
|
346
|
+ *
|
|
|
|
347
|
+ * @static
|
|
|
|
348
|
+ *
|
|
|
|
349
|
+ * @example
|
|
|
|
350
|
+ *
|
|
|
|
351
|
+ * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
|
|
|
|
352
|
+ */
|
|
|
|
353
|
+ stringify: function (wordArray) {
|
|
|
|
354
|
+ // Shortcuts
|
|
|
|
355
|
+ var words = wordArray.words;
|
|
|
|
356
|
+ var sigBytes = wordArray.sigBytes;
|
|
|
|
357
|
+
|
|
|
|
358
|
+ // Convert
|
|
|
|
359
|
+ var hexChars = [];
|
|
|
|
360
|
+ for (var i = 0; i < sigBytes; i++) {
|
|
|
|
361
|
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
|
|
362
|
+ hexChars.push((bite >>> 4).toString(16));
|
|
|
|
363
|
+ hexChars.push((bite & 0x0f).toString(16));
|
|
|
|
364
|
+ }
|
|
|
|
365
|
+
|
|
|
|
366
|
+ return hexChars.join('');
|
|
|
|
367
|
+ },
|
|
|
|
368
|
+
|
|
|
|
369
|
+ /**
|
|
|
|
370
|
+ * Converts a hex string to a word array.
|
|
|
|
371
|
+ *
|
|
|
|
372
|
+ * @param {string} hexStr The hex string.
|
|
|
|
373
|
+ *
|
|
|
|
374
|
+ * @return {WordArray} The word array.
|
|
|
|
375
|
+ *
|
|
|
|
376
|
+ * @static
|
|
|
|
377
|
+ *
|
|
|
|
378
|
+ * @example
|
|
|
|
379
|
+ *
|
|
|
|
380
|
+ * var wordArray = CryptoJS.enc.Hex.parse(hexString);
|
|
|
|
381
|
+ */
|
|
|
|
382
|
+ parse: function (hexStr) {
|
|
|
|
383
|
+ // Shortcut
|
|
|
|
384
|
+ var hexStrLength = hexStr.length;
|
|
|
|
385
|
+
|
|
|
|
386
|
+ // Convert
|
|
|
|
387
|
+ var words = [];
|
|
|
|
388
|
+ for (var i = 0; i < hexStrLength; i += 2) {
|
|
|
|
389
|
+ words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
|
|
|
|
390
|
+ }
|
|
|
|
391
|
+
|
|
|
|
392
|
+ return new WordArray.init(words, hexStrLength / 2);
|
|
|
|
393
|
+ }
|
|
|
|
394
|
+ };
|
|
|
|
395
|
+
|
|
|
|
396
|
+ /**
|
|
|
|
397
|
+ * Latin1 encoding strategy.
|
|
|
|
398
|
+ */
|
|
|
|
399
|
+ var Latin1 = C_enc.Latin1 = {
|
|
|
|
400
|
+ /**
|
|
|
|
401
|
+ * Converts a word array to a Latin1 string.
|
|
|
|
402
|
+ *
|
|
|
|
403
|
+ * @param {WordArray} wordArray The word array.
|
|
|
|
404
|
+ *
|
|
|
|
405
|
+ * @return {string} The Latin1 string.
|
|
|
|
406
|
+ *
|
|
|
|
407
|
+ * @static
|
|
|
|
408
|
+ *
|
|
|
|
409
|
+ * @example
|
|
|
|
410
|
+ *
|
|
|
|
411
|
+ * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
|
|
|
|
412
|
+ */
|
|
|
|
413
|
+ stringify: function (wordArray) {
|
|
|
|
414
|
+ // Shortcuts
|
|
|
|
415
|
+ var words = wordArray.words;
|
|
|
|
416
|
+ var sigBytes = wordArray.sigBytes;
|
|
|
|
417
|
+
|
|
|
|
418
|
+ // Convert
|
|
|
|
419
|
+ var latin1Chars = [];
|
|
|
|
420
|
+ for (var i = 0; i < sigBytes; i++) {
|
|
|
|
421
|
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
|
|
422
|
+ latin1Chars.push(String.fromCharCode(bite));
|
|
|
|
423
|
+ }
|
|
|
|
424
|
+
|
|
|
|
425
|
+ return latin1Chars.join('');
|
|
|
|
426
|
+ },
|
|
|
|
427
|
+
|
|
|
|
428
|
+ /**
|
|
|
|
429
|
+ * Converts a Latin1 string to a word array.
|
|
|
|
430
|
+ *
|
|
|
|
431
|
+ * @param {string} latin1Str The Latin1 string.
|
|
|
|
432
|
+ *
|
|
|
|
433
|
+ * @return {WordArray} The word array.
|
|
|
|
434
|
+ *
|
|
|
|
435
|
+ * @static
|
|
|
|
436
|
+ *
|
|
|
|
437
|
+ * @example
|
|
|
|
438
|
+ *
|
|
|
|
439
|
+ * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
|
|
|
|
440
|
+ */
|
|
|
|
441
|
+ parse: function (latin1Str) {
|
|
|
|
442
|
+ // Shortcut
|
|
|
|
443
|
+ var latin1StrLength = latin1Str.length;
|
|
|
|
444
|
+
|
|
|
|
445
|
+ // Convert
|
|
|
|
446
|
+ var words = [];
|
|
|
|
447
|
+ for (var i = 0; i < latin1StrLength; i++) {
|
|
|
|
448
|
+ words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
|
|
|
|
449
|
+ }
|
|
|
|
450
|
+
|
|
|
|
451
|
+ return new WordArray.init(words, latin1StrLength);
|
|
|
|
452
|
+ }
|
|
|
|
453
|
+ };
|
|
|
|
454
|
+
|
|
|
|
455
|
+ /**
|
|
|
|
456
|
+ * UTF-8 encoding strategy.
|
|
|
|
457
|
+ */
|
|
|
|
458
|
+ var Utf8 = C_enc.Utf8 = {
|
|
|
|
459
|
+ /**
|
|
|
|
460
|
+ * Converts a word array to a UTF-8 string.
|
|
|
|
461
|
+ *
|
|
|
|
462
|
+ * @param {WordArray} wordArray The word array.
|
|
|
|
463
|
+ *
|
|
|
|
464
|
+ * @return {string} The UTF-8 string.
|
|
|
|
465
|
+ *
|
|
|
|
466
|
+ * @static
|
|
|
|
467
|
+ *
|
|
|
|
468
|
+ * @example
|
|
|
|
469
|
+ *
|
|
|
|
470
|
+ * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
|
|
|
|
471
|
+ */
|
|
|
|
472
|
+ stringify: function (wordArray) {
|
|
|
|
473
|
+ try {
|
|
|
|
474
|
+ return decodeURIComponent(escape(Latin1.stringify(wordArray)));
|
|
|
|
475
|
+ } catch (e) {
|
|
|
|
476
|
+ throw new Error('Malformed UTF-8 data');
|
|
|
|
477
|
+ }
|
|
|
|
478
|
+ },
|
|
|
|
479
|
+
|
|
|
|
480
|
+ /**
|
|
|
|
481
|
+ * Converts a UTF-8 string to a word array.
|
|
|
|
482
|
+ *
|
|
|
|
483
|
+ * @param {string} utf8Str The UTF-8 string.
|
|
|
|
484
|
+ *
|
|
|
|
485
|
+ * @return {WordArray} The word array.
|
|
|
|
486
|
+ *
|
|
|
|
487
|
+ * @static
|
|
|
|
488
|
+ *
|
|
|
|
489
|
+ * @example
|
|
|
|
490
|
+ *
|
|
|
|
491
|
+ * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
|
|
|
|
492
|
+ */
|
|
|
|
493
|
+ parse: function (utf8Str) {
|
|
|
|
494
|
+ return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
|
|
|
|
495
|
+ }
|
|
|
|
496
|
+ };
|
|
|
|
497
|
+
|
|
|
|
498
|
+ /**
|
|
|
|
499
|
+ * Abstract buffered block algorithm template.
|
|
|
|
500
|
+ *
|
|
|
|
501
|
+ * The property blockSize must be implemented in a concrete subtype.
|
|
|
|
502
|
+ *
|
|
|
|
503
|
+ * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
|
|
|
|
504
|
+ */
|
|
|
|
505
|
+ var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
|
|
|
|
506
|
+ /**
|
|
|
|
507
|
+ * Resets this block algorithm's data buffer to its initial state.
|
|
|
|
508
|
+ *
|
|
|
|
509
|
+ * @example
|
|
|
|
510
|
+ *
|
|
|
|
511
|
+ * bufferedBlockAlgorithm.reset();
|
|
|
|
512
|
+ */
|
|
|
|
513
|
+ reset: function () {
|
|
|
|
514
|
+ // Initial values
|
|
|
|
515
|
+ this._data = new WordArray.init();
|
|
|
|
516
|
+ this._nDataBytes = 0;
|
|
|
|
517
|
+ },
|
|
|
|
518
|
+
|
|
|
|
519
|
+ /**
|
|
|
|
520
|
+ * Adds new data to this block algorithm's buffer.
|
|
|
|
521
|
+ *
|
|
|
|
522
|
+ * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
|
|
|
|
523
|
+ *
|
|
|
|
524
|
+ * @example
|
|
|
|
525
|
+ *
|
|
|
|
526
|
+ * bufferedBlockAlgorithm._append('data');
|
|
|
|
527
|
+ * bufferedBlockAlgorithm._append(wordArray);
|
|
|
|
528
|
+ */
|
|
|
|
529
|
+ _append: function (data) {
|
|
|
|
530
|
+ // Convert string to WordArray, else assume WordArray already
|
|
|
|
531
|
+ if (typeof data == 'string') {
|
|
|
|
532
|
+ data = Utf8.parse(data);
|
|
|
|
533
|
+ }
|
|
|
|
534
|
+
|
|
|
|
535
|
+ // Append
|
|
|
|
536
|
+ this._data.concat(data);
|
|
|
|
537
|
+ this._nDataBytes += data.sigBytes;
|
|
|
|
538
|
+ },
|
|
|
|
539
|
+
|
|
|
|
540
|
+ /**
|
|
|
|
541
|
+ * Processes available data blocks.
|
|
|
|
542
|
+ *
|
|
|
|
543
|
+ * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
|
|
|
|
544
|
+ *
|
|
|
|
545
|
+ * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
|
|
|
|
546
|
+ *
|
|
|
|
547
|
+ * @return {WordArray} The processed data.
|
|
|
|
548
|
+ *
|
|
|
|
549
|
+ * @example
|
|
|
|
550
|
+ *
|
|
|
|
551
|
+ * var processedData = bufferedBlockAlgorithm._process();
|
|
|
|
552
|
+ * var processedData = bufferedBlockAlgorithm._process(!!'flush');
|
|
|
|
553
|
+ */
|
|
|
|
554
|
+ _process: function (doFlush) {
|
|
|
|
555
|
+ // Shortcuts
|
|
|
|
556
|
+ var data = this._data;
|
|
|
|
557
|
+ var dataWords = data.words;
|
|
|
|
558
|
+ var dataSigBytes = data.sigBytes;
|
|
|
|
559
|
+ var blockSize = this.blockSize;
|
|
|
|
560
|
+ var blockSizeBytes = blockSize * 4;
|
|
|
|
561
|
+
|
|
|
|
562
|
+ // Count blocks ready
|
|
|
|
563
|
+ var nBlocksReady = dataSigBytes / blockSizeBytes;
|
|
|
|
564
|
+ if (doFlush) {
|
|
|
|
565
|
+ // Round up to include partial blocks
|
|
|
|
566
|
+ nBlocksReady = Math.ceil(nBlocksReady);
|
|
|
|
567
|
+ } else {
|
|
|
|
568
|
+ // Round down to include only full blocks,
|
|
|
|
569
|
+ // less the number of blocks that must remain in the buffer
|
|
|
|
570
|
+ nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
|
|
|
|
571
|
+ }
|
|
|
|
572
|
+
|
|
|
|
573
|
+ // Count words ready
|
|
|
|
574
|
+ var nWordsReady = nBlocksReady * blockSize;
|
|
|
|
575
|
+
|
|
|
|
576
|
+ // Count bytes ready
|
|
|
|
577
|
+ var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
|
|
|
|
578
|
+
|
|
|
|
579
|
+ // Process blocks
|
|
|
|
580
|
+ if (nWordsReady) {
|
|
|
|
581
|
+ for (var offset = 0; offset < nWordsReady; offset += blockSize) {
|
|
|
|
582
|
+ // Perform concrete-algorithm logic
|
|
|
|
583
|
+ this._doProcessBlock(dataWords, offset);
|
|
|
|
584
|
+ }
|
|
|
|
585
|
+
|
|
|
|
586
|
+ // Remove processed words
|
|
|
|
587
|
+ var processedWords = dataWords.splice(0, nWordsReady);
|
|
|
|
588
|
+ data.sigBytes -= nBytesReady;
|
|
|
|
589
|
+ }
|
|
|
|
590
|
+
|
|
|
|
591
|
+ // Return processed words
|
|
|
|
592
|
+ return new WordArray.init(processedWords, nBytesReady);
|
|
|
|
593
|
+ },
|
|
|
|
594
|
+
|
|
|
|
595
|
+ /**
|
|
|
|
596
|
+ * Creates a copy of this object.
|
|
|
|
597
|
+ *
|
|
|
|
598
|
+ * @return {Object} The clone.
|
|
|
|
599
|
+ *
|
|
|
|
600
|
+ * @example
|
|
|
|
601
|
+ *
|
|
|
|
602
|
+ * var clone = bufferedBlockAlgorithm.clone();
|
|
|
|
603
|
+ */
|
|
|
|
604
|
+ clone: function () {
|
|
|
|
605
|
+ var clone = Base.clone.call(this);
|
|
|
|
606
|
+ clone._data = this._data.clone();
|
|
|
|
607
|
+
|
|
|
|
608
|
+ return clone;
|
|
|
|
609
|
+ },
|
|
|
|
610
|
+
|
|
|
|
611
|
+ _minBufferSize: 0
|
|
|
|
612
|
+ });
|
|
|
|
613
|
+
|
|
|
|
614
|
+ /**
|
|
|
|
615
|
+ * Abstract hasher template.
|
|
|
|
616
|
+ *
|
|
|
|
617
|
+ * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
|
|
|
|
618
|
+ */
|
|
|
|
619
|
+ var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
|
|
|
|
620
|
+ /**
|
|
|
|
621
|
+ * Configuration options.
|
|
|
|
622
|
+ */
|
|
|
|
623
|
+ cfg: Base.extend(),
|
|
|
|
624
|
+
|
|
|
|
625
|
+ /**
|
|
|
|
626
|
+ * Initializes a newly created hasher.
|
|
|
|
627
|
+ *
|
|
|
|
628
|
+ * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
|
|
|
|
629
|
+ *
|
|
|
|
630
|
+ * @example
|
|
|
|
631
|
+ *
|
|
|
|
632
|
+ * var hasher = CryptoJS.algo.SHA256.create();
|
|
|
|
633
|
+ */
|
|
|
|
634
|
+ init: function (cfg) {
|
|
|
|
635
|
+ // Apply config defaults
|
|
|
|
636
|
+ this.cfg = this.cfg.extend(cfg);
|
|
|
|
637
|
+
|
|
|
|
638
|
+ // Set initial values
|
|
|
|
639
|
+ this.reset();
|
|
|
|
640
|
+ },
|
|
|
|
641
|
+
|
|
|
|
642
|
+ /**
|
|
|
|
643
|
+ * Resets this hasher to its initial state.
|
|
|
|
644
|
+ *
|
|
|
|
645
|
+ * @example
|
|
|
|
646
|
+ *
|
|
|
|
647
|
+ * hasher.reset();
|
|
|
|
648
|
+ */
|
|
|
|
649
|
+ reset: function () {
|
|
|
|
650
|
+ // Reset data buffer
|
|
|
|
651
|
+ BufferedBlockAlgorithm.reset.call(this);
|
|
|
|
652
|
+
|
|
|
|
653
|
+ // Perform concrete-hasher logic
|
|
|
|
654
|
+ this._doReset();
|
|
|
|
655
|
+ },
|
|
|
|
656
|
+
|
|
|
|
657
|
+ /**
|
|
|
|
658
|
+ * Updates this hasher with a message.
|
|
|
|
659
|
+ *
|
|
|
|
660
|
+ * @param {WordArray|string} messageUpdate The message to append.
|
|
|
|
661
|
+ *
|
|
|
|
662
|
+ * @return {Hasher} This hasher.
|
|
|
|
663
|
+ *
|
|
|
|
664
|
+ * @example
|
|
|
|
665
|
+ *
|
|
|
|
666
|
+ * hasher.update('message');
|
|
|
|
667
|
+ * hasher.update(wordArray);
|
|
|
|
668
|
+ */
|
|
|
|
669
|
+ update: function (messageUpdate) {
|
|
|
|
670
|
+ // Append
|
|
|
|
671
|
+ this._append(messageUpdate);
|
|
|
|
672
|
+
|
|
|
|
673
|
+ // Update the hash
|
|
|
|
674
|
+ this._process();
|
|
|
|
675
|
+
|
|
|
|
676
|
+ // Chainable
|
|
|
|
677
|
+ return this;
|
|
|
|
678
|
+ },
|
|
|
|
679
|
+
|
|
|
|
680
|
+ /**
|
|
|
|
681
|
+ * Finalizes the hash computation.
|
|
|
|
682
|
+ * Note that the finalize operation is effectively a destructive, read-once operation.
|
|
|
|
683
|
+ *
|
|
|
|
684
|
+ * @param {WordArray|string} messageUpdate (Optional) A final message update.
|
|
|
|
685
|
+ *
|
|
|
|
686
|
+ * @return {WordArray} The hash.
|
|
|
|
687
|
+ *
|
|
|
|
688
|
+ * @example
|
|
|
|
689
|
+ *
|
|
|
|
690
|
+ * var hash = hasher.finalize();
|
|
|
|
691
|
+ * var hash = hasher.finalize('message');
|
|
|
|
692
|
+ * var hash = hasher.finalize(wordArray);
|
|
|
|
693
|
+ */
|
|
|
|
694
|
+ finalize: function (messageUpdate) {
|
|
|
|
695
|
+ // Final message update
|
|
|
|
696
|
+ if (messageUpdate) {
|
|
|
|
697
|
+ this._append(messageUpdate);
|
|
|
|
698
|
+ }
|
|
|
|
699
|
+
|
|
|
|
700
|
+ // Perform concrete-hasher logic
|
|
|
|
701
|
+ var hash = this._doFinalize();
|
|
|
|
702
|
+
|
|
|
|
703
|
+ return hash;
|
|
|
|
704
|
+ },
|
|
|
|
705
|
+
|
|
|
|
706
|
+ blockSize: 512 / 32,
|
|
|
|
707
|
+
|
|
|
|
708
|
+ /**
|
|
|
|
709
|
+ * Creates a shortcut function to a hasher's object interface.
|
|
|
|
710
|
+ *
|
|
|
|
711
|
+ * @param {Hasher} hasher The hasher to create a helper for.
|
|
|
|
712
|
+ *
|
|
|
|
713
|
+ * @return {Function} The shortcut function.
|
|
|
|
714
|
+ *
|
|
|
|
715
|
+ * @static
|
|
|
|
716
|
+ *
|
|
|
|
717
|
+ * @example
|
|
|
|
718
|
+ *
|
|
|
|
719
|
+ * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
|
|
|
|
720
|
+ */
|
|
|
|
721
|
+ _createHelper: function (hasher) {
|
|
|
|
722
|
+ return function (message, cfg) {
|
|
|
|
723
|
+ return new hasher.init(cfg).finalize(message);
|
|
|
|
724
|
+ };
|
|
|
|
725
|
+ },
|
|
|
|
726
|
+
|
|
|
|
727
|
+ /**
|
|
|
|
728
|
+ * Creates a shortcut function to the HMAC's object interface.
|
|
|
|
729
|
+ *
|
|
|
|
730
|
+ * @param {Hasher} hasher The hasher to use in this HMAC helper.
|
|
|
|
731
|
+ *
|
|
|
|
732
|
+ * @return {Function} The shortcut function.
|
|
|
|
733
|
+ *
|
|
|
|
734
|
+ * @static
|
|
|
|
735
|
+ *
|
|
|
|
736
|
+ * @example
|
|
|
|
737
|
+ *
|
|
|
|
738
|
+ * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
|
|
|
|
739
|
+ */
|
|
|
|
740
|
+ _createHmacHelper: function (hasher) {
|
|
|
|
741
|
+ return function (message, key) {
|
|
|
|
742
|
+ return new C_algo.HMAC.init(hasher, key).finalize(message);
|
|
|
|
743
|
+ };
|
|
|
|
744
|
+ }
|
|
|
|
745
|
+ });
|
|
|
|
746
|
+
|
|
|
|
747
|
+ /**
|
|
|
|
748
|
+ * Algorithm namespace.
|
|
|
|
749
|
+ */
|
|
|
|
750
|
+ var C_algo = C.algo = {};
|
|
|
|
751
|
+
|
|
|
|
752
|
+ return C;
|
|
|
|
753
|
+ }(Math));
|
|
|
|
754
|
+
|
|
|
|
755
|
+
|
|
|
|
756
|
+ return CryptoJS;
|
|
|
|
757
|
+
|
|
|
|
758
|
+})); |