hex_hmac_sha1.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  3. * in FIPS PUB 180-1
  4. * Version 2.1a 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. * Configurable variables. You may need to tweak these to be compatible with
  11. * the server-side, but the defaults work in most cases.
  12. */
  13. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  14. /*
  15. * These are the functions you'll usually want to call
  16. * They take string arguments and return either hex or base-64 encoded strings
  17. */
  18. function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
  19. /*
  20. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  21. */
  22. function core_sha1(x, len)
  23. {
  24. /* append padding */
  25. x[len >> 5] |= 0x80 << (24 - len % 32);
  26. x[((len + 64 >> 9) << 4) + 15] = len;
  27. var w = Array(80);
  28. var a = 1732584193;
  29. var b = -271733879;
  30. var c = -1732584194;
  31. var d = 271733878;
  32. var e = -1009589776;
  33. for(var i = 0; i < x.length; i += 16)
  34. {
  35. var olda = a;
  36. var oldb = b;
  37. var oldc = c;
  38. var oldd = d;
  39. var olde = e;
  40. for(var j = 0; j < 80; j++)
  41. {
  42. if(j < 16) w[j] = x[i + j];
  43. else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  44. var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
  45. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  46. e = d;
  47. d = c;
  48. c = rol(b, 30);
  49. b = a;
  50. a = t;
  51. }
  52. a = safe_add(a, olda);
  53. b = safe_add(b, oldb);
  54. c = safe_add(c, oldc);
  55. d = safe_add(d, oldd);
  56. e = safe_add(e, olde);
  57. }
  58. return Array(a, b, c, d, e);
  59. }
  60. /*
  61. * Perform the appropriate triplet combination function for the current
  62. * iteration
  63. */
  64. function sha1_ft(t, b, c, d)
  65. {
  66. if(t < 20) return (b & c) | ((~b) & d);
  67. if(t < 40) return b ^ c ^ d;
  68. if(t < 60) return (b & c) | (b & d) | (c & d);
  69. return b ^ c ^ d;
  70. }
  71. /*
  72. * Determine the appropriate additive constant for the current iteration
  73. */
  74. function sha1_kt(t)
  75. {
  76. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  77. (t < 60) ? -1894007588 : -899497514;
  78. }
  79. /*
  80. * Calculate the HMAC-SHA1 of a key and some data
  81. */
  82. function core_hmac_sha1(key, data)
  83. {
  84. var bkey = str2binb(key);
  85. if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
  86. var ipad = Array(16), opad = Array(16);
  87. for(var i = 0; i < 16; i++)
  88. {
  89. ipad[i] = bkey[i] ^ 0x36363636;
  90. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  91. }
  92. var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  93. return core_sha1(opad.concat(hash), 512 + 160);
  94. }
  95. /*
  96. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  97. * to work around bugs in some JS interpreters.
  98. */
  99. function safe_add(x, y)
  100. {
  101. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  102. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  103. return (msw << 16) | (lsw & 0xFFFF);
  104. }
  105. /*
  106. * Bitwise rotate a 32-bit number to the left.
  107. */
  108. function rol(num, cnt)
  109. {
  110. return (num << cnt) | (num >>> (32 - cnt));
  111. }
  112. /*
  113. * Convert an 8-bit or 16-bit string to an array of big-endian words
  114. * In 8-bit function, characters >255 have their hi-byte silently ignored.
  115. */
  116. function str2binb(str)
  117. {
  118. var bin = Array();
  119. var mask = (1 << chrsz) - 1;
  120. for(var i = 0; i < str.length * chrsz; i += chrsz)
  121. bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
  122. return bin;
  123. }
  124. /*
  125. * Convert an array of big-endian words to a hex string.
  126. */
  127. function binb2hex(binarray)
  128. {
  129. var hex_tab = "0123456789ABCDEF";
  130. var str = "";
  131. for(var i = 0; i < binarray.length * 4; i++)
  132. {
  133. str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
  134. hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
  135. }
  136. return str;
  137. }