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 (Math) {
  8.     // Shortcuts
  9.     var C = CryptoJS;
  10.     var C_lib = C.lib;
  11.     var WordArray = C_lib.WordArray;
  12.     var Hasher = C_lib.Hasher;
  13.     var C_algo = C.algo;
  14.  
  15.     // Initialization and round constants tables
  16.     var H = [];
  17.     var K = [];
  18.  
  19.     // Compute constants
  20.     (function () {
  21.         function isPrime(n) {
  22.             var sqrtN = Math.sqrt(n);
  23.             for (var factor = 2; factor <= sqrtN; factor++) {
  24.                 if (!(n % factor)) {
  25.                     return false;
  26.                 }
  27.             }
  28.  
  29.             return true;
  30.         }
  31.  
  32.         function getFractionalBits(n) {
  33.             return ((n - (n | 0)) * 0x100000000) | 0;
  34.         }
  35.  
  36.         var n = 2;
  37.         var nPrime = 0;
  38.         while (nPrime < 64) {
  39.             if (isPrime(n)) {
  40.                 if (nPrime < 8) {
  41.                     H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
  42.                 }
  43.                 K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
  44.  
  45.                 nPrime++;
  46.             }
  47.  
  48.             n++;
  49.         }
  50.     }());
  51.  
  52.     // Reusable object
  53.     var W = [];
  54.  
  55.     /**
  56.      * SHA-256 hash algorithm.
  57.      */
  58.     var SHA256 = C_algo.SHA256 = Hasher.extend({
  59.         _doReset: function () {
  60.             this._hash = new WordArray.init(H.slice(0));
  61.         },
  62.  
  63.         _doProcessBlock: function (M, offset) {
  64.             // Shortcut
  65.             var H = this._hash.words;
  66.  
  67.             // Working variables
  68.             var a = H[0];
  69.             var b = H[1];
  70.             var c = H[2];
  71.             var d = H[3];
  72.             var e = H[4];
  73.             var f = H[5];
  74.             var g = H[6];
  75.             var h = H[7];
  76.  
  77.             // Computation
  78.             for (var i = 0; i < 64; i++) {
  79.                 if (i < 16) {
  80.                     W[i] = M[offset + i] | 0;
  81.                 } else {
  82.                     var gamma0x = W[i - 15];
  83.                     var gamma0  = ((gamma0x << 25) | (gamma0x >>> 7))  ^
  84.                                   ((gamma0x << 14) | (gamma0x >>> 18)) ^
  85.                                    (gamma0x >>> 3);
  86.  
  87.                     var gamma1x = W[i - 2];
  88.                     var gamma1  = ((gamma1x << 15) | (gamma1x >>> 17)) ^
  89.                                   ((gamma1x << 13) | (gamma1x >>> 19)) ^
  90.                                    (gamma1x >>> 10);
  91.  
  92.                     W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
  93.                 }
  94.  
  95.                 var ch  = (e & f) ^ (~e & g);
  96.                 var maj = (a & b) ^ (a & c) ^ (b & c);
  97.  
  98.                 var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
  99.                 var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7)  | (e >>> 25));
  100.  
  101.                 var t1 = h + sigma1 + ch + K[i] + W[i];
  102.                 var t2 = sigma0 + maj;
  103.  
  104.                 h = g;
  105.                 g = f;
  106.                 f = e;
  107.                 e = (d + t1) | 0;
  108.                 d = c;
  109.                 c = b;
  110.                 b = a;
  111.                 a = (t1 + t2) | 0;
  112.             }
  113.  
  114.             // Intermediate hash value
  115.             H[0] = (H[0] + a) | 0;
  116.             H[1] = (H[1] + b) | 0;
  117.             H[2] = (H[2] + c) | 0;
  118.             H[3] = (H[3] + d) | 0;
  119.             H[4] = (H[4] + e) | 0;
  120.             H[5] = (H[5] + f) | 0;
  121.             H[6] = (H[6] + g) | 0;
  122.             H[7] = (H[7] + h) | 0;
  123.         },
  124.  
  125.         _doFinalize: function () {
  126.             // Shortcuts
  127.             var data = this._data;
  128.             var dataWords = data.words;
  129.  
  130.             var nBitsTotal = this._nDataBytes * 8;
  131.             var nBitsLeft = data.sigBytes * 8;
  132.  
  133.             // Add padding
  134.             dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  135.             dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
  136.             dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
  137.             data.sigBytes = dataWords.length * 4;
  138.  
  139.             // Hash final blocks
  140.             this._process();
  141.  
  142.             // Return final computed hash
  143.             return this._hash;
  144.         },
  145.  
  146.         clone: function () {
  147.             var clone = Hasher.clone.call(this);
  148.             clone._hash = this._hash.clone();
  149.  
  150.             return clone;
  151.         }
  152.     });
  153.  
  154.     /**
  155.      * Shortcut function to the hasher's object interface.
  156.      *
  157.      * @param {WordArray|string} message The message to hash.
  158.      *
  159.      * @return {WordArray} The hash.
  160.      *
  161.      * @static
  162.      *
  163.      * @example
  164.      *
  165.      *     var hash = CryptoJS.SHA256('message');
  166.      *     var hash = CryptoJS.SHA256(wordArray);
  167.      */
  168.     C.SHA256 = Hasher._createHelper(SHA256);
  169.  
  170.     /**
  171.      * Shortcut function to the HMAC's object interface.
  172.      *
  173.      * @param {WordArray|string} message The message to hash.
  174.      * @param {WordArray|string} key The secret key.
  175.      *
  176.      * @return {WordArray} The HMAC.
  177.      *
  178.      * @static
  179.      *
  180.      * @example
  181.      *
  182.      *     var hmac = CryptoJS.HmacSHA256(message, key);
  183.      */
  184.     C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
  185. }(Math));
  186.