Subversion Repositories cryptochat

Rev

View as "text/javascript" | Blame | Last modification | View Log | RSS feed

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