2 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
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.
11 * Configurable variables. You may need to tweak these to be compatible with
12 * the server-side, but the defaults work in most cases.
14 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
15 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
18 * These are the functions you'll usually want to call
19 * They take string arguments and return either hex or base-64 encoded strings
21 function hex_sha1(s) { return rstr2hex(rstr_sha1(str2rstr_utf8(s))); }
22 function b64_sha1(s) { return rstr2b64(rstr_sha1(str2rstr_utf8(s))); }
23 function any_sha1(s, e) { return rstr2any(rstr_sha1(str2rstr_utf8(s)), e); }
24 function hex_hmac_sha1(k, d)
25 { return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
26 function b64_hmac_sha1(k, d)
27 { return rstr2b64(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
28 function any_hmac_sha1(k, d, e)
29 { return rstr2any(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
32 * Perform a simple self-test to see if the VM is working
34 function sha1_vm_test()
36 return hex_sha1("abc").toLowerCase() == "a9993e364706816aba3e25717850c26c9cd0d89d";
40 * Calculate the SHA1 of a raw string
44 return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8));
48 * Calculate the HMAC-SHA1 of a key and some data (raw strings)
50 function rstr_hmac_sha1(key, data)
52 var bkey = rstr2binb(key);
53 if(bkey.length > 16) bkey = binb_sha1(bkey, key.length * 8);
55 var ipad = Array(16), opad = Array(16);
56 for(var i = 0; i < 16; i++)
58 ipad[i] = bkey[i] ^ 0x36363636;
59 opad[i] = bkey[i] ^ 0x5C5C5C5C;
62 var hash = binb_sha1(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
63 return binb2rstr(binb_sha1(opad.concat(hash), 512 + 160));
67 * Convert a raw string to a hex string
69 function rstr2hex(input)
71 try { hexcase } catch(e) { hexcase=0; }
72 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
75 for(var i = 0; i < input.length; i++)
77 x = input.charCodeAt(i);
78 output += hex_tab.charAt((x >>> 4) & 0x0F)
79 + hex_tab.charAt( x & 0x0F);
85 * Convert a raw string to a base-64 string
87 function rstr2b64(input)
89 try { b64pad } catch(e) { b64pad=''; }
90 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
92 var len = input.length;
93 for(var i = 0; i < len; i += 3)
95 var triplet = (input.charCodeAt(i) << 16)
96 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
97 | (i + 2 < len ? input.charCodeAt(i+2) : 0);
98 for(var j = 0; j < 4; j++)
100 if(i * 8 + j * 6 > input.length * 8) output += b64pad;
101 else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
108 * Convert a raw string to an arbitrary string encoding
110 function rstr2any(input, encoding)
112 var divisor = encoding.length;
113 var remainders = Array();
114 var i, q, x, quotient;
116 /* Convert to an array of 16-bit big-endian values, forming the dividend */
117 var dividend = Array(Math.ceil(input.length / 2));
118 for(i = 0; i < dividend.length; i++)
120 dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
124 * Repeatedly perform a long division. The binary array forms the dividend,
125 * the length of the encoding is the divisor. Once computed, the quotient
126 * forms the dividend for the next step. We stop when the dividend is zero.
127 * All remainders are stored for later use.
129 while(dividend.length > 0)
133 for(i = 0; i < dividend.length; i++)
135 x = (x << 16) + dividend[i];
136 q = Math.floor(x / divisor);
138 if(quotient.length > 0 || q > 0)
139 quotient[quotient.length] = q;
141 remainders[remainders.length] = x;
145 /* Convert the remainders to the output string */
147 for(i = remainders.length - 1; i >= 0; i--)
148 output += encoding.charAt(remainders[i]);
150 /* Append leading zero equivalents */
151 var full_length = Math.ceil(input.length * 8 /
152 (Math.log(encoding.length) / Math.log(2)))
153 for(i = output.length; i < full_length; i++)
154 output = encoding[0] + output;
160 * Encode a string as utf-8.
161 * For efficiency, this assumes the input is valid utf-16.
163 function str2rstr_utf8(input)
169 while(++i < input.length)
171 /* Decode utf-16 surrogate pairs */
172 x = input.charCodeAt(i);
173 y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
174 if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
176 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
180 /* Encode output as utf-8 */
182 output += String.fromCharCode(x);
184 output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
187 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
188 0x80 | ((x >>> 6 ) & 0x3F),
190 else if(x <= 0x1FFFFF)
191 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
192 0x80 | ((x >>> 12) & 0x3F),
193 0x80 | ((x >>> 6 ) & 0x3F),
200 * Encode a string as utf-16
202 function str2rstr_utf16le(input)
205 for(var i = 0; i < input.length; i++)
206 output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
207 (input.charCodeAt(i) >>> 8) & 0xFF);
211 function str2rstr_utf16be(input)
214 for(var i = 0; i < input.length; i++)
215 output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
216 input.charCodeAt(i) & 0xFF);
221 * Convert a raw string to an array of big-endian words
222 * Characters >255 have their high-byte silently ignored.
224 function rstr2binb(input)
226 var output = Array(input.length >> 2);
227 for(var i = 0; i < output.length; i++)
229 for(var i = 0; i < input.length * 8; i += 8)
230 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
235 * Convert an array of big-endian words to a string
237 function binb2rstr(input)
240 for(var i = 0; i < input.length * 32; i += 8)
241 output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
246 * Calculate the SHA-1 of an array of big-endian words, and a bit length
248 function binb_sha1(x, len)
251 x[len >> 5] |= 0x80 << (24 - len % 32);
252 x[((len + 64 >> 9) << 4) + 15] = len;
261 for(var i = 0; i < x.length; i += 16)
269 for(var j = 0; j < 80; j++)
271 if(j < 16) w[j] = x[i + j];
272 else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
273 var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
274 safe_add(safe_add(e, w[j]), sha1_kt(j)));
282 a = safe_add(a, olda);
283 b = safe_add(b, oldb);
284 c = safe_add(c, oldc);
285 d = safe_add(d, oldd);
286 e = safe_add(e, olde);
288 return Array(a, b, c, d, e);
293 * Perform the appropriate triplet combination function for the current
296 function sha1_ft(t, b, c, d)
298 if(t < 20) return (b & c) | ((~b) & d);
299 if(t < 40) return b ^ c ^ d;
300 if(t < 60) return (b & c) | (b & d) | (c & d);
305 * Determine the appropriate additive constant for the current iteration
309 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
310 (t < 60) ? -1894007588 : -899497514;
314 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
315 * to work around bugs in some JS interpreters.
317 function safe_add(x, y)
319 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
320 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
321 return (msw << 16) | (lsw & 0xFFFF);
325 * Bitwise rotate a 32-bit number to the left.
327 function bit_rol(num, cnt)
329 return (num << cnt) | (num >>> (32 - cnt));