JavaScript SHA1 encryption algorithm implementation detailed code

From , 3 Years ago, written in JavaScript, viewed 97 times.
URL https://pastebin.vip/view/51d1cd3a
  1. /*
  2.  * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  3.  * in FIPS 180-1
  4.  * Version 2.2 Copyright Paul Johnston 2000 - 2009.
  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.  * http://www.sharejs.com
  9.  */
  10.  
  11. /*
  12.  * Configurable variables. You may need to tweak these to be compatible with
  13.  * the server-side, but the defaults work in most cases.
  14.  */
  15. var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
  16. var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
  17.  
  18. /*
  19.  * These are the functions you'll usually want to call
  20.  * They take string arguments and return either hex or base-64 encoded strings
  21.  */
  22. function hex_sha1(s)    { return rstr2hex(rstr_sha1(str2rstr_utf8(s))); }
  23. function b64_sha1(s)    { return rstr2b64(rstr_sha1(str2rstr_utf8(s))); }
  24. function any_sha1(s, e) { return rstr2any(rstr_sha1(str2rstr_utf8(s)), e); }
  25. function hex_hmac_sha1(k, d)
  26.   { return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
  27. function b64_hmac_sha1(k, d)
  28.   { return rstr2b64(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
  29. function any_hmac_sha1(k, d, e)
  30.   { return rstr2any(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
  31.  
  32. /*
  33.  * Perform a simple self-test to see if the VM is working
  34.  */
  35. function sha1_vm_test()
  36. {
  37.   return hex_sha1("abc").toLowerCase() == "a9993e364706816aba3e25717850c26c9cd0d89d";
  38. }
  39.  
  40. /*
  41.  * Calculate the SHA1 of a raw string
  42.  */
  43. function rstr_sha1(s)
  44. {
  45.   return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8));
  46. }
  47.  
  48. /*
  49.  * Calculate the HMAC-SHA1 of a key and some data (raw strings)
  50.  */
  51. function rstr_hmac_sha1(key, data)
  52. {
  53.   var bkey = rstr2binb(key);
  54.   if(bkey.length > 16) bkey = binb_sha1(bkey, key.length * 8);
  55.  
  56.   var ipad = Array(16), opad = Array(16);
  57.   for(var i = 0; i < 16; i++)
  58.   {
  59.     ipad[i] = bkey[i] ^ 0x36363636;
  60.     opad[i] = bkey[i] ^ 0x5C5C5C5C;
  61.   }
  62.  
  63.   var hash = binb_sha1(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
  64.   return binb2rstr(binb_sha1(opad.concat(hash), 512 + 160));
  65. }
  66.  
  67. /*
  68.  * Convert a raw string to a hex string
  69.  */
  70. function rstr2hex(input)
  71. {
  72.   try { hexcase } catch(e) { hexcase=0; }
  73.   var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  74.   var output = "";
  75.   var x;
  76.   for(var i = 0; i < input.length; i++)
  77.   {
  78.     x = input.charCodeAt(i);
  79.     output += hex_tab.charAt((x >>> 4) & 0x0F)
  80.            +  hex_tab.charAt( x        & 0x0F);
  81.   }
  82.   return output;
  83. }
  84.  
  85. /*
  86.  * Convert a raw string to a base-64 string
  87.  */
  88. function rstr2b64(input)
  89. {
  90.   try { b64pad } catch(e) { b64pad=''; }
  91.   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  92.   var output = "";
  93.   var len = input.length;
  94.   for(var i = 0; i < len; i += 3)
  95.   {
  96.     var triplet = (input.charCodeAt(i) << 16)
  97.                 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  98.                 | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
  99.     for(var j = 0; j < 4; j++)
  100.     {
  101.       if(i * 8 + j * 6 > input.length * 8) output += b64pad;
  102.       else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  103.     }
  104.   }
  105.   return output;
  106. }
  107.  
  108. /*
  109.  * Convert a raw string to an arbitrary string encoding
  110.  */
  111. function rstr2any(input, encoding)
  112. {
  113.   var divisor = encoding.length;
  114.   var remainders = Array();
  115.   var i, q, x, quotient;
  116.  
  117.   /* Convert to an array of 16-bit big-endian values, forming the dividend */
  118.   var dividend = Array(Math.ceil(input.length / 2));
  119.   for(i = 0; i < dividend.length; i++)
  120.   {
  121.     dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  122.   }
  123.  
  124.   /*
  125.    * Repeatedly perform a long division. The binary array forms the dividend,
  126.    * the length of the encoding is the divisor. Once computed, the quotient
  127.    * forms the dividend for the next step. We stop when the dividend is zero.
  128.    * All remainders are stored for later use.
  129.    */
  130.   while(dividend.length > 0)
  131.   {
  132.     quotient = Array();
  133.     x = 0;
  134.     for(i = 0; i < dividend.length; i++)
  135.     {
  136.       x = (x << 16) + dividend[i];
  137.       q = Math.floor(x / divisor);
  138.       x -= q * divisor;
  139.       if(quotient.length > 0 || q > 0)
  140.         quotient[quotient.length] = q;
  141.     }
  142.     remainders[remainders.length] = x;
  143.     dividend = quotient;
  144.   }
  145.  
  146.   /* Convert the remainders to the output string */
  147.   var output = "";
  148.   for(i = remainders.length - 1; i >= 0; i--)
  149.     output += encoding.charAt(remainders[i]);
  150.  
  151.   /* Append leading zero equivalents */
  152.   var full_length = Math.ceil(input.length * 8 /
  153.                                     (Math.log(encoding.length) / Math.log(2)))
  154.   for(i = output.length; i < full_length; i++)
  155.     output = encoding[0] + output;
  156.  
  157.   return output;
  158. }
  159.  
  160. /*
  161.  * Encode a string as utf-8.
  162.  * For efficiency, this assumes the input is valid utf-16.
  163.  */
  164. function str2rstr_utf8(input)
  165. {
  166.   var output = "";
  167.   var i = -1;
  168.   var x, y;
  169.  
  170.   while(++i < input.length)
  171.   {
  172.     /* Decode utf-16 surrogate pairs */
  173.     x = input.charCodeAt(i);
  174.     y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  175.     if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
  176.     {
  177.       x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  178.       i++;
  179.     }
  180.  
  181.     /* Encode output as utf-8 */
  182.     if(x <= 0x7F)
  183.       output += String.fromCharCode(x);
  184.     else if(x <= 0x7FF)
  185.       output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  186.                                     0x80 | ( x         & 0x3F));
  187.     else if(x <= 0xFFFF)
  188.       output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  189.                                     0x80 | ((x >>> 6 ) & 0x3F),
  190.                                     0x80 | ( x         & 0x3F));
  191.     else if(x <= 0x1FFFFF)
  192.       output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  193.                                     0x80 | ((x >>> 12) & 0x3F),
  194.                                     0x80 | ((x >>> 6 ) & 0x3F),
  195.                                     0x80 | ( x         & 0x3F));
  196.   }
  197.   return output;
  198. }
  199.  
  200. /*
  201.  * Encode a string as utf-16
  202.  */
  203. function str2rstr_utf16le(input)
  204. {
  205.   var output = "";
  206.   for(var i = 0; i < input.length; i++)
  207.     output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,
  208.                                   (input.charCodeAt(i) >>> 8) & 0xFF);
  209.   return output;
  210. }
  211.  
  212. function str2rstr_utf16be(input)
  213. {
  214.   var output = "";
  215.   for(var i = 0; i < input.length; i++)
  216.     output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  217.                                    input.charCodeAt(i)        & 0xFF);
  218.   return output;
  219. }
  220.  
  221. /*
  222.  * Convert a raw string to an array of big-endian words
  223.  * Characters >255 have their high-byte silently ignored.
  224.  */
  225. function rstr2binb(input)
  226. {
  227.   var output = Array(input.length >> 2);
  228.   for(var i = 0; i < output.length; i++)
  229.     output[i] = 0;
  230.   for(var i = 0; i < input.length * 8; i += 8)
  231.     output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
  232.   return output;
  233. }
  234.  
  235. /*
  236.  * Convert an array of big-endian words to a string
  237.  */
  238. function binb2rstr(input)
  239. {
  240.   var output = "";
  241.   for(var i = 0; i < input.length * 32; i += 8)
  242.     output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
  243.   return output;
  244. }
  245.  
  246. /*
  247.  * Calculate the SHA-1 of an array of big-endian words, and a bit length
  248.  */
  249. function binb_sha1(x, len)
  250. {
  251.   /* append padding */
  252.   x[len >> 5] |= 0x80 << (24 - len % 32);
  253.   x[((len + 64 >> 9) << 4) + 15] = len;
  254.  
  255.   var w = Array(80);
  256.   var a =  1732584193;
  257.   var b = -271733879;
  258.   var c = -1732584194;
  259.   var d =  271733878;
  260.   var e = -1009589776;
  261.  
  262.   for(var i = 0; i < x.length; i += 16)
  263.   {
  264.     var olda = a;
  265.     var oldb = b;
  266.     var oldc = c;
  267.     var oldd = d;
  268.     var olde = e;
  269.  
  270.     for(var j = 0; j < 80; j++)
  271.     {
  272.       if(j < 16) w[j] = x[i + j];
  273.       else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  274.       var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
  275.                        safe_add(safe_add(e, w[j]), sha1_kt(j)));
  276.       e = d;
  277.       d = c;
  278.       c = bit_rol(b, 30);
  279.       b = a;
  280.       a = t;
  281.     }
  282.  
  283.     a = safe_add(a, olda);
  284.     b = safe_add(b, oldb);
  285.     c = safe_add(c, oldc);
  286.     d = safe_add(d, oldd);
  287.     e = safe_add(e, olde);
  288.   }
  289.   return Array(a, b, c, d, e);
  290.  
  291. }
  292.  
  293. /*
  294.  * Perform the appropriate triplet combination function for the current
  295.  * iteration
  296.  */
  297. function sha1_ft(t, b, c, d)
  298. {
  299.   if(t < 20) return (b & c) | ((~b) & d);
  300.   if(t < 40) return b ^ c ^ d;
  301.   if(t < 60) return (b & c) | (b & d) | (c & d);
  302.   return b ^ c ^ d;
  303. }
  304.  
  305. /*
  306.  * Determine the appropriate additive constant for the current iteration
  307.  */
  308. function sha1_kt(t)
  309. {
  310.   return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
  311.          (t < 60) ? -1894007588 : -899497514;
  312. }
  313.  
  314. /*
  315.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  316.  * to work around bugs in some JS interpreters.
  317.  */
  318. function safe_add(x, y)
  319. {
  320.   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  321.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  322.   return (msw << 16) | (lsw & 0xFFFF);
  323. }
  324.  
  325. /*
  326.  * Bitwise rotate a 32-bit number to the left.
  327.  */
  328. function bit_rol(num, cnt)
  329. {
  330.   return (num << cnt) | (num >>> (32 - cnt));
  331. }
  332. //javascript/8902

Reply to "JavaScript SHA1 encryption algorithm implementation detailed code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website