Project

General

Profile

« Previous | Next » 

Revision 261548ff

Added by koszko about 2 years ago

emply an sh-based build system; make some changes to blocking

View differences:

common/sha256.js
7 7
 * @license MIT
8 8
 */
9 9

  
10
"use strict";
10
var fake_window = {};
11 11

  
12
(() => {
13
  var fake_window = {};
14

  
15
  console.log('hello, crypto!');
16
  var ERROR = 'input is invalid type';
17
  var WINDOW = typeof window === 'object';
18
  var root = /*WINDOW ? window : {}*/ fake_window;
19
  if (root.JS_SHA256_NO_WINDOW) {
12
var ERROR = 'input is invalid type';
13
var WINDOW = typeof fake_window === 'object';
14
var root = /*WINDOW ? window : {}*/ fake_window;
15
if (root.JS_SHA256_NO_WINDOW) {
20 16
    WINDOW = false;
21
  }
22
  var WEB_WORKER = !WINDOW && typeof self === 'object';
23
  var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
24
  if (NODE_JS) {
17
}
18
var WEB_WORKER = !WINDOW && typeof self === 'object';
19
var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
20
if (NODE_JS) {
25 21
    root = global;
26
  } else if (WEB_WORKER) {
22
} else if (WEB_WORKER) {
27 23
    root = self;
28
  }
29
  var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
30
  var AMD = typeof define === 'function' && define.amd;
31
  var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
32
  var HEX_CHARS = '0123456789abcdef'.split('');
33
  var EXTRA = [-2147483648, 8388608, 32768, 128];
34
  var SHIFT = [24, 16, 8, 0];
35
  var K = [
24
}
25
var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
26
var AMD = typeof define === 'function' && define.amd;
27
var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
28
var HEX_CHARS = '0123456789abcdef'.split('');
29
var EXTRA = [-2147483648, 8388608, 32768, 128];
30
var SHIFT = [24, 16, 8, 0];
31
var K = [
36 32
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
37 33
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
38 34
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
......
41 37
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
42 38
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
43 39
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
44
  ];
45
  var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
40
];
41
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
46 42

  
47
  var blocks = [];
43
var blocks = [];
48 44

  
49
  if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
45
if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
50 46
    Array.isArray = function (obj) {
51
      return Object.prototype.toString.call(obj) === '[object Array]';
47
	return Object.prototype.toString.call(obj) === '[object Array]';
52 48
    };
53
  }
49
}
54 50

  
55
  if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
51
if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
56 52
    ArrayBuffer.isView = function (obj) {
57
      return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
53
	return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
58 54
    };
59
  }
55
}
60 56

  
61
  var createOutputMethod = function (outputType, is224) {
57
var createOutputMethod = function (outputType, is224) {
62 58
    return function (message) {
63
      return new Sha256(is224, true).update(message)[outputType]();
59
	return new Sha256(is224, true).update(message)[outputType]();
64 60
    };
65
  };
61
};
66 62

  
67
  var createMethod = function (is224) {
63
var createMethod = function (is224) {
68 64
    var method = createOutputMethod('hex', is224);
69 65
    if (NODE_JS) {
70
      method = nodeWrap(method, is224);
66
	method = nodeWrap(method, is224);
71 67
    }
72 68
    method.create = function () {
73
      return new Sha256(is224);
69
	return new Sha256(is224);
74 70
    };
75 71
    method.update = function (message) {
76
      return method.create().update(message);
72
	return method.create().update(message);
77 73
    };
78 74
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
79
      var type = OUTPUT_TYPES[i];
80
      method[type] = createOutputMethod(type, is224);
75
	var type = OUTPUT_TYPES[i];
76
	method[type] = createOutputMethod(type, is224);
81 77
    }
82 78
    return method;
83
  };
79
};
84 80

  
85
  var nodeWrap = function (method, is224) {
81
var nodeWrap = function (method, is224) {
86 82
    var crypto = eval("require('crypto')");
87 83
    var Buffer = eval("require('buffer').Buffer");
88 84
    var algorithm = is224 ? 'sha224' : 'sha256';
89 85
    var nodeMethod = function (message) {
90
      if (typeof message === 'string') {
91
        return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
92
      } else {
93
        if (message === null || message === undefined) {
94
          throw new Error(ERROR);
95
        } else if (message.constructor === ArrayBuffer) {
96
          message = new Uint8Array(message);
97
        }
98
      }
99
      if (Array.isArray(message) || ArrayBuffer.isView(message) ||
100
        message.constructor === Buffer) {
101
        return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
102
      } else {
103
        return method(message);
104
      }
86
	if (typeof message === 'string') {
87
            return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
88
	} else {
89
            if (message === null || message === undefined) {
90
		throw new Error(ERROR);
91
            } else if (message.constructor === ArrayBuffer) {
92
		message = new Uint8Array(message);
93
            }
94
	}
95
	if (Array.isArray(message) || ArrayBuffer.isView(message) ||
96
            message.constructor === Buffer) {
97
            return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
98
	} else {
99
            return method(message);
100
	}
105 101
    };
106 102
    return nodeMethod;
107
  };
103
};
108 104

  
109
  var createHmacOutputMethod = function (outputType, is224) {
105
var createHmacOutputMethod = function (outputType, is224) {
110 106
    return function (key, message) {
111
      return new HmacSha256(key, is224, true).update(message)[outputType]();
107
	return new HmacSha256(key, is224, true).update(message)[outputType]();
112 108
    };
113
  };
109
};
114 110

  
115
  var createHmacMethod = function (is224) {
111
var createHmacMethod = function (is224) {
116 112
    var method = createHmacOutputMethod('hex', is224);
117 113
    method.create = function (key) {
118
      return new HmacSha256(key, is224);
114
	return new HmacSha256(key, is224);
119 115
    };
120 116
    method.update = function (key, message) {
121
      return method.create(key).update(message);
117
	return method.create(key).update(message);
122 118
    };
123 119
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
124
      var type = OUTPUT_TYPES[i];
125
      method[type] = createHmacOutputMethod(type, is224);
120
	var type = OUTPUT_TYPES[i];
121
	method[type] = createHmacOutputMethod(type, is224);
126 122
    }
127 123
    return method;
128
  };
124
};
129 125

  
130
  function Sha256(is224, sharedMemory) {
126
function Sha256(is224, sharedMemory) {
131 127
    if (sharedMemory) {
132
      blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
133
        blocks[4] = blocks[5] = blocks[6] = blocks[7] =
134
        blocks[8] = blocks[9] = blocks[10] = blocks[11] =
135
        blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
136
      this.blocks = blocks;
128
	blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
129
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
130
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
131
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
132
	this.blocks = blocks;
137 133
    } else {
138
      this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
134
	this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
139 135
    }
140 136

  
141 137
    if (is224) {
142
      this.h0 = 0xc1059ed8;
143
      this.h1 = 0x367cd507;
144
      this.h2 = 0x3070dd17;
145
      this.h3 = 0xf70e5939;
146
      this.h4 = 0xffc00b31;
147
      this.h5 = 0x68581511;
148
      this.h6 = 0x64f98fa7;
149
      this.h7 = 0xbefa4fa4;
138
	this.h0 = 0xc1059ed8;
139
	this.h1 = 0x367cd507;
140
	this.h2 = 0x3070dd17;
141
	this.h3 = 0xf70e5939;
142
	this.h4 = 0xffc00b31;
143
	this.h5 = 0x68581511;
144
	this.h6 = 0x64f98fa7;
145
	this.h7 = 0xbefa4fa4;
150 146
    } else { // 256
151
      this.h0 = 0x6a09e667;
152
      this.h1 = 0xbb67ae85;
153
      this.h2 = 0x3c6ef372;
154
      this.h3 = 0xa54ff53a;
155
      this.h4 = 0x510e527f;
156
      this.h5 = 0x9b05688c;
157
      this.h6 = 0x1f83d9ab;
158
      this.h7 = 0x5be0cd19;
147
	this.h0 = 0x6a09e667;
148
	this.h1 = 0xbb67ae85;
149
	this.h2 = 0x3c6ef372;
150
	this.h3 = 0xa54ff53a;
151
	this.h4 = 0x510e527f;
152
	this.h5 = 0x9b05688c;
153
	this.h6 = 0x1f83d9ab;
154
	this.h7 = 0x5be0cd19;
159 155
    }
160 156

  
161 157
    this.block = this.start = this.bytes = this.hBytes = 0;
162 158
    this.finalized = this.hashed = false;
163 159
    this.first = true;
164 160
    this.is224 = is224;
165
  }
161
}
166 162

  
167
  Sha256.prototype.update = function (message) {
163
Sha256.prototype.update = function (message) {
168 164
    if (this.finalized) {
169
      return;
165
	return;
170 166
    }
171 167
    var notString, type = typeof message;
172 168
    if (type !== 'string') {
173
      if (type === 'object') {
174
        if (message === null) {
175
          throw new Error(ERROR);
176
        } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
177
          message = new Uint8Array(message);
178
        } else if (!Array.isArray(message)) {
179
          if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
169
	if (type === 'object') {
170
            if (message === null) {
171
		throw new Error(ERROR);
172
            } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
173
		message = new Uint8Array(message);
174
            } else if (!Array.isArray(message)) {
175
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
176
		    throw new Error(ERROR);
177
		}
178
            }
179
	} else {
180 180
            throw new Error(ERROR);
181
          }
182
        }
183
      } else {
184
        throw new Error(ERROR);
185
      }
186
      notString = true;
181
	}
182
	notString = true;
187 183
    }
188 184
    var code, index = 0, i, length = message.length, blocks = this.blocks;
189 185

  
190 186
    while (index < length) {
191
      if (this.hashed) {
192
        this.hashed = false;
193
        blocks[0] = this.block;
194
        blocks[16] = blocks[1] = blocks[2] = blocks[3] =
195
          blocks[4] = blocks[5] = blocks[6] = blocks[7] =
196
          blocks[8] = blocks[9] = blocks[10] = blocks[11] =
197
          blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
198
      }
199

  
200
      if (notString) {
201
        for (i = this.start; index < length && i < 64; ++index) {
202
          blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
203
        }
204
      } else {
205
        for (i = this.start; index < length && i < 64; ++index) {
206
          code = message.charCodeAt(index);
207
          if (code < 0x80) {
208
            blocks[i >> 2] |= code << SHIFT[i++ & 3];
209
          } else if (code < 0x800) {
210
            blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
211
            blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
212
          } else if (code < 0xd800 || code >= 0xe000) {
213
            blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
214
            blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
215
            blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
216
          } else {
217
            code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
218
            blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
219
            blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
220
            blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
221
            blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
222
          }
223
        }
224
      }
225

  
226
      this.lastByteIndex = i;
227
      this.bytes += i - this.start;
228
      if (i >= 64) {
229
        this.block = blocks[16];
230
        this.start = i - 64;
231
        this.hash();
232
        this.hashed = true;
233
      } else {
234
        this.start = i;
235
      }
187
	if (this.hashed) {
188
            this.hashed = false;
189
            blocks[0] = this.block;
190
            blocks[16] = blocks[1] = blocks[2] = blocks[3] =
191
		blocks[4] = blocks[5] = blocks[6] = blocks[7] =
192
		blocks[8] = blocks[9] = blocks[10] = blocks[11] =
193
		blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
194
	}
195

  
196
	if (notString) {
197
            for (i = this.start; index < length && i < 64; ++index) {
198
		blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
199
            }
200
	} else {
201
            for (i = this.start; index < length && i < 64; ++index) {
202
		code = message.charCodeAt(index);
203
		if (code < 0x80) {
204
		    blocks[i >> 2] |= code << SHIFT[i++ & 3];
205
		} else if (code < 0x800) {
206
		    blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
207
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
208
		} else if (code < 0xd800 || code >= 0xe000) {
209
		    blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
210
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
211
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
212
		} else {
213
		    code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
214
		    blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
215
		    blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
216
		    blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
217
		    blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
218
		}
219
            }
220
	}
221

  
222
	this.lastByteIndex = i;
223
	this.bytes += i - this.start;
224
	if (i >= 64) {
225
            this.block = blocks[16];
226
            this.start = i - 64;
227
            this.hash();
228
            this.hashed = true;
229
	} else {
230
            this.start = i;
231
	}
236 232
    }
237 233
    if (this.bytes > 4294967295) {
238
      this.hBytes += this.bytes / 4294967296 << 0;
239
      this.bytes = this.bytes % 4294967296;
234
	this.hBytes += this.bytes / 4294967296 << 0;
235
	this.bytes = this.bytes % 4294967296;
240 236
    }
241 237
    return this;
242
  };
238
};
243 239

  
244
  Sha256.prototype.finalize = function () {
240
Sha256.prototype.finalize = function () {
245 241
    if (this.finalized) {
246
      return;
242
	return;
247 243
    }
248 244
    this.finalized = true;
249 245
    var blocks = this.blocks, i = this.lastByteIndex;
......
251 247
    blocks[i >> 2] |= EXTRA[i & 3];
252 248
    this.block = blocks[16];
253 249
    if (i >= 56) {
254
      if (!this.hashed) {
255
        this.hash();
256
      }
257
      blocks[0] = this.block;
258
      blocks[16] = blocks[1] = blocks[2] = blocks[3] =
259
        blocks[4] = blocks[5] = blocks[6] = blocks[7] =
260
        blocks[8] = blocks[9] = blocks[10] = blocks[11] =
261
        blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
250
	if (!this.hashed) {
251
            this.hash();
252
	}
253
	blocks[0] = this.block;
254
	blocks[16] = blocks[1] = blocks[2] = blocks[3] =
255
            blocks[4] = blocks[5] = blocks[6] = blocks[7] =
256
            blocks[8] = blocks[9] = blocks[10] = blocks[11] =
257
            blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
262 258
    }
263 259
    blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
264 260
    blocks[15] = this.bytes << 3;
265 261
    this.hash();
266
  };
262
};
267 263

  
268
  Sha256.prototype.hash = function () {
264
Sha256.prototype.hash = function () {
269 265
    var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
270
      h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
266
	h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
271 267

  
272 268
    for (j = 16; j < 64; ++j) {
273
      // rightrotate
274
      t1 = blocks[j - 15];
275
      s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
276
      t1 = blocks[j - 2];
277
      s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
278
      blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
269
	// rightrotate
270
	t1 = blocks[j - 15];
271
	s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
272
	t1 = blocks[j - 2];
273
	s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
274
	blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
279 275
    }
280 276

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

  
336 332
    this.h0 = this.h0 + a << 0;
......
341 337
    this.h5 = this.h5 + f << 0;
342 338
    this.h6 = this.h6 + g << 0;
343 339
    this.h7 = this.h7 + h << 0;
344
  };
340
};
345 341

  
346
  Sha256.prototype.hex = function () {
342
Sha256.prototype.hex = function () {
347 343
    this.finalize();
348 344

  
349 345
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
350
      h6 = this.h6, h7 = this.h7;
346
	h6 = this.h6, h7 = this.h7;
351 347

  
352 348
    var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
353
      HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
354
      HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
355
      HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
356
      HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
357
      HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
358
      HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
359
      HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
360
      HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
361
      HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
362
      HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
363
      HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
364
      HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
365
      HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
366
      HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
367
      HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
368
      HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
369
      HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
370
      HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
371
      HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
372
      HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
373
      HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
374
      HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
375
      HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
376
      HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
377
      HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
378
      HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
379
      HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
349
	HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
350
	HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
351
	HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
352
	HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
353
	HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
354
	HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
355
	HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
356
	HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
357
	HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
358
	HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
359
	HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
360
	HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
361
	HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
362
	HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
363
	HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
364
	HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
365
	HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
366
	HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
367
	HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
368
	HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
369
	HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
370
	HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
371
	HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
372
	HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
373
	HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
374
	HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
375
	HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
380 376
    if (!this.is224) {
381
      hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
382
        HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
383
        HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
384
        HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
377
	hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
378
            HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
379
            HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
380
            HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
385 381
    }
386 382
    return hex;
387
  };
383
};
388 384

  
389
  Sha256.prototype.toString = Sha256.prototype.hex;
385
Sha256.prototype.toString = Sha256.prototype.hex;
390 386

  
391
  Sha256.prototype.digest = function () {
387
Sha256.prototype.digest = function () {
392 388
    this.finalize();
393 389

  
394 390
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
395
      h6 = this.h6, h7 = this.h7;
391
	h6 = this.h6, h7 = this.h7;
396 392

  
397 393
    var arr = [
398
      (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
399
      (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
400
      (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
401
      (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
402
      (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
403
      (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
404
      (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
394
	(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
395
	(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
396
	(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
397
	(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
398
	(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
399
	(h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
400
	(h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
405 401
    ];
406 402
    if (!this.is224) {
407
      arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
403
	arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
408 404
    }
409 405
    return arr;
410
  };
406
};
411 407

  
412
  Sha256.prototype.array = Sha256.prototype.digest;
408
Sha256.prototype.array = Sha256.prototype.digest;
413 409

  
414
  Sha256.prototype.arrayBuffer = function () {
410
Sha256.prototype.arrayBuffer = function () {
415 411
    this.finalize();
416 412

  
417 413
    var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
......
424 420
    dataView.setUint32(20, this.h5);
425 421
    dataView.setUint32(24, this.h6);
426 422
    if (!this.is224) {
427
      dataView.setUint32(28, this.h7);
423
	dataView.setUint32(28, this.h7);
428 424
    }
429 425
    return buffer;
430
  };
426
};
431 427

  
432
  function HmacSha256(key, is224, sharedMemory) {
428
function HmacSha256(key, is224, sharedMemory) {
433 429
    var i, type = typeof key;
434 430
    if (type === 'string') {
435
      var bytes = [], length = key.length, index = 0, code;
436
      for (i = 0; i < length; ++i) {
437
        code = key.charCodeAt(i);
438
        if (code < 0x80) {
439
          bytes[index++] = code;
440
        } else if (code < 0x800) {
441
          bytes[index++] = (0xc0 | (code >> 6));
442
          bytes[index++] = (0x80 | (code & 0x3f));
443
        } else if (code < 0xd800 || code >= 0xe000) {
444
          bytes[index++] = (0xe0 | (code >> 12));
445
          bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
446
          bytes[index++] = (0x80 | (code & 0x3f));
447
        } else {
448
          code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
449
          bytes[index++] = (0xf0 | (code >> 18));
450
          bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
451
          bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
452
          bytes[index++] = (0x80 | (code & 0x3f));
453
        }
454
      }
455
      key = bytes;
431
	var bytes = [], length = key.length, index = 0, code;
432
	for (i = 0; i < length; ++i) {
433
            code = key.charCodeAt(i);
434
            if (code < 0x80) {
435
		bytes[index++] = code;
436
            } else if (code < 0x800) {
437
		bytes[index++] = (0xc0 | (code >> 6));
438
		bytes[index++] = (0x80 | (code & 0x3f));
439
            } else if (code < 0xd800 || code >= 0xe000) {
440
		bytes[index++] = (0xe0 | (code >> 12));
441
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
442
		bytes[index++] = (0x80 | (code & 0x3f));
443
            } else {
444
		code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
445
		bytes[index++] = (0xf0 | (code >> 18));
446
		bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
447
		bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
448
		bytes[index++] = (0x80 | (code & 0x3f));
449
            }
450
	}
451
	key = bytes;
456 452
    } else {
457
      if (type === 'object') {
458
        if (key === null) {
459
          throw new Error(ERROR);
460
        } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
461
          key = new Uint8Array(key);
462
        } else if (!Array.isArray(key)) {
463
          if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
453
	if (type === 'object') {
454
            if (key === null) {
455
		throw new Error(ERROR);
456
            } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
457
		key = new Uint8Array(key);
458
            } else if (!Array.isArray(key)) {
459
		if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
460
		    throw new Error(ERROR);
461
		}
462
            }
463
	} else {
464 464
            throw new Error(ERROR);
465
          }
466
        }
467
      } else {
468
        throw new Error(ERROR);
469
      }
465
	}
470 466
    }
471 467

  
472 468
    if (key.length > 64) {
473
      key = (new Sha256(is224, true)).update(key).array();
469
	key = (new Sha256(is224, true)).update(key).array();
474 470
    }
475 471

  
476 472
    var oKeyPad = [], iKeyPad = [];
477 473
    for (i = 0; i < 64; ++i) {
478
      var b = key[i] || 0;
479
      oKeyPad[i] = 0x5c ^ b;
480
      iKeyPad[i] = 0x36 ^ b;
474
	var b = key[i] || 0;
475
	oKeyPad[i] = 0x5c ^ b;
476
	iKeyPad[i] = 0x36 ^ b;
481 477
    }
482 478

  
483 479
    Sha256.call(this, is224, sharedMemory);
......
486 482
    this.oKeyPad = oKeyPad;
487 483
    this.inner = true;
488 484
    this.sharedMemory = sharedMemory;
489
  }
490
  HmacSha256.prototype = new Sha256();
485
}
486
HmacSha256.prototype = new Sha256();
491 487

  
492
  HmacSha256.prototype.finalize = function () {
488
HmacSha256.prototype.finalize = function () {
493 489
    Sha256.prototype.finalize.call(this);
494 490
    if (this.inner) {
495
      this.inner = false;
496
      var innerHash = this.array();
497
      Sha256.call(this, this.is224, this.sharedMemory);
498
      this.update(this.oKeyPad);
499
      this.update(innerHash);
500
      Sha256.prototype.finalize.call(this);
491
	this.inner = false;
492
	var innerHash = this.array();
493
	Sha256.call(this, this.is224, this.sharedMemory);
494
	this.update(this.oKeyPad);
495
	this.update(innerHash);
496
	Sha256.prototype.finalize.call(this);
501 497
    }
502
  };
498
};
503 499

  
504
  var exports = createMethod();
505
  exports.sha256 = exports;
506
  exports.sha224 = createMethod(true);
507
  exports.sha256.hmac = createHmacMethod();
508
  exports.sha224.hmac = createHmacMethod(true);
500
var exports = createMethod();
501
exports.sha256 = exports;
502
exports.sha224 = createMethod(true);
503
exports.sha256.hmac = createHmacMethod();
504
exports.sha224.hmac = createHmacMethod(true);
509 505

  
510
  if (COMMON_JS) {
506
if (COMMON_JS) {
511 507
    module.exports = exports;
512
  } else {
508
} else {
513 509
    root.sha256 = exports.sha256;
514 510
    root.sha224 = exports.sha224;
515 511
    if (AMD) {
516
      define(function () {
517
        return exports;
518
      });
512
	define(function () {
513
            return exports;
514
	});
519 515
    }
520
  }
516
}
517

  
518
const sha256 = fake_window.sha256;
521 519

  
522
  window.sha256 = fake_window.sha256;
523
})();
520
/*
521
 * EXPORTS_START
522
 * EXPORT sha256
523
 * EXPORTS_END
524
 */

Also available in: Unified diff