home *** CD-ROM | disk | FTP | other *** search
/ Design Your Dream Home / H&RCD2004.ISO / sha1.js < prev    next >
Text File  |  2004-04-23  |  3KB  |  122 lines

  1. /*
  2.  * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  3.  * in FIPS PUB 180-1
  4.  * Copyright (C) Paul Johnston 2000.
  5.  * See http://pajhome.org.uk/site/legal.html for details.
  6.  */
  7.  
  8. /*
  9.  * Convert a 32-bit number to a hex string with ms-byte first
  10.  */
  11. var hex_chr = "0123456789abcdef";
  12. function hex(num)
  13. {
  14.   var str = "";
  15.   for(var j = 7; j >= 0; j--)
  16.     str += hex_chr.charAt((num >> (j * 4)) & 0x0F);
  17.   return str;
  18. }
  19.  
  20. /*
  21.  * Convert a string to a sequence of 16-word blocks, stored as an array.
  22.  * Append padding bits and the length, as described in the SHA1 standard.
  23.  */
  24. function str2blks_SHA1(str)
  25. {
  26.   var nblk = ((str.length + 8) >> 6) + 1;
  27.   var blks = new Array(nblk * 16);
  28.   for(var i = 0; i < nblk * 16; i++) blks[i] = 0;
  29.   for(i = 0; i < str.length; i++)
  30.     blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
  31.   blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
  32.   blks[nblk * 16 - 1] = str.length * 8;
  33.   return blks;
  34. }
  35.  
  36. /*
  37.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  38.  * to work around bugs in some JS interpreters.
  39.  */
  40. function add(x, y)
  41. {
  42.   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  43.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  44.   return (msw << 16) | (lsw & 0xFFFF);
  45. }
  46.  
  47. /*
  48.  * Bitwise rotate a 32-bit number to the left
  49.  */
  50. function rol(num, cnt)
  51. {
  52.   return (num << cnt) | (num >>> (32 - cnt));
  53. }
  54.  
  55. /*
  56.  * Perform the appropriate triplet combination function for the current
  57.  * iteration
  58.  */
  59. function ft(t, b, c, d)
  60. {
  61.   if(t < 20) return (b & c) | ((~b) & d);
  62.   if(t < 40) return b ^ c ^ d;
  63.   if(t < 60) return (b & c) | (b & d) | (c & d);
  64.   return b ^ c ^ d;
  65. }
  66.  
  67. /*
  68.  * Determine the appropriate additive constant for the current iteration
  69.  */
  70. function kt(t)
  71. {
  72.   return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
  73.          (t < 60) ? -1894007588 : -899497514;
  74. }
  75.  
  76. /*
  77.  * Take a string and return the hex representation of its SHA-1.
  78.  */
  79. function calcSHA1(str)
  80. {
  81.   var x = str2blks_SHA1(str);
  82.   var w = new Array(80);
  83.  
  84.   var a =  1732584193;
  85.   var b = -271733879;
  86.   var c = -1732584194;
  87.   var d =  271733878;
  88.   var e = -1009589776;
  89.  
  90.   for(var i = 0; i < x.length; i += 16)
  91.   {
  92.     var olda = a;
  93.     var oldb = b;
  94.     var oldc = c;
  95.     var oldd = d;
  96.     var olde = e;
  97.  
  98.     for(var j = 0; j < 80; j++)
  99.     {
  100.       if(j < 16) w[j] = x[i + j];
  101.       else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  102.       t = add(add(rol(a, 5), ft(j, b, c, d)), add(add(e, w[j]), kt(j)));
  103.       e = d;
  104.       d = c;
  105.       c = rol(b, 30);
  106.       b = a;
  107.       a = t;
  108.     }
  109.  
  110.     a = add(a, olda);
  111.     b = add(b, oldb);
  112.     c = add(c, oldc);
  113.     d = add(d, oldd);
  114.     e = add(e, olde);
  115.   }
  116.   return hex(a) + hex(b) + hex(c) + hex(d) + hex(e);
  117. }
  118.  
  119. // Created with XBuilder (2.5.3727.1), Copyright (c) 1997-1999 Berry International, Inc. All Rights Reserved 
  120. // Page: http://hrcd2004.icowip.co.uk/sha1.js 
  121. // Date: 05/05/04 08:31 PM 
  122.