2 * [js-sha256]{@link https://github.com/emn178/js-sha256}
5 * @author Chen, Yi-Cyuan [emn178@gmail.com]
6 * @copyright Chen, Yi-Cyuan 2014-2017
9 /*jslint bitwise: true */
13 var ERROR
= 'input is invalid type';
14 var WINDOW
= typeof window
=== 'object';
15 var root
= WINDOW
? window
: {};
16 if (root
.JS_SHA256_NO_WINDOW
) {
19 var WEB_WORKER
= !WINDOW
&& typeof self
=== 'object';
20 var NODE_JS
= !root
.JS_SHA256_NO_NODE_JS
&& typeof process
=== 'object' && process
.versions
&& process
.versions
.node
;
23 } else if (WEB_WORKER
) {
26 var COMMON_JS
= !root
.JS_SHA256_NO_COMMON_JS
&& typeof module
=== 'object' && module
.exports
;
27 var AMD
= typeof define
=== 'function' && define
.amd
;
28 var ARRAY_BUFFER
= !root
.JS_SHA256_NO_ARRAY_BUFFER
&& typeof ArrayBuffer
!== 'undefined';
29 var HEX_CHARS
= '0123456789abcdef'.split('');
30 var EXTRA
= [-2147483648, 8388608, 32768, 128];
31 var SHIFT
= [24, 16, 8, 0];
33 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
34 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
35 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
36 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
37 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
38 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
39 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
40 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
42 var OUTPUT_TYPES
= ['hex', 'array', 'digest', 'arrayBuffer'];
46 if (root
.JS_SHA256_NO_NODE_JS
|| !Array
.isArray
) {
47 Array
.isArray = function (obj
) {
48 return Object
.prototype.toString
.call(obj
) === '[object Array]';
52 if (ARRAY_BUFFER
&& (root
.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW
|| !ArrayBuffer
.isView
)) {
53 ArrayBuffer
.isView = function (obj
) {
54 return typeof obj
=== 'object' && obj
.buffer
&& obj
.buffer
.constructor === ArrayBuffer
;
58 var createOutputMethod = function (outputType
, is224
) {
59 return function (message
) {
60 return new Sha256(is224
, true).update(message
)[outputType
]();
64 var createMethod = function (is224
) {
65 var method
= createOutputMethod('hex', is224
);
67 method
= nodeWrap(method
, is224
);
69 method
.create = function () {
70 return new Sha256(is224
);
72 method
.update = function (message
) {
73 return method
.create().update(message
);
75 for (var i
= 0; i
< OUTPUT_TYPES
.length
; ++i
) {
76 var type
= OUTPUT_TYPES
[i
];
77 method
[type
] = createOutputMethod(type
, is224
);
82 var nodeWrap = function (method
, is224
) {
83 var crypto
= eval("require('crypto')");
84 var Buffer
= eval("require('buffer').Buffer");
85 var algorithm
= is224
? 'sha224' : 'sha256';
86 var nodeMethod = function (message
) {
87 if (typeof message
=== 'string') {
88 return crypto
.createHash(algorithm
).update(message
, 'utf8').digest('hex');
90 if (message
=== null || message
=== undefined) {
91 throw new Error(ERROR
);
92 } else if (message
.constructor === ArrayBuffer
) {
93 message
= new Uint8Array(message
);
96 if (Array
.isArray(message
) || ArrayBuffer
.isView(message
) ||
97 message
.constructor === Buffer
) {
98 return crypto
.createHash(algorithm
).update(new Buffer(message
)).digest('hex');
100 return method(message
);
106 var createHmacOutputMethod = function (outputType
, is224
) {
107 return function (key
, message
) {
108 return new HmacSha256(key
, is224
, true).update(message
)[outputType
]();
112 var createHmacMethod = function (is224
) {
113 var method
= createHmacOutputMethod('hex', is224
);
114 method
.create = function (key
) {
115 return new HmacSha256(key
, is224
);
117 method
.update = function (key
, message
) {
118 return method
.create(key
).update(message
);
120 for (var i
= 0; i
< OUTPUT_TYPES
.length
; ++i
) {
121 var type
= OUTPUT_TYPES
[i
];
122 method
[type
] = createHmacOutputMethod(type
, is224
);
127 function Sha256(is224
, sharedMemory
) {
129 blocks
[0] = blocks
[16] = blocks
[1] = blocks
[2] = blocks
[3] =
130 blocks
[4] = blocks
[5] = blocks
[6] = blocks
[7] =
131 blocks
[8] = blocks
[9] = blocks
[10] = blocks
[11] =
132 blocks
[12] = blocks
[13] = blocks
[14] = blocks
[15] = 0;
133 this.blocks
= blocks
;
135 this.blocks
= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
139 this.h0
= 0xc1059ed8;
140 this.h1
= 0x367cd507;
141 this.h2
= 0x3070dd17;
142 this.h3
= 0xf70e5939;
143 this.h4
= 0xffc00b31;
144 this.h5
= 0x68581511;
145 this.h6
= 0x64f98fa7;
146 this.h7
= 0xbefa4fa4;
148 this.h0
= 0x6a09e667;
149 this.h1
= 0xbb67ae85;
150 this.h2
= 0x3c6ef372;
151 this.h3
= 0xa54ff53a;
152 this.h4
= 0x510e527f;
153 this.h5
= 0x9b05688c;
154 this.h6
= 0x1f83d9ab;
155 this.h7
= 0x5be0cd19;
158 this.block
= this.start
= this.bytes
= this.hBytes
= 0;
159 this.finalized
= this.hashed
= false;
164 Sha256
.prototype.update = function (message
) {
165 if (this.finalized
) {
168 var notString
, type
= typeof message
;
169 if (type
!== 'string') {
170 if (type
=== 'object') {
171 if (message
=== null) {
172 throw new Error(ERROR
);
173 } else if (ARRAY_BUFFER
&& message
.constructor === ArrayBuffer
) {
174 message
= new Uint8Array(message
);
175 } else if (!Array
.isArray(message
)) {
176 if (!ARRAY_BUFFER
|| !ArrayBuffer
.isView(message
)) {
177 throw new Error(ERROR
);
181 throw new Error(ERROR
);
185 var code
, index
= 0, i
, length
= message
.length
, blocks
= this.blocks
;
187 while (index
< length
) {
190 blocks
[0] = this.block
;
191 blocks
[16] = blocks
[1] = blocks
[2] = blocks
[3] =
192 blocks
[4] = blocks
[5] = blocks
[6] = blocks
[7] =
193 blocks
[8] = blocks
[9] = blocks
[10] = blocks
[11] =
194 blocks
[12] = blocks
[13] = blocks
[14] = blocks
[15] = 0;
198 for (i
= this.start
; index
< length
&& i
< 64; ++index
) {
199 blocks
[i
>> 2] |= message
[index
] << SHIFT
[i
++ & 3];
202 for (i
= this.start
; index
< length
&& i
< 64; ++index
) {
203 code
= message
.charCodeAt(index
);
205 blocks
[i
>> 2] |= code
<< SHIFT
[i
++ & 3];
206 } else if (code
< 0x800) {
207 blocks
[i
>> 2] |= (0xc0 | (code
>> 6)) << SHIFT
[i
++ & 3];
208 blocks
[i
>> 2] |= (0x80 | (code
& 0x3f)) << SHIFT
[i
++ & 3];
209 } else if (code
< 0xd800 || code
>= 0xe000) {
210 blocks
[i
>> 2] |= (0xe0 | (code
>> 12)) << SHIFT
[i
++ & 3];
211 blocks
[i
>> 2] |= (0x80 | ((code
>> 6) & 0x3f)) << SHIFT
[i
++ & 3];
212 blocks
[i
>> 2] |= (0x80 | (code
& 0x3f)) << SHIFT
[i
++ & 3];
214 code
= 0x10000 + (((code
& 0x3ff) << 10) | (message
.charCodeAt(++index
) & 0x3ff));
215 blocks
[i
>> 2] |= (0xf0 | (code
>> 18)) << SHIFT
[i
++ & 3];
216 blocks
[i
>> 2] |= (0x80 | ((code
>> 12) & 0x3f)) << SHIFT
[i
++ & 3];
217 blocks
[i
>> 2] |= (0x80 | ((code
>> 6) & 0x3f)) << SHIFT
[i
++ & 3];
218 blocks
[i
>> 2] |= (0x80 | (code
& 0x3f)) << SHIFT
[i
++ & 3];
223 this.lastByteIndex
= i
;
224 this.bytes
+= i
- this.start
;
226 this.block
= blocks
[16];
234 if (this.bytes
> 4294967295) {
235 this.hBytes
+= this.bytes
/ 4294967296 << 0;
236 this.bytes
= this.bytes
% 4294967296;
241 Sha256
.prototype.finalize = function () {
242 if (this.finalized
) {
245 this.finalized
= true;
246 var blocks
= this.blocks
, i
= this.lastByteIndex
;
247 blocks
[16] = this.block
;
248 blocks
[i
>> 2] |= EXTRA
[i
& 3];
249 this.block
= blocks
[16];
254 blocks
[0] = this.block
;
255 blocks
[16] = blocks
[1] = blocks
[2] = blocks
[3] =
256 blocks
[4] = blocks
[5] = blocks
[6] = blocks
[7] =
257 blocks
[8] = blocks
[9] = blocks
[10] = blocks
[11] =
258 blocks
[12] = blocks
[13] = blocks
[14] = blocks
[15] = 0;
260 blocks
[14] = this.hBytes
<< 3 | this.bytes
>>> 29;
261 blocks
[15] = this.bytes
<< 3;
265 Sha256
.prototype.hash = function () {
266 var a
= this.h0
, b
= this.h1
, c
= this.h2
, d
= this.h3
, e
= this.h4
, f
= this.h5
, g
= this.h6
,
267 h
= this.h7
, blocks
= this.blocks
, j
, s0
, s1
, maj
, t1
, t2
, ch
, ab
, da
, cd
, bc
;
269 for (j
= 16; j
< 64; ++j
) {
272 s0
= ((t1
>>> 7) | (t1
<< 25)) ^ ((t1
>>> 18) | (t1
<< 14)) ^ (t1
>>> 3);
274 s1
= ((t1
>>> 17) | (t1
<< 15)) ^ ((t1
>>> 19) | (t1
<< 13)) ^ (t1
>>> 10);
275 blocks
[j
] = blocks
[j
- 16] + s0
+ blocks
[j
- 7] + s1
<< 0;
279 for (j
= 0; j
< 64; j
+= 4) {
283 t1
= blocks
[0] - 1413257819;
284 h
= t1
- 150054599 << 0;
285 d
= t1
+ 24177077 << 0;
288 t1
= blocks
[0] - 210244248;
289 h
= t1
- 1521486534 << 0;
290 d
= t1
+ 143694565 << 0;
294 s0
= ((a
>>> 2) | (a
<< 30)) ^ ((a
>>> 13) | (a
<< 19)) ^ ((a
>>> 22) | (a
<< 10));
295 s1
= ((e
>>> 6) | (e
<< 26)) ^ ((e
>>> 11) | (e
<< 21)) ^ ((e
>>> 25) | (e
<< 7));
297 maj
= ab
^ (a
& c
) ^ bc
;
298 ch
= (e
& f
) ^ (~e
& g
);
299 t1
= h
+ s1
+ ch
+ K
[j
] + blocks
[j
];
304 s0
= ((d
>>> 2) | (d
<< 30)) ^ ((d
>>> 13) | (d
<< 19)) ^ ((d
>>> 22) | (d
<< 10));
305 s1
= ((h
>>> 6) | (h
<< 26)) ^ ((h
>>> 11) | (h
<< 21)) ^ ((h
>>> 25) | (h
<< 7));
307 maj
= da
^ (d
& b
) ^ ab
;
308 ch
= (h
& e
) ^ (~h
& f
);
309 t1
= g
+ s1
+ ch
+ K
[j
+ 1] + blocks
[j
+ 1];
313 s0
= ((c
>>> 2) | (c
<< 30)) ^ ((c
>>> 13) | (c
<< 19)) ^ ((c
>>> 22) | (c
<< 10));
314 s1
= ((g
>>> 6) | (g
<< 26)) ^ ((g
>>> 11) | (g
<< 21)) ^ ((g
>>> 25) | (g
<< 7));
316 maj
= cd
^ (c
& a
) ^ da
;
317 ch
= (g
& h
) ^ (~g
& e
);
318 t1
= f
+ s1
+ ch
+ K
[j
+ 2] + blocks
[j
+ 2];
322 s0
= ((b
>>> 2) | (b
<< 30)) ^ ((b
>>> 13) | (b
<< 19)) ^ ((b
>>> 22) | (b
<< 10));
323 s1
= ((f
>>> 6) | (f
<< 26)) ^ ((f
>>> 11) | (f
<< 21)) ^ ((f
>>> 25) | (f
<< 7));
325 maj
= bc
^ (b
& d
) ^ cd
;
326 ch
= (f
& g
) ^ (~f
& h
);
327 t1
= e
+ s1
+ ch
+ K
[j
+ 3] + blocks
[j
+ 3];
333 this.h0
= this.h0
+ a
<< 0;
334 this.h1
= this.h1
+ b
<< 0;
335 this.h2
= this.h2
+ c
<< 0;
336 this.h3
= this.h3
+ d
<< 0;
337 this.h4
= this.h4
+ e
<< 0;
338 this.h5
= this.h5
+ f
<< 0;
339 this.h6
= this.h6
+ g
<< 0;
340 this.h7
= this.h7
+ h
<< 0;
343 Sha256
.prototype.hex = function () {
346 var h0
= this.h0
, h1
= this.h1
, h2
= this.h2
, h3
= this.h3
, h4
= this.h4
, h5
= this.h5
,
347 h6
= this.h6
, h7
= this.h7
;
349 var hex
= HEX_CHARS
[(h0
>> 28) & 0x0F] + HEX_CHARS
[(h0
>> 24) & 0x0F] +
350 HEX_CHARS
[(h0
>> 20) & 0x0F] + HEX_CHARS
[(h0
>> 16) & 0x0F] +
351 HEX_CHARS
[(h0
>> 12) & 0x0F] + HEX_CHARS
[(h0
>> 8) & 0x0F] +
352 HEX_CHARS
[(h0
>> 4) & 0x0F] + HEX_CHARS
[h0
& 0x0F] +
353 HEX_CHARS
[(h1
>> 28) & 0x0F] + HEX_CHARS
[(h1
>> 24) & 0x0F] +
354 HEX_CHARS
[(h1
>> 20) & 0x0F] + HEX_CHARS
[(h1
>> 16) & 0x0F] +
355 HEX_CHARS
[(h1
>> 12) & 0x0F] + HEX_CHARS
[(h1
>> 8) & 0x0F] +
356 HEX_CHARS
[(h1
>> 4) & 0x0F] + HEX_CHARS
[h1
& 0x0F] +
357 HEX_CHARS
[(h2
>> 28) & 0x0F] + HEX_CHARS
[(h2
>> 24) & 0x0F] +
358 HEX_CHARS
[(h2
>> 20) & 0x0F] + HEX_CHARS
[(h2
>> 16) & 0x0F] +
359 HEX_CHARS
[(h2
>> 12) & 0x0F] + HEX_CHARS
[(h2
>> 8) & 0x0F] +
360 HEX_CHARS
[(h2
>> 4) & 0x0F] + HEX_CHARS
[h2
& 0x0F] +
361 HEX_CHARS
[(h3
>> 28) & 0x0F] + HEX_CHARS
[(h3
>> 24) & 0x0F] +
362 HEX_CHARS
[(h3
>> 20) & 0x0F] + HEX_CHARS
[(h3
>> 16) & 0x0F] +
363 HEX_CHARS
[(h3
>> 12) & 0x0F] + HEX_CHARS
[(h3
>> 8) & 0x0F] +
364 HEX_CHARS
[(h3
>> 4) & 0x0F] + HEX_CHARS
[h3
& 0x0F] +
365 HEX_CHARS
[(h4
>> 28) & 0x0F] + HEX_CHARS
[(h4
>> 24) & 0x0F] +
366 HEX_CHARS
[(h4
>> 20) & 0x0F] + HEX_CHARS
[(h4
>> 16) & 0x0F] +
367 HEX_CHARS
[(h4
>> 12) & 0x0F] + HEX_CHARS
[(h4
>> 8) & 0x0F] +
368 HEX_CHARS
[(h4
>> 4) & 0x0F] + HEX_CHARS
[h4
& 0x0F] +
369 HEX_CHARS
[(h5
>> 28) & 0x0F] + HEX_CHARS
[(h5
>> 24) & 0x0F] +
370 HEX_CHARS
[(h5
>> 20) & 0x0F] + HEX_CHARS
[(h5
>> 16) & 0x0F] +
371 HEX_CHARS
[(h5
>> 12) & 0x0F] + HEX_CHARS
[(h5
>> 8) & 0x0F] +
372 HEX_CHARS
[(h5
>> 4) & 0x0F] + HEX_CHARS
[h5
& 0x0F] +
373 HEX_CHARS
[(h6
>> 28) & 0x0F] + HEX_CHARS
[(h6
>> 24) & 0x0F] +
374 HEX_CHARS
[(h6
>> 20) & 0x0F] + HEX_CHARS
[(h6
>> 16) & 0x0F] +
375 HEX_CHARS
[(h6
>> 12) & 0x0F] + HEX_CHARS
[(h6
>> 8) & 0x0F] +
376 HEX_CHARS
[(h6
>> 4) & 0x0F] + HEX_CHARS
[h6
& 0x0F];
378 hex
+= HEX_CHARS
[(h7
>> 28) & 0x0F] + HEX_CHARS
[(h7
>> 24) & 0x0F] +
379 HEX_CHARS
[(h7
>> 20) & 0x0F] + HEX_CHARS
[(h7
>> 16) & 0x0F] +
380 HEX_CHARS
[(h7
>> 12) & 0x0F] + HEX_CHARS
[(h7
>> 8) & 0x0F] +
381 HEX_CHARS
[(h7
>> 4) & 0x0F] + HEX_CHARS
[h7
& 0x0F];
386 Sha256
.prototype.toString
= Sha256
.prototype.hex
;
388 Sha256
.prototype.digest = function () {
391 var h0
= this.h0
, h1
= this.h1
, h2
= this.h2
, h3
= this.h3
, h4
= this.h4
, h5
= this.h5
,
392 h6
= this.h6
, h7
= this.h7
;
395 (h0
>> 24) & 0xFF, (h0
>> 16) & 0xFF, (h0
>> 8) & 0xFF, h0
& 0xFF,
396 (h1
>> 24) & 0xFF, (h1
>> 16) & 0xFF, (h1
>> 8) & 0xFF, h1
& 0xFF,
397 (h2
>> 24) & 0xFF, (h2
>> 16) & 0xFF, (h2
>> 8) & 0xFF, h2
& 0xFF,
398 (h3
>> 24) & 0xFF, (h3
>> 16) & 0xFF, (h3
>> 8) & 0xFF, h3
& 0xFF,
399 (h4
>> 24) & 0xFF, (h4
>> 16) & 0xFF, (h4
>> 8) & 0xFF, h4
& 0xFF,
400 (h5
>> 24) & 0xFF, (h5
>> 16) & 0xFF, (h5
>> 8) & 0xFF, h5
& 0xFF,
401 (h6
>> 24) & 0xFF, (h6
>> 16) & 0xFF, (h6
>> 8) & 0xFF, h6
& 0xFF
404 arr
.push((h7
>> 24) & 0xFF, (h7
>> 16) & 0xFF, (h7
>> 8) & 0xFF, h7
& 0xFF);
409 Sha256
.prototype.array
= Sha256
.prototype.digest
;
411 Sha256
.prototype.arrayBuffer = function () {
414 var buffer
= new ArrayBuffer(this.is224
? 28 : 32);
415 var dataView
= new DataView(buffer
);
416 dataView
.setUint32(0, this.h0
);
417 dataView
.setUint32(4, this.h1
);
418 dataView
.setUint32(8, this.h2
);
419 dataView
.setUint32(12, this.h3
);
420 dataView
.setUint32(16, this.h4
);
421 dataView
.setUint32(20, this.h5
);
422 dataView
.setUint32(24, this.h6
);
424 dataView
.setUint32(28, this.h7
);
429 function HmacSha256(key
, is224
, sharedMemory
) {
430 var i
, type
= typeof key
;
431 if (type
=== 'string') {
432 var bytes
= [], length
= key
.length
, index
= 0, code
;
433 for (i
= 0; i
< length
; ++i
) {
434 code
= key
.charCodeAt(i
);
436 bytes
[index
++] = code
;
437 } else if (code
< 0x800) {
438 bytes
[index
++] = (0xc0 | (code
>> 6));
439 bytes
[index
++] = (0x80 | (code
& 0x3f));
440 } else if (code
< 0xd800 || code
>= 0xe000) {
441 bytes
[index
++] = (0xe0 | (code
>> 12));
442 bytes
[index
++] = (0x80 | ((code
>> 6) & 0x3f));
443 bytes
[index
++] = (0x80 | (code
& 0x3f));
445 code
= 0x10000 + (((code
& 0x3ff) << 10) | (key
.charCodeAt(++i
) & 0x3ff));
446 bytes
[index
++] = (0xf0 | (code
>> 18));
447 bytes
[index
++] = (0x80 | ((code
>> 12) & 0x3f));
448 bytes
[index
++] = (0x80 | ((code
>> 6) & 0x3f));
449 bytes
[index
++] = (0x80 | (code
& 0x3f));
454 if (type
=== 'object') {
456 throw new Error(ERROR
);
457 } else if (ARRAY_BUFFER
&& key
.constructor === ArrayBuffer
) {
458 key
= new Uint8Array(key
);
459 } else if (!Array
.isArray(key
)) {
460 if (!ARRAY_BUFFER
|| !ArrayBuffer
.isView(key
)) {
461 throw new Error(ERROR
);
465 throw new Error(ERROR
);
469 if (key
.length
> 64) {
470 key
= (new Sha256(is224
, true)).update(key
).array();
473 var oKeyPad
= [], iKeyPad
= [];
474 for (i
= 0; i
< 64; ++i
) {
476 oKeyPad
[i
] = 0x5c ^ b
;
477 iKeyPad
[i
] = 0x36 ^ b
;
480 Sha256
.call(this, is224
, sharedMemory
);
482 this.update(iKeyPad
);
483 this.oKeyPad
= oKeyPad
;
485 this.sharedMemory
= sharedMemory
;
487 HmacSha256
.prototype = new Sha256();
489 HmacSha256
.prototype.finalize = function () {
490 Sha256
.prototype.finalize
.call(this);
493 var innerHash
= this.array();
494 Sha256
.call(this, this.is224
, this.sharedMemory
);
495 this.update(this.oKeyPad
);
496 this.update(innerHash
);
497 Sha256
.prototype.finalize
.call(this);
501 var exports
= createMethod();
502 exports
.sha256
= exports
;
503 exports
.sha224
= createMethod(true);
504 exports
.sha256
.hmac
= createHmacMethod();
505 exports
.sha224
.hmac
= createHmacMethod(true);
508 module
.exports
= exports
;
510 root
.sha256
= exports
.sha256
;
511 root
.sha224
= exports
.sha224
;