~bcsaller/juju-gui/exportXY

« back to all changes in this revision

Viewing changes to lib/cryptojs/components/core.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-11 16:32:03 UTC
  • Revision ID: kapil.foss@gmail.com-20120711163203-nsuoy9r0p48az7mu
commit wip

Show diffs side-by-side

added added

removed removed

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