blob: b3b38da75f210d6029929cdb038a82926b3e6418 [file] [log] [blame]
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07001/*
2 * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
3 * in FIPS 180-2
4 * Version 2.2 Copyright Angel Marin, 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 * Also http://anmar.eu.org/projects/jssha2/
9 */
10
11/*
12 * Configurable variables. You may need to tweak these to be compatible with
13 * the server-side, but the defaults work in most cases.
14 */
15var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
16var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
17
18/*
19 * These are the functions you'll usually want to call
20 * They take string arguments and return either hex or base-64 encoded strings
21 */
22
23
24function hex_sha256(s) { return rstr2hex(rstr_sha256(str2rstr_utf8(s))); }
25function b64_sha256(s) { return rstr2b64(rstr_sha256(str2rstr_utf8(s))); }
26function any_sha256(s, e) { return rstr2any(rstr_sha256(str2rstr_utf8(s)), e); }
27function hex_hmac_sha256(k, d)
28 { return rstr2hex(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d))); }
29function b64_hmac_sha256(k, d)
30 { return rstr2b64(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d))); }
31function any_hmac_sha256(k, d, e)
32 { return rstr2any(rstr_hmac_sha256(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
33
34
35/*
36 function hex_sha256(s) { return rstr2hex(rstr_sha256(s)); }
37function b64_sha256(s) { return rstr2b64(rstr_sha256(s)); }
38function any_sha256(s, e) { return rstr2any(rstr_sha256(s), e); }
39function hex_hmac_sha256(k, d)
40 { return rstr2hex(rstr_hmac_sha256(str2rstr_utf8(k), d)); }
41function b64_hmac_sha256(k, d)
42 { return rstr2b64(rstr_hmac_sha256(str2rstr_utf8(k), d)); }
43function any_hmac_sha256(k, d, e)
44 { return rstr2any(rstr_hmac_sha256(str2rstr_utf8(k), d), e); }
45*/
46
47/*
48 * Perform a simple self-test to see if the VM is working
49 */
50function sha256_vm_test()
51{
52 return hex_sha256("abc").toLowerCase() ==
53 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
54}
55
56/*
57 * Calculate the sha256 of a raw string
58 */
59function rstr_sha256(s)
60{
61 return binb2rstr(binb_sha256(rstr2binb(s), s.length * 8));
62}
63
64/*
65 * Calculate the HMAC-sha256 of a key and some data (raw strings)
66 */
67function rstr_hmac_sha256(key, data)
68{
69 var bkey = rstr2binb(key);
70 if(bkey.length > 16) bkey = binb_sha256(bkey, key.length * 8);
71
72 var ipad = Array(16), opad = Array(16);
73 for(var i = 0; i < 16; i++)
74 {
75 ipad[i] = bkey[i] ^ 0x36363636;
76 opad[i] = bkey[i] ^ 0x5C5C5C5C;
77 }
78
79 var hash = binb_sha256(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
80 return binb2rstr(binb_sha256(opad.concat(hash), 512 + 256));
81}
82
83/*
84 * Convert a raw string to a hex string
85 */
86function rstr2hex(input)
87{
88 try { hexcase } catch(e) { hexcase=0; }
89 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
90 var output = "";
91 var x;
92 for(var i = 0; i < input.length; i++)
93 {
94 x = input.charCodeAt(i);
95 output += hex_tab.charAt((x >>> 4) & 0x0F)
96 + hex_tab.charAt( x & 0x0F);
97 }
98 return output;
99}
100
101/*
102 * Convert a raw string to a base-64 string
103 */
104function rstr2b64(input)
105{
106 try { b64pad } catch(e) { b64pad=''; }
107 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
108 var output = "";
109 var len = input.length;
110 for(var i = 0; i < len; i += 3)
111 {
112 var triplet = (input.charCodeAt(i) << 16)
113 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
114 | (i + 2 < len ? input.charCodeAt(i+2) : 0);
115 for(var j = 0; j < 4; j++)
116 {
117 if(i * 8 + j * 6 > input.length * 8) output += b64pad;
118 else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
119 }
120 }
121 return output;
122}
123
124/*
125 * Convert a raw string to an arbitrary string encoding
126 */
127function rstr2any(input, encoding)
128{
129 var divisor = encoding.length;
130 var remainders = Array();
131 var i, q, x, quotient;
132
133 /* Convert to an array of 16-bit big-endian values, forming the dividend */
134 var dividend = Array(Math.ceil(input.length / 2));
135 for(i = 0; i < dividend.length; i++)
136 {
137 dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
138 }
139
140 /*
141 * Repeatedly perform a long division. The binary array forms the dividend,
142 * the length of the encoding is the divisor. Once computed, the quotient
143 * forms the dividend for the next step. We stop when the dividend is zero.
144 * All remainders are stored for later use.
145 */
146 while(dividend.length > 0)
147 {
148 quotient = Array();
149 x = 0;
150 for(i = 0; i < dividend.length; i++)
151 {
152 x = (x << 16) + dividend[i];
153 q = Math.floor(x / divisor);
154 x -= q * divisor;
155 if(quotient.length > 0 || q > 0)
156 quotient[quotient.length] = q;
157 }
158 remainders[remainders.length] = x;
159 dividend = quotient;
160 }
161
162 /* Convert the remainders to the output string */
163 var output = "";
164 for(i = remainders.length - 1; i >= 0; i--)
165 output += encoding.charAt(remainders[i]);
166
167 /* Append leading zero equivalents */
168 var full_length = Math.ceil(input.length * 8 /
169 (Math.log(encoding.length) / Math.log(2)))
170 for(i = output.length; i < full_length; i++)
171 output = encoding[0] + output;
172
173 return output;
174}
175
176/*
177 * Encode a string as utf-8.
178 * For efficiency, this assumes the input is valid utf-16.
179 */
180function str2rstr_utf8(input)
181{
182 var output = "";
183 var i = -1;
184 var x, y;
185
186 while(++i < input.length)
187 {
188 /* Decode utf-16 surrogate pairs */
189 x = input.charCodeAt(i);
190 y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
191 if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
192 {
193 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
194 i++;
195 }
196
197 /* Encode output as utf-8 */
198 if(x <= 0x7F)
199 output += String.fromCharCode(x);
200 else if(x <= 0x7FF)
201 output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
202 0x80 | ( x & 0x3F));
203 else if(x <= 0xFFFF)
204 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
205 0x80 | ((x >>> 6 ) & 0x3F),
206 0x80 | ( x & 0x3F));
207 else if(x <= 0x1FFFFF)
208 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
209 0x80 | ((x >>> 12) & 0x3F),
210 0x80 | ((x >>> 6 ) & 0x3F),
211 0x80 | ( x & 0x3F));
212 }
213 return output;
214}
215
216/*
217 * Encode a string as utf-16
218 */
219function str2rstr_utf16le(input)
220{
221 var output = "";
222 for(var i = 0; i < input.length; i++)
223 output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
224 (input.charCodeAt(i) >>> 8) & 0xFF);
225 return output;
226}
227
228function str2rstr_utf16be(input)
229{
230 var output = "";
231 for(var i = 0; i < input.length; i++)
232 output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
233 input.charCodeAt(i) & 0xFF);
234 return output;
235}
236
237/*
238 * Convert a raw string to an array of big-endian words
239 * Characters >255 have their high-byte silently ignored.
240 */
241function rstr2binb(input)
242{
243 console.log('Raw string comming is '+input);
244 var output = Array(input.length >> 2);
245 for(var i = 0; i < output.length; i++)
246 output[i] = 0;
247 for(var i = 0; i < input.length * 8; i += 8)
248 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
249 return output;
250}
251
252/*
253 * Convert an array of big-endian words to a string
254 */
255function binb2rstr(input)
256{
257 var output = "";
258 for(var i = 0; i < input.length * 32; i += 8)
259 output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
260 return output;
261}
262
263/*
264 * Main sha256 function, with its support functions
265 */
266function sha256_S (X, n) {return ( X >>> n ) | (X << (32 - n));}
267function sha256_R (X, n) {return ( X >>> n );}
268function sha256_Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
269function sha256_Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
270function sha256_Sigma0256(x) {return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22));}
271function sha256_Sigma1256(x) {return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25));}
272function sha256_Gamma0256(x) {return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3));}
273function sha256_Gamma1256(x) {return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10));}
274function sha256_Sigma0512(x) {return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39));}
275function sha256_Sigma1512(x) {return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41));}
276function sha256_Gamma0512(x) {return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7));}
277function sha256_Gamma1512(x) {return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6));}
278
279var sha256_K = new Array
280(
281 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993,
282 -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
283 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
284 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986,
285 -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
286 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
287 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
288 -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
289 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
290 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872,
291 -1866530822, -1538233109, -1090935817, -965641998
292);
293
294function binb_sha256(m, l)
295{
296 var HASH = new Array(1779033703, -1150833019, 1013904242, -1521486534,
297 1359893119, -1694144372, 528734635, 1541459225);
298 var W = new Array(64);
299 var a, b, c, d, e, f, g, h;
300 var i, j, T1, T2;
301
302 /* append padding */
303 m[l >> 5] |= 0x80 << (24 - l % 32);
304 m[((l + 64 >> 9) << 4) + 15] = l;
305
306 for(i = 0; i < m.length; i += 16)
307 {
308 a = HASH[0];
309 b = HASH[1];
310 c = HASH[2];
311 d = HASH[3];
312 e = HASH[4];
313 f = HASH[5];
314 g = HASH[6];
315 h = HASH[7];
316
317 for(j = 0; j < 64; j++)
318 {
319 if (j < 16) W[j] = m[j + i];
320 else W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]),
321 sha256_Gamma0256(W[j - 15])), W[j - 16]);
322
323 T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)),
324 sha256_K[j]), W[j]);
325 T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c));
326 h = g;
327 g = f;
328 f = e;
329 e = safe_add(d, T1);
330 d = c;
331 c = b;
332 b = a;
333 a = safe_add(T1, T2);
334 }
335
336 HASH[0] = safe_add(a, HASH[0]);
337 HASH[1] = safe_add(b, HASH[1]);
338 HASH[2] = safe_add(c, HASH[2]);
339 HASH[3] = safe_add(d, HASH[3]);
340 HASH[4] = safe_add(e, HASH[4]);
341 HASH[5] = safe_add(f, HASH[5]);
342 HASH[6] = safe_add(g, HASH[6]);
343 HASH[7] = safe_add(h, HASH[7]);
344 }
345 return HASH;
346}
347
348function safe_add (x, y)
349{
350 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
351 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
352 return (msw << 16) | (lsw & 0xFFFF);
353}