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. (function () {
  8.     // Shortcuts
  9.     var C = CryptoJS;
  10.     var C_lib = C.lib;
  11.     var Base = C_lib.Base;
  12.     var C_enc = C.enc;
  13.     var Utf8 = C_enc.Utf8;
  14.     var C_algo = C.algo;
  15.  
  16.     /**
  17.      * HMAC algorithm.
  18.      */
  19.     var HMAC = C_algo.HMAC = Base.extend({
  20.         /**
  21.          * Initializes a newly created HMAC.
  22.          *
  23.          * @param {Hasher} hasher The hash algorithm to use.
  24.          * @param {WordArray|string} key The secret key.
  25.          *
  26.          * @example
  27.          *
  28.          *     var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
  29.          */
  30.         init: function (hasher, key) {
  31.             // Init hasher
  32.             hasher = this._hasher = new hasher.init();
  33.  
  34.             // Convert string to WordArray, else assume WordArray already
  35.             if (typeof key == 'string') {
  36.                 key = Utf8.parse(key);
  37.             }
  38.  
  39.             // Shortcuts
  40.             var hasherBlockSize = hasher.blockSize;
  41.             var hasherBlockSizeBytes = hasherBlockSize * 4;
  42.  
  43.             // Allow arbitrary length keys
  44.             if (key.sigBytes > hasherBlockSizeBytes) {
  45.                 key = hasher.finalize(key);
  46.             }
  47.  
  48.             // Clamp excess bits
  49.             key.clamp();
  50.  
  51.             // Clone key for inner and outer pads
  52.             var oKey = this._oKey = key.clone();
  53.             var iKey = this._iKey = key.clone();
  54.  
  55.             // Shortcuts
  56.             var oKeyWords = oKey.words;
  57.             var iKeyWords = iKey.words;
  58.  
  59.             // XOR keys with pad constants
  60.             for (var i = 0; i < hasherBlockSize; i++) {
  61.                 oKeyWords[i] ^= 0x5c5c5c5c;
  62.                 iKeyWords[i] ^= 0x36363636;
  63.             }
  64.             oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
  65.  
  66.             // Set initial values
  67.             this.reset();
  68.         },
  69.  
  70.         /**
  71.          * Resets this HMAC to its initial state.
  72.          *
  73.          * @example
  74.          *
  75.          *     hmacHasher.reset();
  76.          */
  77.         reset: function () {
  78.             // Shortcut
  79.             var hasher = this._hasher;
  80.  
  81.             // Reset
  82.             hasher.reset();
  83.             hasher.update(this._iKey);
  84.         },
  85.  
  86.         /**
  87.          * Updates this HMAC with a message.
  88.          *
  89.          * @param {WordArray|string} messageUpdate The message to append.
  90.          *
  91.          * @return {HMAC} This HMAC instance.
  92.          *
  93.          * @example
  94.          *
  95.          *     hmacHasher.update('message');
  96.          *     hmacHasher.update(wordArray);
  97.          */
  98.         update: function (messageUpdate) {
  99.             this._hasher.update(messageUpdate);
  100.  
  101.             // Chainable
  102.             return this;
  103.         },
  104.  
  105.         /**
  106.          * Finalizes the HMAC computation.
  107.          * Note that the finalize operation is effectively a destructive, read-once operation.
  108.          *
  109.          * @param {WordArray|string} messageUpdate (Optional) A final message update.
  110.          *
  111.          * @return {WordArray} The HMAC.
  112.          *
  113.          * @example
  114.          *
  115.          *     var hmac = hmacHasher.finalize();
  116.          *     var hmac = hmacHasher.finalize('message');
  117.          *     var hmac = hmacHasher.finalize(wordArray);
  118.          */
  119.         finalize: function (messageUpdate) {
  120.             // Shortcut
  121.             var hasher = this._hasher;
  122.  
  123.             // Compute HMAC
  124.             var innerHash = hasher.finalize(messageUpdate);
  125.             hasher.reset();
  126.             var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
  127.  
  128.             return hmac;
  129.         }
  130.     });
  131. }());
  132.