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
|
var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
|
40
|
if (NODE_JS) {
|
41
|
root = global;
|
42
|
} else if (WEB_WORKER) {
|
43
|
root = self;
|
44
|
}
|
45
|
var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
|
46
|
var AMD = typeof define === 'function' && define.amd;
|
47
|
var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
|
48
|
var HEX_CHARS = '0123456789abcdef'.split('');
|
49
|
var EXTRA = [-2147483648, 8388608, 32768, 128];
|
50
|
var SHIFT = [24, 16, 8, 0];
|
51
|
var K = [
|
52
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
53
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
54
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
55
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
56
|
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
57
|
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
58
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
59
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
60
|
];
|
61
|
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
|
62
|
|
63
|
var blocks = [];
|
64
|
|
65
|
if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
|
66
|
Array.isArray = function (obj) {
|
67
|
return Object.prototype.toString.call(obj) === '[object Array]';
|
68
|
};
|
69
|
}
|
70
|
|
71
|
if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
|
72
|
ArrayBuffer.isView = function (obj) {
|
73
|
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
|
74
|
};
|
75
|
}
|
76
|
|
77
|
var createOutputMethod = function (outputType, is224) {
|
78
|
return function (message) {
|
79
|
return new Sha256(is224, true).update(message)[outputType]();
|
80
|
};
|
81
|
};
|
82
|
|
83
|
var createMethod = function (is224) {
|
84
|
var method = createOutputMethod('hex', is224);
|
85
|
if (NODE_JS) {
|
86
|
method = nodeWrap(method, is224);
|
87
|
}
|
88
|
method.create = function () {
|
89
|
return new Sha256(is224);
|
90
|
};
|
91
|
method.update = function (message) {
|
92
|
return method.create().update(message);
|
93
|
};
|
94
|
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
95
|
var type = OUTPUT_TYPES[i];
|
96
|
method[type] = createOutputMethod(type, is224);
|
97
|
}
|
98
|
return method;
|
99
|
};
|
100
|
|
101
|
var nodeWrap = function (method, is224) {
|
102
|
var crypto = eval("require('crypto')");
|
103
|
var Buffer = eval("require('buffer').Buffer");
|
104
|
var algorithm = is224 ? 'sha224' : 'sha256';
|
105
|
var nodeMethod = function (message) {
|
106
|
if (typeof message === 'string') {
|
107
|
return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
|
108
|
} else {
|
109
|
if (message === null || message === undefined) {
|
110
|
throw new Error(ERROR);
|
111
|
} else if (message.constructor === ArrayBuffer) {
|
112
|
message = new Uint8Array(message);
|
113
|
}
|
114
|
}
|
115
|
if (Array.isArray(message) || ArrayBuffer.isView(message) ||
|
116
|
message.constructor === Buffer) {
|
117
|
return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
|
118
|
} else {
|
119
|
return method(message);
|
120
|
}
|
121
|
};
|
122
|
return nodeMethod;
|
123
|
};
|
124
|
|
125
|
var createHmacOutputMethod = function (outputType, is224) {
|
126
|
return function (key, message) {
|
127
|
return new HmacSha256(key, is224, true).update(message)[outputType]();
|
128
|
};
|
129
|
};
|
130
|
|
131
|
var createHmacMethod = function (is224) {
|
132
|
var method = createHmacOutputMethod('hex', is224);
|
133
|
method.create = function (key) {
|
134
|
return new HmacSha256(key, is224);
|
135
|
};
|
136
|
method.update = function (key, message) {
|
137
|
return method.create(key).update(message);
|
138
|
};
|
139
|
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
140
|
var type = OUTPUT_TYPES[i];
|
141
|
method[type] = createHmacOutputMethod(type, is224);
|
142
|
}
|
143
|
return method;
|
144
|
};
|
145
|
|
146
|
function Sha256(is224, sharedMemory) {
|
147
|
if (sharedMemory) {
|
148
|
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
149
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
150
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
151
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
152
|
this.blocks = blocks;
|
153
|
} else {
|
154
|
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
155
|
}
|
156
|
|
157
|
if (is224) {
|
158
|
this.h0 = 0xc1059ed8;
|
159
|
this.h1 = 0x367cd507;
|
160
|
this.h2 = 0x3070dd17;
|
161
|
this.h3 = 0xf70e5939;
|
162
|
this.h4 = 0xffc00b31;
|
163
|
this.h5 = 0x68581511;
|
164
|
this.h6 = 0x64f98fa7;
|
165
|
this.h7 = 0xbefa4fa4;
|
166
|
} else { // 256
|
167
|
this.h0 = 0x6a09e667;
|
168
|
this.h1 = 0xbb67ae85;
|
169
|
this.h2 = 0x3c6ef372;
|
170
|
this.h3 = 0xa54ff53a;
|
171
|
this.h4 = 0x510e527f;
|
172
|
this.h5 = 0x9b05688c;
|
173
|
this.h6 = 0x1f83d9ab;
|
174
|
this.h7 = 0x5be0cd19;
|
175
|
}
|
176
|
|
177
|
this.block = this.start = this.bytes = this.hBytes = 0;
|
178
|
this.finalized = this.hashed = false;
|
179
|
this.first = true;
|
180
|
this.is224 = is224;
|
181
|
}
|
182
|
|
183
|
Sha256.prototype.update = function (message) {
|
184
|
if (this.finalized) {
|
185
|
return;
|
186
|
}
|
187
|
var notString, type = typeof message;
|
188
|
if (type !== 'string') {
|
189
|
if (type === 'object') {
|
190
|
if (message === null) {
|
191
|
throw new Error(ERROR);
|
192
|
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
|
193
|
message = new Uint8Array(message);
|
194
|
} else if (!Array.isArray(message)) {
|
195
|
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
|
196
|
throw new Error(ERROR);
|
197
|
}
|
198
|
}
|
199
|
} else {
|
200
|
throw new Error(ERROR);
|
201
|
}
|
202
|
notString = true;
|
203
|
}
|
204
|
var code, index = 0, i, length = message.length, blocks = this.blocks;
|
205
|
|
206
|
while (index < length) {
|
207
|
if (this.hashed) {
|
208
|
this.hashed = false;
|
209
|
blocks[0] = this.block;
|
210
|
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
211
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
212
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
213
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
214
|
}
|
215
|
|
216
|
if (notString) {
|
217
|
for (i = this.start; index < length && i < 64; ++index) {
|
218
|
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
|
219
|
}
|
220
|
} else {
|
221
|
for (i = this.start; index < length && i < 64; ++index) {
|
222
|
code = message.charCodeAt(index);
|
223
|
if (code < 0x80) {
|
224
|
blocks[i >> 2] |= code << SHIFT[i++ & 3];
|
225
|
} else if (code < 0x800) {
|
226
|
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
|
227
|
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
228
|
} else if (code < 0xd800 || code >= 0xe000) {
|
229
|
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
|
230
|
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
231
|
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
232
|
} else {
|
233
|
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
|
234
|
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
|
235
|
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
|
236
|
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
237
|
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
238
|
}
|
239
|
}
|
240
|
}
|
241
|
|
242
|
this.lastByteIndex = i;
|
243
|
this.bytes += i - this.start;
|
244
|
if (i >= 64) {
|
245
|
this.block = blocks[16];
|
246
|
this.start = i - 64;
|
247
|
this.hash();
|
248
|
this.hashed = true;
|
249
|
} else {
|
250
|
this.start = i;
|
251
|
}
|
252
|
}
|
253
|
if (this.bytes > 4294967295) {
|
254
|
this.hBytes += this.bytes / 4294967296 << 0;
|
255
|
this.bytes = this.bytes % 4294967296;
|
256
|
}
|
257
|
return this;
|
258
|
};
|
259
|
|
260
|
Sha256.prototype.finalize = function () {
|
261
|
if (this.finalized) {
|
262
|
return;
|
263
|
}
|
264
|
this.finalized = true;
|
265
|
var blocks = this.blocks, i = this.lastByteIndex;
|
266
|
blocks[16] = this.block;
|
267
|
blocks[i >> 2] |= EXTRA[i & 3];
|
268
|
this.block = blocks[16];
|
269
|
if (i >= 56) {
|
270
|
if (!this.hashed) {
|
271
|
this.hash();
|
272
|
}
|
273
|
blocks[0] = this.block;
|
274
|
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
275
|
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
276
|
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
277
|
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
278
|
}
|
279
|
blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
|
280
|
blocks[15] = this.bytes << 3;
|
281
|
this.hash();
|
282
|
};
|
283
|
|
284
|
Sha256.prototype.hash = function () {
|
285
|
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
|
286
|
h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
|
287
|
|
288
|
for (j = 16; j < 64; ++j) {
|
289
|
// rightrotate
|
290
|
t1 = blocks[j - 15];
|
291
|
s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
|
292
|
t1 = blocks[j - 2];
|
293
|
s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
|
294
|
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
|
295
|
}
|
296
|
|
297
|
bc = b & c;
|
298
|
for (j = 0; j < 64; j += 4) {
|
299
|
if (this.first) {
|
300
|
if (this.is224) {
|
301
|
ab = 300032;
|
302
|
t1 = blocks[0] - 1413257819;
|
303
|
h = t1 - 150054599 << 0;
|
304
|
d = t1 + 24177077 << 0;
|
305
|
} else {
|
306
|
ab = 704751109;
|
307
|
t1 = blocks[0] - 210244248;
|
308
|
h = t1 - 1521486534 << 0;
|
309
|
d = t1 + 143694565 << 0;
|
310
|
}
|
311
|
this.first = false;
|
312
|
} else {
|
313
|
s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
314
|
s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
315
|
ab = a & b;
|
316
|
maj = ab ^ (a & c) ^ bc;
|
317
|
ch = (e & f) ^ (~e & g);
|
318
|
t1 = h + s1 + ch + K[j] + blocks[j];
|
319
|
t2 = s0 + maj;
|
320
|
h = d + t1 << 0;
|
321
|
d = t1 + t2 << 0;
|
322
|
}
|
323
|
s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
|
324
|
s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
|
325
|
da = d & a;
|
326
|
maj = da ^ (d & b) ^ ab;
|
327
|
ch = (h & e) ^ (~h & f);
|
328
|
t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
|
329
|
t2 = s0 + maj;
|
330
|
g = c + t1 << 0;
|
331
|
c = t1 + t2 << 0;
|
332
|
s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
|
333
|
s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
|
334
|
cd = c & d;
|
335
|
maj = cd ^ (c & a) ^ da;
|
336
|
ch = (g & h) ^ (~g & e);
|
337
|
t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
|
338
|
t2 = s0 + maj;
|
339
|
f = b + t1 << 0;
|
340
|
b = t1 + t2 << 0;
|
341
|
s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
|
342
|
s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
|
343
|
bc = b & c;
|
344
|
maj = bc ^ (b & d) ^ cd;
|
345
|
ch = (f & g) ^ (~f & h);
|
346
|
t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
|
347
|
t2 = s0 + maj;
|
348
|
e = a + t1 << 0;
|
349
|
a = t1 + t2 << 0;
|
350
|
}
|
351
|
|
352
|
this.h0 = this.h0 + a << 0;
|
353
|
this.h1 = this.h1 + b << 0;
|
354
|
this.h2 = this.h2 + c << 0;
|
355
|
this.h3 = this.h3 + d << 0;
|
356
|
this.h4 = this.h4 + e << 0;
|
357
|
this.h5 = this.h5 + f << 0;
|
358
|
this.h6 = this.h6 + g << 0;
|
359
|
this.h7 = this.h7 + h << 0;
|
360
|
};
|
361
|
|
362
|
Sha256.prototype.hex = function () {
|
363
|
this.finalize();
|
364
|
|
365
|
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
|
366
|
h6 = this.h6, h7 = this.h7;
|
367
|
|
368
|
var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
|
369
|
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
|
370
|
HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
|
371
|
HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
|
372
|
HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
|
373
|
HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
|
374
|
HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
|
375
|
HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
|
376
|
HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
|
377
|
HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
|
378
|
HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
|
379
|
HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
|
380
|
HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
|
381
|
HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
|
382
|
HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
|
383
|
HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
|
384
|
HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
|
385
|
HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
|
386
|
HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
|
387
|
HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
|
388
|
HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
|
389
|
HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
|
390
|
HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
|
391
|
HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
|
392
|
HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
|
393
|
HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
|
394
|
HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
|
395
|
HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
|
396
|
if (!this.is224) {
|
397
|
hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
|
398
|
HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
|
399
|
HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
|
400
|
HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
|
401
|
}
|
402
|
return hex;
|
403
|
};
|
404
|
|
405
|
Sha256.prototype.toString = Sha256.prototype.hex;
|
406
|
|
407
|
Sha256.prototype.digest = function () {
|
408
|
this.finalize();
|
409
|
|
410
|
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
|
411
|
h6 = this.h6, h7 = this.h7;
|
412
|
|
413
|
var arr = [
|
414
|
(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
|
415
|
(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
|
416
|
(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
|
417
|
(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
|
418
|
(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
|
419
|
(h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
|
420
|
(h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
|
421
|
];
|
422
|
if (!this.is224) {
|
423
|
arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
|
424
|
}
|
425
|
return arr;
|
426
|
};
|
427
|
|
428
|
Sha256.prototype.array = Sha256.prototype.digest;
|
429
|
|
430
|
Sha256.prototype.arrayBuffer = function () {
|
431
|
this.finalize();
|
432
|
|
433
|
var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
|
434
|
var dataView = new DataView(buffer);
|
435
|
dataView.setUint32(0, this.h0);
|
436
|
dataView.setUint32(4, this.h1);
|
437
|
dataView.setUint32(8, this.h2);
|
438
|
dataView.setUint32(12, this.h3);
|
439
|
dataView.setUint32(16, this.h4);
|
440
|
dataView.setUint32(20, this.h5);
|
441
|
dataView.setUint32(24, this.h6);
|
442
|
if (!this.is224) {
|
443
|
dataView.setUint32(28, this.h7);
|
444
|
}
|
445
|
return buffer;
|
446
|
};
|
447
|
|
448
|
function HmacSha256(key, is224, sharedMemory) {
|
449
|
var i, type = typeof key;
|
450
|
if (type === 'string') {
|
451
|
var bytes = [], length = key.length, index = 0, code;
|
452
|
for (i = 0; i < length; ++i) {
|
453
|
code = key.charCodeAt(i);
|
454
|
if (code < 0x80) {
|
455
|
bytes[index++] = code;
|
456
|
} else if (code < 0x800) {
|
457
|
bytes[index++] = (0xc0 | (code >> 6));
|
458
|
bytes[index++] = (0x80 | (code & 0x3f));
|
459
|
} else if (code < 0xd800 || code >= 0xe000) {
|
460
|
bytes[index++] = (0xe0 | (code >> 12));
|
461
|
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
462
|
bytes[index++] = (0x80 | (code & 0x3f));
|
463
|
} else {
|
464
|
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
|
465
|
bytes[index++] = (0xf0 | (code >> 18));
|
466
|
bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
|
467
|
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
468
|
bytes[index++] = (0x80 | (code & 0x3f));
|
469
|
}
|
470
|
}
|
471
|
key = bytes;
|
472
|
} else {
|
473
|
if (type === 'object') {
|
474
|
if (key === null) {
|
475
|
throw new Error(ERROR);
|
476
|
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
|
477
|
key = new Uint8Array(key);
|
478
|
} else if (!Array.isArray(key)) {
|
479
|
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
|
480
|
throw new Error(ERROR);
|
481
|
}
|
482
|
}
|
483
|
} else {
|
484
|
throw new Error(ERROR);
|
485
|
}
|
486
|
}
|
487
|
|
488
|
if (key.length > 64) {
|
489
|
key = (new Sha256(is224, true)).update(key).array();
|
490
|
}
|
491
|
|
492
|
var oKeyPad = [], iKeyPad = [];
|
493
|
for (i = 0; i < 64; ++i) {
|
494
|
var b = key[i] || 0;
|
495
|
oKeyPad[i] = 0x5c ^ b;
|
496
|
iKeyPad[i] = 0x36 ^ b;
|
497
|
}
|
498
|
|
499
|
Sha256.call(this, is224, sharedMemory);
|
500
|
|
501
|
this.update(iKeyPad);
|
502
|
this.oKeyPad = oKeyPad;
|
503
|
this.inner = true;
|
504
|
this.sharedMemory = sharedMemory;
|
505
|
}
|
506
|
HmacSha256.prototype = new Sha256();
|
507
|
|
508
|
HmacSha256.prototype.finalize = function () {
|
509
|
Sha256.prototype.finalize.call(this);
|
510
|
if (this.inner) {
|
511
|
this.inner = false;
|
512
|
var innerHash = this.array();
|
513
|
Sha256.call(this, this.is224, this.sharedMemory);
|
514
|
this.update(this.oKeyPad);
|
515
|
this.update(innerHash);
|
516
|
Sha256.prototype.finalize.call(this);
|
517
|
}
|
518
|
};
|
519
|
|
520
|
var exports = createMethod();
|
521
|
exports.sha256 = exports;
|
522
|
exports.sha224 = createMethod(true);
|
523
|
exports.sha256.hmac = createHmacMethod();
|
524
|
exports.sha224.hmac = createHmacMethod(true);
|
525
|
|
526
|
if (COMMON_JS) {
|
527
|
module.exports = exports;
|
528
|
} else {
|
529
|
root.sha256 = exports.sha256;
|
530
|
root.sha224 = exports.sha224;
|
531
|
if (AMD) {
|
532
|
define(function () {
|
533
|
return exports;
|
534
|
});
|
535
|
}
|
536
|
}
|
537
|
|
538
|
#EXPORT fake_window.sha256 AS sha256
|