[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/rsc/js/ -> sha1.js (source)

   1  /*


   2   * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined


   3   * in FIPS 180-1


   4   * Version 2.2-alpha Copyright Paul Johnston 2000 - 2002.


   5   * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet


   6   * Distributed under the BSD License


   7   * See http://pajhome.org.uk/crypt/md5 for details.


   8   */

   9  

  10  /*


  11   * Configurable variables. You may need to tweak these to be compatible with


  12   * the server-side, but the defaults work in most cases.


  13   */

  14  var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */

  15  var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */

  16  

  17  /*


  18   * These are the functions you'll usually want to call


  19   * They take string arguments and return either hex or base-64 encoded strings


  20   */

  21  function hex_sha1(s)    { return rstr2hex(rstr_sha1(str2rstr_utf8(s))); }

  22  function b64_sha1(s)    { return rstr2b64(rstr_sha1(str2rstr_utf8(s))); }

  23  function any_sha1(s, e) { return rstr2any(rstr_sha1(str2rstr_utf8(s)), e); }

  24  function hex_hmac_sha1(k, d)

  25    { return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }

  26  function b64_hmac_sha1(k, d)

  27    { return rstr2b64(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }

  28  function any_hmac_sha1(k, d, e)

  29    { return rstr2any(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d)), e); }

  30  

  31  /*


  32   * Perform a simple self-test to see if the VM is working


  33   */

  34  function sha1_vm_test()

  35  {

  36    return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";

  37  }

  38  

  39  /*


  40   * Calculate the SHA1 of a raw string


  41   */

  42  function rstr_sha1(s)

  43  {

  44    return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8));

  45  }

  46  

  47  /*


  48   * Calculate the HMAC-SHA1 of a key and some data (raw strings)


  49   */

  50  function rstr_hmac_sha1(key, data)

  51  {

  52    var bkey = rstr2binb(key);

  53    if(bkey.length > 16) bkey = binb_sha1(bkey, key.length * 8);

  54  

  55    var ipad = Array(16), opad = Array(16);

  56    for(var i = 0; i < 16; i++)

  57    {

  58      ipad[i] = bkey[i] ^ 0x36363636;

  59      opad[i] = bkey[i] ^ 0x5C5C5C5C;

  60    }

  61  

  62    var hash = binb_sha1(ipad.concat(rstr2binb(data)), 512 + data.length * 8);

  63    return binb2rstr(binb_sha1(opad.concat(hash), 512 + 160));

  64  }

  65  

  66  /*


  67   * Convert a raw string to a hex string


  68   */

  69  function rstr2hex(input)

  70  {

  71    var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";

  72    var output = "";

  73    var x;

  74    for(var i = 0; i < input.length; i++)

  75    {

  76      x = input.charCodeAt(i);

  77      output += hex_tab.charAt((x >>> 4) & 0x0F)

  78             +  hex_tab.charAt( x        & 0x0F);

  79    }

  80    return output;

  81  }

  82  

  83  /*


  84   * Convert a raw string to a base-64 string


  85   */

  86  function rstr2b64(input)

  87  {

  88    var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  89    var output = "";

  90    var len = input.length;

  91    for(var i = 0; i < len; i += 3)

  92    {

  93      var triplet = (input.charCodeAt(i) << 16)

  94                  | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)

  95                  | (i + 2 < len ? input.charCodeAt(i+2)      : 0);

  96      for(var j = 0; j < 4; j++)

  97      {

  98        if(i * 8 + j * 6 > input.length * 8) output += b64pad;

  99        else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);

 100      }

 101    }

 102    return output;

 103  }

 104  

 105  /*


 106   * Convert a raw string to an arbitrary string encoding


 107   */

 108  function rstr2any(input, encoding)

 109  {

 110    var divisor = encoding.length;

 111    var remainders = Array();

 112    var i, q, x, quotient;

 113  

 114    /* Convert to an array of 16-bit big-endian values, forming the dividend */


 115    var dividend = Array(Math.ceil(input.length / 2));

 116    for(i = 0; i < dividend.length; i++)

 117    {

 118      dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);

 119    }

 120  

 121    /*


 122     * Repeatedly perform a long division. The binary array forms the dividend,


 123     * the length of the encoding is the divisor. Once computed, the quotient


 124     * forms the dividend for the next step. We stop when the dividend is zero.


 125     * All remainders are stored for later use.


 126     */

 127    while(dividend.length > 0)

 128    {

 129      quotient = Array();

 130      x = 0;

 131      for(i = 0; i < dividend.length; i++)

 132      {

 133        x = (x << 16) + dividend[i];

 134        q = Math.floor(x / divisor);

 135        x -= q * divisor;

 136        if(quotient.length > 0 || q > 0)

 137          quotient[quotient.length] = q;

 138      }

 139      remainders[remainders.length] = x;

 140      dividend = quotient;

 141    }

 142  

 143    /* Convert the remainders to the output string */


 144    var output = "";

 145    for(i = remainders.length - 1; i >= 0; i--)

 146      output += encoding.charAt(remainders[i]);

 147  

 148    /* Append leading zero equivalents */


 149    var full_length = Math.ceil(input.length * 8 /

 150                                      (Math.log(encoding.length) / Math.log(2)))

 151    for(i = output.length; i < full_length; i++)

 152      output = encoding[0] + output;

 153  

 154    return output;

 155  }

 156  

 157  /*


 158   * Encode a string as utf-8.


 159   * For efficiency, this assumes the input is valid utf-16.


 160   */

 161  function str2rstr_utf8(input)

 162  {

 163    var output = "";

 164    var i = -1;

 165    var x, y;

 166  

 167    while(++i < input.length)

 168    {

 169      /* Decode utf-16 surrogate pairs */


 170      x = input.charCodeAt(i);

 171      y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;

 172      if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)

 173      {

 174        x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);

 175        i++;

 176      }

 177  

 178      /* Encode output as utf-8 */


 179      if(x <= 0x7F)

 180        output += String.fromCharCode(x);

 181      else if(x <= 0x7FF)

 182        output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),

 183                                      0x80 | ( x         & 0x3F));

 184      else if(x <= 0xFFFF)

 185        output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),

 186                                      0x80 | ((x >>> 6 ) & 0x3F),

 187                                      0x80 | ( x         & 0x3F));

 188      else if(x <= 0x1FFFFF)

 189        output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),

 190                                      0x80 | ((x >>> 12) & 0x3F),

 191                                      0x80 | ((x >>> 6 ) & 0x3F),

 192                                      0x80 | ( x         & 0x3F));

 193    }

 194    return output;

 195  }

 196  

 197  /*


 198   * Encode a string as utf-16


 199   */

 200  function str2rstr_utf16le(input)

 201  {

 202    var output = "";

 203    for(var i = 0; i < input.length; i++)

 204      output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,

 205                                    (input.charCodeAt(i) >>> 8) & 0xFF);

 206    return output;

 207  }

 208  

 209  function str2rstr_utf16be(input)

 210  {

 211    var output = "";

 212    for(var i = 0; i < input.length; i++)

 213      output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,

 214                                     input.charCodeAt(i)        & 0xFF);

 215    return output;

 216  }

 217  

 218  /*


 219   * Convert a raw string to an array of big-endian words


 220   * Characters >255 have their high-byte silently ignored.


 221   */

 222  function rstr2binb(input)

 223  {

 224    var output = Array(input.length >> 2);

 225    for(var i = 0; i < output.length; i++)

 226      output[i] = 0;

 227    for(var i = 0; i < input.length * 8; i += 8)

 228      output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);

 229    return output;

 230  }

 231  

 232  /*


 233   * Convert an array of little-endian words to a string


 234   */

 235  function binb2rstr(input)

 236  {

 237    var output = "";

 238    for(var i = 0; i < input.length * 32; i += 8)

 239      output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);

 240    return output;

 241  }

 242  

 243  /*


 244   * Calculate the SHA-1 of an array of big-endian words, and a bit length


 245   */

 246  function binb_sha1(x, len)

 247  {

 248    /* append padding */


 249    x[len >> 5] |= 0x80 << (24 - len % 32);

 250    x[((len + 64 >> 9) << 4) + 15] = len;

 251  

 252    var w = Array(80);

 253    var a =  1732584193;

 254    var b = -271733879;

 255    var c = -1732584194;

 256    var d =  271733878;

 257    var e = -1009589776;

 258  

 259    for(var i = 0; i < x.length; i += 16)

 260    {

 261      var olda = a;

 262      var oldb = b;

 263      var oldc = c;

 264      var oldd = d;

 265      var olde = e;

 266  

 267      for(var j = 0; j < 80; j++)

 268      {

 269        if(j < 16) w[j] = x[i + j];

 270        else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);

 271        var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),

 272                         safe_add(safe_add(e, w[j]), sha1_kt(j)));

 273        e = d;

 274        d = c;

 275        c = bit_rol(b, 30);

 276        b = a;

 277        a = t;

 278      }

 279  

 280      a = safe_add(a, olda);

 281      b = safe_add(b, oldb);

 282      c = safe_add(c, oldc);

 283      d = safe_add(d, oldd);

 284      e = safe_add(e, olde);

 285    }

 286    return Array(a, b, c, d, e);

 287  

 288  }

 289  

 290  /*


 291   * Perform the appropriate triplet combination function for the current


 292   * iteration


 293   */

 294  function sha1_ft(t, b, c, d)

 295  {

 296    if(t < 20) return (b & c) | ((~b) & d);

 297    if(t < 40) return b ^ c ^ d;

 298    if(t < 60) return (b & c) | (b & d) | (c & d);

 299    return b ^ c ^ d;

 300  }

 301  

 302  /*


 303   * Determine the appropriate additive constant for the current iteration


 304   */

 305  function sha1_kt(t)

 306  {

 307    return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :

 308           (t < 60) ? -1894007588 : -899497514;

 309  }

 310  

 311  /*


 312   * Add integers, wrapping at 2^32. This uses 16-bit operations internally


 313   * to work around bugs in some JS interpreters.


 314   */

 315  function safe_add(x, y)

 316  {

 317    var lsw = (x & 0xFFFF) + (y & 0xFFFF);

 318    var msw = (x >> 16) + (y >> 16) + (lsw >> 16);

 319    return (msw << 16) | (lsw & 0xFFFF);

 320  }

 321  

 322  /*


 323   * Bitwise rotate a 32-bit number to the left.


 324   */

 325  function bit_rol(num, cnt)

 326  {

 327    return (num << cnt) | (num >>> (32 - cnt));

 328  }



Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics