Project

General

Profile

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

haketilo / common / sha256.js @ 1ca1357d

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

    
28
var fake_window = {};
29

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

    
61
var blocks = [];
62

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

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

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

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

    
99
var nodeWrap = function (method, is224) {
100
    var crypto = eval("require('crypto')");
101
    var Buffer = eval("require('buffer').Buffer");
102
    var algorithm = is224 ? 'sha224' : 'sha256';
103
    var nodeMethod = function (message) {
104
	if (typeof message === 'string') {
105
            return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
106
	} else {
107
            if (message === null || message === undefined) {
108
		throw new Error(ERROR);
109
            } else if (message.constructor === ArrayBuffer) {
110
		message = new Uint8Array(message);
111
            }
112
	}
113
	if (Array.isArray(message) || ArrayBuffer.isView(message) ||
114
            message.constructor === Buffer) {
115
            return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
116
	} else {
117
            return method(message);
118
	}
119
    };
120
    return nodeMethod;
121
};
122

    
123
var createHmacOutputMethod = function (outputType, is224) {
124
    return function (key, message) {
125
	return new HmacSha256(key, is224, true).update(message)[outputType]();
126
    };
127
};
128

    
129
var createHmacMethod = function (is224) {
130
    var method = createHmacOutputMethod('hex', is224);
131
    method.create = function (key) {
132
	return new HmacSha256(key, is224);
133
    };
134
    method.update = function (key, message) {
135
	return method.create(key).update(message);
136
    };
137
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
138
	var type = OUTPUT_TYPES[i];
139
	method[type] = createHmacOutputMethod(type, is224);
140
    }
141
    return method;
142
};
143

    
144
function Sha256(is224, sharedMemory) {
145
    if (sharedMemory) {
146
	blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
147
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
148
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
149
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
150
	this.blocks = blocks;
151
    } else {
152
	this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
153
    }
154

    
155
    if (is224) {
156
	this.h0 = 0xc1059ed8;
157
	this.h1 = 0x367cd507;
158
	this.h2 = 0x3070dd17;
159
	this.h3 = 0xf70e5939;
160
	this.h4 = 0xffc00b31;
161
	this.h5 = 0x68581511;
162
	this.h6 = 0x64f98fa7;
163
	this.h7 = 0xbefa4fa4;
164
    } else { // 256
165
	this.h0 = 0x6a09e667;
166
	this.h1 = 0xbb67ae85;
167
	this.h2 = 0x3c6ef372;
168
	this.h3 = 0xa54ff53a;
169
	this.h4 = 0x510e527f;
170
	this.h5 = 0x9b05688c;
171
	this.h6 = 0x1f83d9ab;
172
	this.h7 = 0x5be0cd19;
173
    }
174

    
175
    this.block = this.start = this.bytes = this.hBytes = 0;
176
    this.finalized = this.hashed = false;
177
    this.first = true;
178
    this.is224 = is224;
179
}
180

    
181
Sha256.prototype.update = function (message) {
182
    if (this.finalized) {
183
	return;
184
    }
185
    var notString, type = typeof message;
186
    if (type !== 'string') {
187
	if (type === 'object') {
188
            if (message === null) {
189
		throw new Error(ERROR);
190
            } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
191
		message = new Uint8Array(message);
192
            } else if (!Array.isArray(message)) {
193
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
194
		    throw new Error(ERROR);
195
		}
196
            }
197
	} else {
198
            throw new Error(ERROR);
199
	}
200
	notString = true;
201
    }
202
    var code, index = 0, i, length = message.length, blocks = this.blocks;
203

    
204
    while (index < length) {
205
	if (this.hashed) {
206
            this.hashed = false;
207
            blocks[0] = this.block;
208
            blocks[16] = blocks[1] = blocks[2] = blocks[3] =
209
		blocks[4] = blocks[5] = blocks[6] = blocks[7] =
210
		blocks[8] = blocks[9] = blocks[10] = blocks[11] =
211
		blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
212
	}
213

    
214
	if (notString) {
215
            for (i = this.start; index < length && i < 64; ++index) {
216
		blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
217
            }
218
	} else {
219
            for (i = this.start; index < length && i < 64; ++index) {
220
		code = message.charCodeAt(index);
221
		if (code < 0x80) {
222
		    blocks[i >> 2] |= code << SHIFT[i++ & 3];
223
		} else if (code < 0x800) {
224
		    blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
225
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
226
		} else if (code < 0xd800 || code >= 0xe000) {
227
		    blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
228
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
229
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
230
		} else {
231
		    code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
232
		    blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
233
		    blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
234
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
235
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
236
		}
237
            }
238
	}
239

    
240
	this.lastByteIndex = i;
241
	this.bytes += i - this.start;
242
	if (i >= 64) {
243
            this.block = blocks[16];
244
            this.start = i - 64;
245
            this.hash();
246
            this.hashed = true;
247
	} else {
248
            this.start = i;
249
	}
250
    }
251
    if (this.bytes > 4294967295) {
252
	this.hBytes += this.bytes / 4294967296 << 0;
253
	this.bytes = this.bytes % 4294967296;
254
    }
255
    return this;
256
};
257

    
258
Sha256.prototype.finalize = function () {
259
    if (this.finalized) {
260
	return;
261
    }
262
    this.finalized = true;
263
    var blocks = this.blocks, i = this.lastByteIndex;
264
    blocks[16] = this.block;
265
    blocks[i >> 2] |= EXTRA[i & 3];
266
    this.block = blocks[16];
267
    if (i >= 56) {
268
	if (!this.hashed) {
269
            this.hash();
270
	}
271
	blocks[0] = this.block;
272
	blocks[16] = blocks[1] = blocks[2] = blocks[3] =
273
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
274
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
275
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
276
    }
277
    blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
278
    blocks[15] = this.bytes << 3;
279
    this.hash();
280
};
281

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

    
286
    for (j = 16; j < 64; ++j) {
287
	// rightrotate
288
	t1 = blocks[j - 15];
289
	s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
290
	t1 = blocks[j - 2];
291
	s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
292
	blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
293
    }
294

    
295
    bc = b & c;
296
    for (j = 0; j < 64; j += 4) {
297
	if (this.first) {
298
            if (this.is224) {
299
		ab = 300032;
300
		t1 = blocks[0] - 1413257819;
301
		h = t1 - 150054599 << 0;
302
		d = t1 + 24177077 << 0;
303
            } else {
304
		ab = 704751109;
305
		t1 = blocks[0] - 210244248;
306
		h = t1 - 1521486534 << 0;
307
		d = t1 + 143694565 << 0;
308
            }
309
            this.first = false;
310
	} else {
311
            s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
312
            s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
313
            ab = a & b;
314
            maj = ab ^ (a & c) ^ bc;
315
            ch = (e & f) ^ (~e & g);
316
            t1 = h + s1 + ch + K[j] + blocks[j];
317
            t2 = s0 + maj;
318
            h = d + t1 << 0;
319
            d = t1 + t2 << 0;
320
	}
321
	s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
322
	s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
323
	da = d & a;
324
	maj = da ^ (d & b) ^ ab;
325
	ch = (h & e) ^ (~h & f);
326
	t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
327
	t2 = s0 + maj;
328
	g = c + t1 << 0;
329
	c = t1 + t2 << 0;
330
	s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
331
	s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
332
	cd = c & d;
333
	maj = cd ^ (c & a) ^ da;
334
	ch = (g & h) ^ (~g & e);
335
	t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
336
	t2 = s0 + maj;
337
	f = b + t1 << 0;
338
	b = t1 + t2 << 0;
339
	s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
340
	s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
341
	bc = b & c;
342
	maj = bc ^ (b & d) ^ cd;
343
	ch = (f & g) ^ (~f & h);
344
	t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
345
	t2 = s0 + maj;
346
	e = a + t1 << 0;
347
	a = t1 + t2 << 0;
348
    }
349

    
350
    this.h0 = this.h0 + a << 0;
351
    this.h1 = this.h1 + b << 0;
352
    this.h2 = this.h2 + c << 0;
353
    this.h3 = this.h3 + d << 0;
354
    this.h4 = this.h4 + e << 0;
355
    this.h5 = this.h5 + f << 0;
356
    this.h6 = this.h6 + g << 0;
357
    this.h7 = this.h7 + h << 0;
358
};
359

    
360
Sha256.prototype.hex = function () {
361
    this.finalize();
362

    
363
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
364
	h6 = this.h6, h7 = this.h7;
365

    
366
    var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
367
	HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
368
	HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
369
	HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
370
	HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
371
	HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
372
	HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
373
	HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
374
	HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
375
	HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
376
	HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
377
	HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
378
	HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
379
	HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
380
	HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
381
	HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
382
	HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
383
	HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
384
	HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
385
	HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
386
	HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
387
	HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
388
	HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
389
	HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
390
	HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
391
	HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
392
	HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
393
	HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
394
    if (!this.is224) {
395
	hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
396
            HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
397
            HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
398
            HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
399
    }
400
    return hex;
401
};
402

    
403
Sha256.prototype.toString = Sha256.prototype.hex;
404

    
405
Sha256.prototype.digest = function () {
406
    this.finalize();
407

    
408
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
409
	h6 = this.h6, h7 = this.h7;
410

    
411
    var arr = [
412
	(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
413
	(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
414
	(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
415
	(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
416
	(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
417
	(h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
418
	(h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
419
    ];
420
    if (!this.is224) {
421
	arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
422
    }
423
    return arr;
424
};
425

    
426
Sha256.prototype.array = Sha256.prototype.digest;
427

    
428
Sha256.prototype.arrayBuffer = function () {
429
    this.finalize();
430

    
431
    var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
432
    var dataView = new DataView(buffer);
433
    dataView.setUint32(0, this.h0);
434
    dataView.setUint32(4, this.h1);
435
    dataView.setUint32(8, this.h2);
436
    dataView.setUint32(12, this.h3);
437
    dataView.setUint32(16, this.h4);
438
    dataView.setUint32(20, this.h5);
439
    dataView.setUint32(24, this.h6);
440
    if (!this.is224) {
441
	dataView.setUint32(28, this.h7);
442
    }
443
    return buffer;
444
};
445

    
446
function HmacSha256(key, is224, sharedMemory) {
447
    var i, type = typeof key;
448
    if (type === 'string') {
449
	var bytes = [], length = key.length, index = 0, code;
450
	for (i = 0; i < length; ++i) {
451
            code = key.charCodeAt(i);
452
            if (code < 0x80) {
453
		bytes[index++] = code;
454
            } else if (code < 0x800) {
455
		bytes[index++] = (0xc0 | (code >> 6));
456
		bytes[index++] = (0x80 | (code & 0x3f));
457
            } else if (code < 0xd800 || code >= 0xe000) {
458
		bytes[index++] = (0xe0 | (code >> 12));
459
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
460
		bytes[index++] = (0x80 | (code & 0x3f));
461
            } else {
462
		code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
463
		bytes[index++] = (0xf0 | (code >> 18));
464
		bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
465
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
466
		bytes[index++] = (0x80 | (code & 0x3f));
467
            }
468
	}
469
	key = bytes;
470
    } else {
471
	if (type === 'object') {
472
            if (key === null) {
473
		throw new Error(ERROR);
474
            } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
475
		key = new Uint8Array(key);
476
            } else if (!Array.isArray(key)) {
477
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
478
		    throw new Error(ERROR);
479
		}
480
            }
481
	} else {
482
            throw new Error(ERROR);
483
	}
484
    }
485

    
486
    if (key.length > 64) {
487
	key = (new Sha256(is224, true)).update(key).array();
488
    }
489

    
490
    var oKeyPad = [], iKeyPad = [];
491
    for (i = 0; i < 64; ++i) {
492
	var b = key[i] || 0;
493
	oKeyPad[i] = 0x5c ^ b;
494
	iKeyPad[i] = 0x36 ^ b;
495
    }
496

    
497
    Sha256.call(this, is224, sharedMemory);
498

    
499
    this.update(iKeyPad);
500
    this.oKeyPad = oKeyPad;
501
    this.inner = true;
502
    this.sharedMemory = sharedMemory;
503
}
504
HmacSha256.prototype = new Sha256();
505

    
506
HmacSha256.prototype.finalize = function () {
507
    Sha256.prototype.finalize.call(this);
508
    if (this.inner) {
509
	this.inner = false;
510
	var innerHash = this.array();
511
	Sha256.call(this, this.is224, this.sharedMemory);
512
	this.update(this.oKeyPad);
513
	this.update(innerHash);
514
	Sha256.prototype.finalize.call(this);
515
    }
516
};
517

    
518
var exports = createMethod();
519
exports.sha256 = exports;
520
exports.sha224 = createMethod(true);
521
exports.sha256.hmac = createHmacMethod();
522
exports.sha224.hmac = createHmacMethod(true);
523

    
524
if (COMMON_JS) {
525
    module.exports = exports;
526
} else {
527
    root.sha256 = exports.sha256;
528
    root.sha224 = exports.sha224;
529
    if (AMD) {
530
	define(function () {
531
            return exports;
532
	});
533
    }
534
}
535

    
536
const sha256 = fake_window.sha256;
537

    
538
/*
539
 * EXPORTS_START
540
 * EXPORT sha256
541
 * EXPORTS_END
542
 */
(11-11/16)