Project

General

Profile

Download (16.9 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / sha256.js @ 1f9ccef9

1
/**
2
 * [js-sha256]{@link https://github.com/emn178/js-sha256/}
3
 *
4
 * @version 0.9.0
5
 *
6
 * Copyright (c) 2014-2017 Chen, Yi-Cyuan
7
 *
8
 * MIT License
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining
11
 * a copy of this software and associated documentation files (the
12
 * "Software"), to deal in the Software without restriction, including
13
 * without limitation the rights to use, copy, modify, merge, publish,
14
 * distribute, sublicense, and/or sell copies of the Software, and to
15
 * permit persons to whom the Software is furnished to do so, subject to
16
 * the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be
19
 * included in all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
 */
29

    
30
var fake_window = {};
31

    
32
var ERROR = 'input is invalid type';
33
var WINDOW = typeof fake_window === 'object';
34
var root = /*WINDOW ? window : {}*/ fake_window;
35
if (root.JS_SHA256_NO_WINDOW) {
36
    WINDOW = false;
37
}
38
var WEB_WORKER = !WINDOW && typeof self === 'object';
39
if (WEB_WORKER) {
40
    root = self;
41
}
42
var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
43
var AMD = typeof define === 'function' && define.amd;
44
var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
45
var HEX_CHARS = '0123456789abcdef'.split('');
46
var EXTRA = [-2147483648, 8388608, 32768, 128];
47
var SHIFT = [24, 16, 8, 0];
48
var K = [
49
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
50
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
51
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
52
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
53
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
54
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
55
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
56
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
57
];
58
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
59

    
60
var blocks = [];
61

    
62
if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
63
    Array.isArray = function (obj) {
64
	return Object.prototype.toString.call(obj) === '[object Array]';
65
    };
66
}
67

    
68
if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
69
    ArrayBuffer.isView = function (obj) {
70
	return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
71
    };
72
}
73

    
74
var createOutputMethod = function (outputType, is224) {
75
    return function (message) {
76
	return new Sha256(is224, true).update(message)[outputType]();
77
    };
78
};
79

    
80
var createMethod = function (is224) {
81
    var method = createOutputMethod('hex', is224);
82
    method.create = function () {
83
	return new Sha256(is224);
84
    };
85
    method.update = function (message) {
86
	return method.create().update(message);
87
    };
88
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
89
	var type = OUTPUT_TYPES[i];
90
	method[type] = createOutputMethod(type, is224);
91
    }
92
    return method;
93
};
94

    
95
var createHmacOutputMethod = function (outputType, is224) {
96
    return function (key, message) {
97
	return new HmacSha256(key, is224, true).update(message)[outputType]();
98
    };
99
};
100

    
101
var createHmacMethod = function (is224) {
102
    var method = createHmacOutputMethod('hex', is224);
103
    method.create = function (key) {
104
	return new HmacSha256(key, is224);
105
    };
106
    method.update = function (key, message) {
107
	return method.create(key).update(message);
108
    };
109
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
110
	var type = OUTPUT_TYPES[i];
111
	method[type] = createHmacOutputMethod(type, is224);
112
    }
113
    return method;
114
};
115

    
116
function Sha256(is224, sharedMemory) {
117
    if (sharedMemory) {
118
	blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
119
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
120
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
121
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
122
	this.blocks = blocks;
123
    } else {
124
	this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
125
    }
126

    
127
    if (is224) {
128
	this.h0 = 0xc1059ed8;
129
	this.h1 = 0x367cd507;
130
	this.h2 = 0x3070dd17;
131
	this.h3 = 0xf70e5939;
132
	this.h4 = 0xffc00b31;
133
	this.h5 = 0x68581511;
134
	this.h6 = 0x64f98fa7;
135
	this.h7 = 0xbefa4fa4;
136
    } else { // 256
137
	this.h0 = 0x6a09e667;
138
	this.h1 = 0xbb67ae85;
139
	this.h2 = 0x3c6ef372;
140
	this.h3 = 0xa54ff53a;
141
	this.h4 = 0x510e527f;
142
	this.h5 = 0x9b05688c;
143
	this.h6 = 0x1f83d9ab;
144
	this.h7 = 0x5be0cd19;
145
    }
146

    
147
    this.block = this.start = this.bytes = this.hBytes = 0;
148
    this.finalized = this.hashed = false;
149
    this.first = true;
150
    this.is224 = is224;
151
}
152

    
153
Sha256.prototype.update = function (message) {
154
    if (this.finalized) {
155
	return;
156
    }
157
    var notString, type = typeof message;
158
    if (type !== 'string') {
159
	if (type === 'object') {
160
            if (message === null) {
161
		throw new Error(ERROR);
162
            } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
163
		message = new Uint8Array(message);
164
            } else if (!Array.isArray(message)) {
165
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
166
		    throw new Error(ERROR);
167
		}
168
            }
169
	} else {
170
            throw new Error(ERROR);
171
	}
172
	notString = true;
173
    }
174
    var code, index = 0, i, length = message.length, blocks = this.blocks;
175

    
176
    while (index < length) {
177
	if (this.hashed) {
178
            this.hashed = false;
179
            blocks[0] = this.block;
180
            blocks[16] = blocks[1] = blocks[2] = blocks[3] =
181
		blocks[4] = blocks[5] = blocks[6] = blocks[7] =
182
		blocks[8] = blocks[9] = blocks[10] = blocks[11] =
183
		blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
184
	}
185

    
186
	if (notString) {
187
            for (i = this.start; index < length && i < 64; ++index) {
188
		blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
189
            }
190
	} else {
191
            for (i = this.start; index < length && i < 64; ++index) {
192
		code = message.charCodeAt(index);
193
		if (code < 0x80) {
194
		    blocks[i >> 2] |= code << SHIFT[i++ & 3];
195
		} else if (code < 0x800) {
196
		    blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
197
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
198
		} else if (code < 0xd800 || code >= 0xe000) {
199
		    blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
200
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
201
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
202
		} else {
203
		    code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
204
		    blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
205
		    blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
206
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
207
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
208
		}
209
            }
210
	}
211

    
212
	this.lastByteIndex = i;
213
	this.bytes += i - this.start;
214
	if (i >= 64) {
215
            this.block = blocks[16];
216
            this.start = i - 64;
217
            this.hash();
218
            this.hashed = true;
219
	} else {
220
            this.start = i;
221
	}
222
    }
223
    if (this.bytes > 4294967295) {
224
	this.hBytes += this.bytes / 4294967296 << 0;
225
	this.bytes = this.bytes % 4294967296;
226
    }
227
    return this;
228
};
229

    
230
Sha256.prototype.finalize = function () {
231
    if (this.finalized) {
232
	return;
233
    }
234
    this.finalized = true;
235
    var blocks = this.blocks, i = this.lastByteIndex;
236
    blocks[16] = this.block;
237
    blocks[i >> 2] |= EXTRA[i & 3];
238
    this.block = blocks[16];
239
    if (i >= 56) {
240
	if (!this.hashed) {
241
            this.hash();
242
	}
243
	blocks[0] = this.block;
244
	blocks[16] = blocks[1] = blocks[2] = blocks[3] =
245
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
246
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
247
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
248
    }
249
    blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
250
    blocks[15] = this.bytes << 3;
251
    this.hash();
252
};
253

    
254
Sha256.prototype.hash = function () {
255
    var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
256
	h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
257

    
258
    for (j = 16; j < 64; ++j) {
259
	// rightrotate
260
	t1 = blocks[j - 15];
261
	s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
262
	t1 = blocks[j - 2];
263
	s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
264
	blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
265
    }
266

    
267
    bc = b & c;
268
    for (j = 0; j < 64; j += 4) {
269
	if (this.first) {
270
            if (this.is224) {
271
		ab = 300032;
272
		t1 = blocks[0] - 1413257819;
273
		h = t1 - 150054599 << 0;
274
		d = t1 + 24177077 << 0;
275
            } else {
276
		ab = 704751109;
277
		t1 = blocks[0] - 210244248;
278
		h = t1 - 1521486534 << 0;
279
		d = t1 + 143694565 << 0;
280
            }
281
            this.first = false;
282
	} else {
283
            s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
284
            s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
285
            ab = a & b;
286
            maj = ab ^ (a & c) ^ bc;
287
            ch = (e & f) ^ (~e & g);
288
            t1 = h + s1 + ch + K[j] + blocks[j];
289
            t2 = s0 + maj;
290
            h = d + t1 << 0;
291
            d = t1 + t2 << 0;
292
	}
293
	s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
294
	s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
295
	da = d & a;
296
	maj = da ^ (d & b) ^ ab;
297
	ch = (h & e) ^ (~h & f);
298
	t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
299
	t2 = s0 + maj;
300
	g = c + t1 << 0;
301
	c = t1 + t2 << 0;
302
	s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
303
	s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
304
	cd = c & d;
305
	maj = cd ^ (c & a) ^ da;
306
	ch = (g & h) ^ (~g & e);
307
	t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
308
	t2 = s0 + maj;
309
	f = b + t1 << 0;
310
	b = t1 + t2 << 0;
311
	s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
312
	s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
313
	bc = b & c;
314
	maj = bc ^ (b & d) ^ cd;
315
	ch = (f & g) ^ (~f & h);
316
	t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
317
	t2 = s0 + maj;
318
	e = a + t1 << 0;
319
	a = t1 + t2 << 0;
320
    }
321

    
322
    this.h0 = this.h0 + a << 0;
323
    this.h1 = this.h1 + b << 0;
324
    this.h2 = this.h2 + c << 0;
325
    this.h3 = this.h3 + d << 0;
326
    this.h4 = this.h4 + e << 0;
327
    this.h5 = this.h5 + f << 0;
328
    this.h6 = this.h6 + g << 0;
329
    this.h7 = this.h7 + h << 0;
330
};
331

    
332
Sha256.prototype.hex = function () {
333
    this.finalize();
334

    
335
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
336
	h6 = this.h6, h7 = this.h7;
337

    
338
    var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
339
	HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
340
	HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
341
	HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
342
	HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
343
	HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
344
	HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
345
	HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
346
	HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
347
	HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
348
	HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
349
	HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
350
	HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
351
	HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
352
	HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
353
	HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
354
	HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
355
	HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
356
	HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
357
	HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
358
	HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
359
	HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
360
	HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
361
	HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
362
	HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
363
	HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
364
	HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
365
	HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
366
    if (!this.is224) {
367
	hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
368
            HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
369
            HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
370
            HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
371
    }
372
    return hex;
373
};
374

    
375
Sha256.prototype.toString = Sha256.prototype.hex;
376

    
377
Sha256.prototype.digest = function () {
378
    this.finalize();
379

    
380
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
381
	h6 = this.h6, h7 = this.h7;
382

    
383
    var arr = [
384
	(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
385
	(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
386
	(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
387
	(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
388
	(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
389
	(h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
390
	(h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
391
    ];
392
    if (!this.is224) {
393
	arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
394
    }
395
    return arr;
396
};
397

    
398
Sha256.prototype.array = Sha256.prototype.digest;
399

    
400
Sha256.prototype.arrayBuffer = function () {
401
    this.finalize();
402

    
403
    var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
404
    var dataView = new DataView(buffer);
405
    dataView.setUint32(0, this.h0);
406
    dataView.setUint32(4, this.h1);
407
    dataView.setUint32(8, this.h2);
408
    dataView.setUint32(12, this.h3);
409
    dataView.setUint32(16, this.h4);
410
    dataView.setUint32(20, this.h5);
411
    dataView.setUint32(24, this.h6);
412
    if (!this.is224) {
413
	dataView.setUint32(28, this.h7);
414
    }
415
    return buffer;
416
};
417

    
418
function HmacSha256(key, is224, sharedMemory) {
419
    var i, type = typeof key;
420
    if (type === 'string') {
421
	var bytes = [], length = key.length, index = 0, code;
422
	for (i = 0; i < length; ++i) {
423
            code = key.charCodeAt(i);
424
            if (code < 0x80) {
425
		bytes[index++] = code;
426
            } else if (code < 0x800) {
427
		bytes[index++] = (0xc0 | (code >> 6));
428
		bytes[index++] = (0x80 | (code & 0x3f));
429
            } else if (code < 0xd800 || code >= 0xe000) {
430
		bytes[index++] = (0xe0 | (code >> 12));
431
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
432
		bytes[index++] = (0x80 | (code & 0x3f));
433
            } else {
434
		code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
435
		bytes[index++] = (0xf0 | (code >> 18));
436
		bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
437
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
438
		bytes[index++] = (0x80 | (code & 0x3f));
439
            }
440
	}
441
	key = bytes;
442
    } else {
443
	if (type === 'object') {
444
            if (key === null) {
445
		throw new Error(ERROR);
446
            } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
447
		key = new Uint8Array(key);
448
            } else if (!Array.isArray(key)) {
449
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
450
		    throw new Error(ERROR);
451
		}
452
            }
453
	} else {
454
            throw new Error(ERROR);
455
	}
456
    }
457

    
458
    if (key.length > 64) {
459
	key = (new Sha256(is224, true)).update(key).array();
460
    }
461

    
462
    var oKeyPad = [], iKeyPad = [];
463
    for (i = 0; i < 64; ++i) {
464
	var b = key[i] || 0;
465
	oKeyPad[i] = 0x5c ^ b;
466
	iKeyPad[i] = 0x36 ^ b;
467
    }
468

    
469
    Sha256.call(this, is224, sharedMemory);
470

    
471
    this.update(iKeyPad);
472
    this.oKeyPad = oKeyPad;
473
    this.inner = true;
474
    this.sharedMemory = sharedMemory;
475
}
476
HmacSha256.prototype = new Sha256();
477

    
478
HmacSha256.prototype.finalize = function () {
479
    Sha256.prototype.finalize.call(this);
480
    if (this.inner) {
481
	this.inner = false;
482
	var innerHash = this.array();
483
	Sha256.call(this, this.is224, this.sharedMemory);
484
	this.update(this.oKeyPad);
485
	this.update(innerHash);
486
	Sha256.prototype.finalize.call(this);
487
    }
488
};
489

    
490
var exports = createMethod();
491
exports.sha256 = exports;
492
exports.sha224 = createMethod(true);
493
exports.sha256.hmac = createHmacMethod();
494
exports.sha224.hmac = createHmacMethod(true);
495

    
496
if (COMMON_JS) {
497
    module.exports = exports;
498
} else {
499
    root.sha256 = exports.sha256;
500
    root.sha224 = exports.sha224;
501
    if (AMD) {
502
	define(function () {
503
            return exports;
504
	});
505
    }
506
}
507

    
508
#EXPORT  fake_window.sha256  AS sha256
(11-11/11)