You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-disable */
  2. /*
  3. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  4. * in FIPS PUB 180-1
  5. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  6. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  7. * Distributed under the BSD License
  8. * See http://pajhome.org.uk/crypt/md5 for details.
  9. */
  10. /* global define */
  11. /* Some functions and variables have been stripped for use with Strophe */
  12. /*
  13. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  14. */
  15. function core_sha1(x, len) {
  16. /* append padding */
  17. x[len >> 5] |= 0x80 << (24 - len % 32);
  18. x[((len + 64 >> 9) << 4) + 15] = len;
  19. var w = new Array(80);
  20. var a = 1732584193;
  21. var b = -271733879;
  22. var c = -1732584194;
  23. var d = 271733878;
  24. var e = -1009589776;
  25. var i, j, t, olda, oldb, oldc, oldd, olde;
  26. for (i = 0; i < x.length; i += 16) {
  27. olda = a;
  28. oldb = b;
  29. oldc = c;
  30. oldd = d;
  31. olde = e;
  32. for (j = 0; j < 80; j++) {
  33. if (j < 16) {
  34. w[j] = x[i + j];
  35. } else {
  36. w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  37. }
  38. t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
  39. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  40. e = d;
  41. d = c;
  42. c = rol(b, 30);
  43. b = a;
  44. a = t;
  45. }
  46. a = safe_add(a, olda);
  47. b = safe_add(b, oldb);
  48. c = safe_add(c, oldc);
  49. d = safe_add(d, oldd);
  50. e = safe_add(e, olde);
  51. }
  52. return [a, b, c, d, e];
  53. }
  54. /*
  55. * Perform the appropriate triplet combination function for the current
  56. * iteration
  57. */
  58. function sha1_ft (t, b, c, d) {
  59. if (t < 20) { return (b & c) | ((~b) & d); }
  60. if (t < 40) { return b ^ c ^ d; }
  61. if (t < 60) { return (b & c) | (b & d) | (c & d); }
  62. return b ^ c ^ d;
  63. }
  64. /*
  65. * Determine the appropriate additive constant for the current iteration
  66. */
  67. function sha1_kt(t) {
  68. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
  69. }
  70. /*
  71. * Calculate the HMAC-SHA1 of a key and some data
  72. */
  73. function core_hmac_sha1(key, data) {
  74. var bkey = str2binb(key);
  75. if (bkey.length > 16) {
  76. bkey = core_sha1(bkey, key.length * 8);
  77. }
  78. var ipad = new Array(16), opad = new Array(16);
  79. for (var i = 0; i < 16; i++) {
  80. ipad[i] = bkey[i] ^ 0x36363636;
  81. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  82. }
  83. var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * 8);
  84. return core_sha1(opad.concat(hash), 512 + 160);
  85. }
  86. /*
  87. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  88. * to work around bugs in some JS interpreters.
  89. */
  90. function safe_add(x, y) {
  91. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  92. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  93. return (msw << 16) | (lsw & 0xFFFF);
  94. }
  95. /*
  96. * Bitwise rotate a 32-bit number to the left.
  97. */
  98. function rol(num, cnt) {
  99. return (num << cnt) | (num >>> (32 - cnt));
  100. }
  101. /*
  102. * Convert an 8-bit or 16-bit string to an array of big-endian words
  103. * In 8-bit function, characters >255 have their hi-byte silently ignored.
  104. */
  105. function str2binb(str) {
  106. var bin = [];
  107. var mask = 255;
  108. for (var i = 0; i < str.length * 8; i += 8) {
  109. bin[i>>5] |= (str.charCodeAt(i / 8) & mask) << (24 - i%32);
  110. }
  111. return bin;
  112. }
  113. /*
  114. * Convert an array of big-endian words to a base-64 string
  115. */
  116. function binb2b64 (binarray) {
  117. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  118. var str = "";
  119. var triplet, j;
  120. for (var i = 0; i < binarray.length * 4; i += 3) {
  121. triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) |
  122. (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) |
  123. ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
  124. for (j = 0; j < 4; j++) {
  125. if (i * 8 + j * 6 > binarray.length * 32) { str += "="; }
  126. else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
  127. }
  128. }
  129. return str;
  130. }
  131. /*
  132. * Convert an array of big-endian words to a string
  133. */
  134. function binb2str(bin) {
  135. var str = "";
  136. var mask = 255;
  137. for (var i = 0; i < bin.length * 32; i += 8) {
  138. str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask);
  139. }
  140. return str;
  141. }
  142. /*
  143. * These are the functions you'll usually want to call
  144. * They take string arguments and return either hex or base-64 encoded strings
  145. */
  146. const SHA1 = {
  147. b64_hmac_sha1: function (key, data){ return binb2b64(core_hmac_sha1(key, data)); },
  148. b64_sha1: function (s) { return binb2b64(core_sha1(str2binb(s),s.length * 8)); },
  149. binb2str: binb2str,
  150. core_hmac_sha1: core_hmac_sha1,
  151. str_hmac_sha1: function (key, data){ return binb2str(core_hmac_sha1(key, data)); },
  152. str_sha1: function (s) { return binb2str(core_sha1(str2binb(s),s.length * 8)); },
  153. };
  154. export { SHA1 as default };