      1 //
      2 // NOTE : The original code is wrapped so that the defined functions don't collide with existing ones.
      3 // See http://michaux.ca/articles/javascript-namespacing.
      4 // See http://msdn.microsoft.com/en-us/library/259s7zc1%28v=vs.85%29.aspx
      5 //
      6
      7 /** @namespace */
      8 Crypto = typeof Crypto != 'undefined' ? Crypto : {};
      9 /** @namespace */
     10 Crypto.SHA1 = (function(/*window, undefined, $*/) {
     11
     12 //
     13 // START OF ORIGINAL CODE
     14 //
     15
     16
     17 /*
     18  * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
     19  * in FIPS PUB 180-1
     20  * Version 2.1a Copyright Paul Johnston 2000 - 2002.
     21  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
     22  * Distributed under the BSD License
     23  * See http://pajhome.org.uk/crypt/md5 for details.
     24  */
     25
     26 /*
     27  * Configurable variables. You may need to tweak these to be compatible with
     28  * the server-side, but the defaults work in most cases.
     29  */
     30 var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
     31 var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
     32 var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */
     33
     34 /*
     35  * These are the functions you'll usually want to call
     36  * They take string arguments and return either hex or base-64 encoded strings
     37  */
     38 function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
     39 function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
     40 function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
     41 function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
     42 function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
     43 function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
     44
     45 /*
     46  * Perform a simple self-test to see if the VM is working
     47  */
     48 function sha1_vm_test()
     49 {
     50   return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
     51 }
     52
     53 /*
     54  * Calculate the SHA-1 of an array of big-endian words, and a bit length
     55  */
     56 function core_sha1(x, len)
     57 {
     58   /* append padding */
     59   x[len >> 5] |= 0x80 << (24 - len % 32);
     60   x[((len + 64 >> 9) << 4) + 15] = len;
     61
     62   var w = Array(80);
     63   var a =  1732584193;
     64   var b = -271733879;
     65   var c = -1732584194;
     66   var d =  271733878;
     67   var e = -1009589776;
     68
     69   for(var i = 0; i < x.length; i += 16)
     70   {
     71     var olda = a;
     72     var oldb = b;
     73     var oldc = c;
     74     var oldd = d;
     75     var olde = e;
     76
     77     for(var j = 0; j < 80; j++)
     78     {
     79       if(j < 16) w[j] = x[i + j];
     80       else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
     81       var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
     82                        safe_add(safe_add(e, w[j]), sha1_kt(j)));
     83       e = d;
     84       d = c;
     85       c = rol(b, 30);
     86       b = a;
     87       a = t;
     88     }
     89
     90     a = safe_add(a, olda);
     91     b = safe_add(b, oldb);
     92     c = safe_add(c, oldc);
     93     d = safe_add(d, oldd);
     94     e = safe_add(e, olde);
     95   }
     96   return Array(a, b, c, d, e);
     97
     98 }
     99
    100 /*
    101  * Perform the appropriate triplet combination function for the current
    102  * iteration
    103  */
    104 function sha1_ft(t, b, c, d)
    105 {
    106   if(t < 20) return (b & c) | ((~b) & d);
    107   if(t < 40) return b ^ c ^ d;
    108   if(t < 60) return (b & c) | (b & d) | (c & d);
    109   return b ^ c ^ d;
    110 }
    111
    112 /*
    113  * Determine the appropriate additive constant for the current iteration
    114  */
    115 function sha1_kt(t)
    116 {
    117   return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
    118          (t < 60) ? -1894007588 : -899497514;
    119 }
    120
    121 /*
    122  * Calculate the HMAC-SHA1 of a key and some data
    123  */
    124 function core_hmac_sha1(key, data)
    125 {
    126   var bkey = str2binb(key);
    127   if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
    128
    129   var ipad = Array(16), opad = Array(16);
    130   for(var i = 0; i < 16; i++)
    131   {
    132     ipad[i] = bkey[i] ^ 0x36363636;
    133     opad[i] = bkey[i] ^ 0x5C5C5C5C;
    134   }
    135
    136   var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
    137   return core_sha1(opad.concat(hash), 512 + 160);
    138 }
    139
    140 /*
    141  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
    142  * to work around bugs in some JS interpreters.
    143  */
    144 function safe_add(x, y)
    145 {
    146   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
    147   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    148   return (msw << 16) | (lsw & 0xFFFF);
    149 }
    150
    151 /*
    152  * Bitwise rotate a 32-bit number to the left.
    153  */
    154 function rol(num, cnt)
    155 {
    156   return (num << cnt) | (num >>> (32 - cnt));
    157 }
    158
    159 /*
    160  * Convert an 8-bit or 16-bit string to an array of big-endian words
    161  * In 8-bit function, characters >255 have their hi-byte silently ignored.
    162  */
    163 function str2binb(str)
    164 {
    165   var bin = Array();
    166   var mask = (1 << chrsz) - 1;
    167   for(var i = 0; i < str.length * chrsz; i += chrsz)
    168     bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
    169   return bin;
    170 }
    171
    172 /*
    173  * Convert an array of big-endian words to a string
    174  */
    175 function binb2str(bin)
    176 {
    177   var str = "";
    178   var mask = (1 << chrsz) - 1;
    179   for(var i = 0; i < bin.length * 32; i += chrsz)
    180     str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
    181   return str;
    182 }
    183
    184 /*
    185  * Convert an array of big-endian words to a hex string.
    186  */
    187 function binb2hex(binarray)
    188 {
    189   var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
    190   var str = "";
    191   for(var i = 0; i < binarray.length * 4; i++)
    192   {
    193     str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
    194            hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
    195   }
    196   return str;
    197 }
    198
    199 /*
    200  * Convert an array of big-endian words to a base-64 string
    201  */
    202 function binb2b64(binarray)
    203 {
    204   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    205   var str = "";
    206   for(var i = 0; i < binarray.length * 4; i += 3)
    207   {
    208     var triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
    209                 | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
    210                 |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
    211     for(var j = 0; j < 4; j++)
    212     {
    213       if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
    214       else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    215     }
    216   }
    217   return str;
    218 }
    219
    220 //
    221 // END OF ORIGINAL CODE
    222 //
    223
    224 return {
    225     hex_sha:hex_sha1,
    226     b64_sha1:b64_sha1,
    227     str_sha1:str_sha1,
    228     hex_hmac_sha1:hex_hmac_sha1,
    229     b64_hmac_sha1:b64_hmac_sha1,
    230     str_hmac_sha1:str_hmac_sha1
    231 };
    232
    233 })(/*window, undefined, $*/);