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