OLD | NEW |
(Empty) | |
| 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 */ |
| 9 // clang-format off |
| 10 /* eslint-disable */ |
| 11 /** |
| 12 * @param {string} str |
| 13 * @return {string} |
| 14 */ |
| 15 ProductRegistry.sha1 = function(str) { |
| 16 return rstr2hex(rstr_sha1(str2rstr_utf8(str))); |
| 17 |
| 18 /** |
| 19 * Calculate the SHA1 of a raw string |
| 20 * @param {string} s |
| 21 * @return {string} |
| 22 */ |
| 23 function rstr_sha1(s) |
| 24 { |
| 25 return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8)); |
| 26 } |
| 27 |
| 28 /** |
| 29 * Convert a raw string to a hex string |
| 30 * @param {string} input |
| 31 * @return {string} |
| 32 */ |
| 33 function rstr2hex(input) |
| 34 { |
| 35 var hex_tab = "0123456789abcdef"; |
| 36 var output = ""; |
| 37 var x; |
| 38 for(var i = 0; i < input.length; i++) |
| 39 { |
| 40 x = input.charCodeAt(i); |
| 41 output += hex_tab.charAt((x >>> 4) & 0x0F) |
| 42 + hex_tab.charAt( x & 0x0F); |
| 43 } |
| 44 return output; |
| 45 } |
| 46 |
| 47 /** |
| 48 * Encode a string as utf-8. |
| 49 * For efficiency, this assumes the input is valid utf-16. |
| 50 * @param {string} input |
| 51 * @return {string} |
| 52 */ |
| 53 function str2rstr_utf8(input) |
| 54 { |
| 55 var output = ""; |
| 56 var i = -1; |
| 57 var x, y; |
| 58 |
| 59 while(++i < input.length) |
| 60 { |
| 61 /* Decode utf-16 surrogate pairs */ |
| 62 x = input.charCodeAt(i); |
| 63 y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0; |
| 64 if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) |
| 65 { |
| 66 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF); |
| 67 i++; |
| 68 } |
| 69 |
| 70 /* Encode output as utf-8 */ |
| 71 if(x <= 0x7F) |
| 72 output += String.fromCharCode(x); |
| 73 else if(x <= 0x7FF) |
| 74 output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F), |
| 75 0x80 | ( x & 0x3F)); |
| 76 else if(x <= 0xFFFF) |
| 77 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F), |
| 78 0x80 | ((x >>> 6 ) & 0x3F), |
| 79 0x80 | ( x & 0x3F)); |
| 80 else if(x <= 0x1FFFFF) |
| 81 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07), |
| 82 0x80 | ((x >>> 12) & 0x3F), |
| 83 0x80 | ((x >>> 6 ) & 0x3F), |
| 84 0x80 | ( x & 0x3F)); |
| 85 } |
| 86 return output; |
| 87 } |
| 88 |
| 89 /** |
| 90 * Convert a raw string to an array of big-endian words |
| 91 * Characters >255 have their high-byte silently ignored. |
| 92 * @param {string} input |
| 93 * @return {!Array<number>} |
| 94 */ |
| 95 function rstr2binb(input) |
| 96 { |
| 97 var output = Array(input.length >> 2); |
| 98 for(var i = 0; i < output.length; i++) |
| 99 output[i] = 0; |
| 100 for(var i = 0; i < input.length * 8; i += 8) |
| 101 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32); |
| 102 return output; |
| 103 } |
| 104 |
| 105 /** |
| 106 * Convert an array of big-endian words to a string |
| 107 * @param {!Array<number>} input |
| 108 * @return {string} |
| 109 */ |
| 110 function binb2rstr(input) |
| 111 { |
| 112 var output = ""; |
| 113 for(var i = 0; i < input.length * 32; i += 8) |
| 114 output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF); |
| 115 return output; |
| 116 } |
| 117 |
| 118 /** |
| 119 * Calculate the SHA-1 of an array of big-endian words, and a bit length |
| 120 * @param {!Array<number>} x |
| 121 * @param {number} len |
| 122 * @return {!Array<number>} |
| 123 */ |
| 124 function binb_sha1(x, len) |
| 125 { |
| 126 /* append padding */ |
| 127 x[len >> 5] |= 0x80 << (24 - len % 32); |
| 128 x[((len + 64 >> 9) << 4) + 15] = len; |
| 129 |
| 130 var w = Array(80); |
| 131 var a = 1732584193; |
| 132 var b = -271733879; |
| 133 var c = -1732584194; |
| 134 var d = 271733878; |
| 135 var e = -1009589776; |
| 136 |
| 137 for(var i = 0; i < x.length; i += 16) |
| 138 { |
| 139 var olda = a; |
| 140 var oldb = b; |
| 141 var oldc = c; |
| 142 var oldd = d; |
| 143 var olde = e; |
| 144 |
| 145 for(var j = 0; j < 80; j++) |
| 146 { |
| 147 if(j < 16) w[j] = x[i + j]; |
| 148 else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); |
| 149 var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)), |
| 150 safe_add(safe_add(e, w[j]), sha1_kt(j))); |
| 151 e = d; |
| 152 d = c; |
| 153 c = bit_rol(b, 30); |
| 154 b = a; |
| 155 a = t; |
| 156 } |
| 157 |
| 158 a = safe_add(a, olda); |
| 159 b = safe_add(b, oldb); |
| 160 c = safe_add(c, oldc); |
| 161 d = safe_add(d, oldd); |
| 162 e = safe_add(e, olde); |
| 163 } |
| 164 return Array(a, b, c, d, e); |
| 165 |
| 166 } |
| 167 |
| 168 /** |
| 169 * Perform the appropriate triplet combination function for the current |
| 170 * iteration |
| 171 * @param {number} t |
| 172 * @param {number} b |
| 173 * @param {number} c |
| 174 * @param {number} d |
| 175 * @return {number} |
| 176 */ |
| 177 function sha1_ft(t, b, c, d) |
| 178 { |
| 179 if(t < 20) return (b & c) | ((~b) & d); |
| 180 if(t < 40) return b ^ c ^ d; |
| 181 if(t < 60) return (b & c) | (b & d) | (c & d); |
| 182 return b ^ c ^ d; |
| 183 } |
| 184 |
| 185 /** |
| 186 * Determine the appropriate additive constant for the current iteration |
| 187 * @param {number} t |
| 188 * @return {number} |
| 189 */ |
| 190 function sha1_kt(t) |
| 191 { |
| 192 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : |
| 193 (t < 60) ? -1894007588 : -899497514; |
| 194 } |
| 195 |
| 196 /** |
| 197 * Add integers, wrapping at 2^32. This uses 16-bit operations internally |
| 198 * to work around bugs in some JS interpreters. |
| 199 * @param {number} x |
| 200 * @param {number} y |
| 201 * @return {number} |
| 202 */ |
| 203 function safe_add(x, y) |
| 204 { |
| 205 var lsw = (x & 0xFFFF) + (y & 0xFFFF); |
| 206 var msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
| 207 return (msw << 16) | (lsw & 0xFFFF); |
| 208 } |
| 209 |
| 210 /** |
| 211 * Bitwise rotate a 32-bit number to the left. |
| 212 * @param {number} num |
| 213 * @param {number} cnt |
| 214 * @return {number} |
| 215 */ |
| 216 function bit_rol(num, cnt) |
| 217 { |
| 218 return (num << cnt) | (num >>> (32 - cnt)); |
| 219 } |
| 220 }; |
OLD | NEW |