UNPKG

13.1 kBJavaScriptView Raw
1/*
2 * [js-sha1]{@link https://github.com/emn178/js-sha1}
3 *
4 * @version 0.6.0
5 * @author Chen, Yi-Cyuan [emn178@gmail.com]
6 * @copyright Chen, Yi-Cyuan 2014-2017
7 * @license MIT
8 */
9/*jslint bitwise: true */
10(function() {
11 'use strict';
12
13 var root = typeof window === 'object' ? window : {};
14 var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
15 if (NODE_JS) {
16 root = global;
17 }
18 var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
19 var AMD = typeof define === 'function' && define.amd;
20 var HEX_CHARS = '0123456789abcdef'.split('');
21 var EXTRA = [-2147483648, 8388608, 32768, 128];
22 var SHIFT = [24, 16, 8, 0];
23 var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
24
25 var blocks = [];
26
27 var createOutputMethod = function (outputType) {
28 return function (message) {
29 return new Sha1(true).update(message)[outputType]();
30 };
31 };
32
33 var createMethod = function () {
34 var method = createOutputMethod('hex');
35 if (NODE_JS) {
36 // method = nodeWrap(method);
37 }
38 method.create = function () {
39 return new Sha1();
40 };
41 method.update = function (message) {
42 return method.create().update(message);
43 };
44 for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
45 var type = OUTPUT_TYPES[i];
46 method[type] = createOutputMethod(type);
47 }
48 return method;
49 };
50
51 var nodeWrap = function (method) {
52 var crypto = eval("require('crypto')");
53 var Buffer = eval("require('buffer').Buffer");
54 var nodeMethod = function (message) {
55 if (typeof message === 'string') {
56 return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
57 } else if (message.constructor === ArrayBuffer) {
58 message = new Uint8Array(message);
59 } else if (message.length === undefined) {
60 return method(message);
61 }
62 return crypto.createHash('sha1').update(new Buffer(message)).digest('hex');
63 };
64 return nodeMethod;
65 };
66
67 function Sha1(sharedMemory) {
68 if (sharedMemory) {
69 blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
70 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
71 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
72 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
73 this.blocks = blocks;
74 } else {
75 this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
76 }
77
78 this.h0 = 0x67452301;
79 this.h1 = 0xEFCDAB89;
80 this.h2 = 0x98BADCFE;
81 this.h3 = 0x10325476;
82 this.h4 = 0xC3D2E1F0;
83
84 this.block = this.start = this.bytes = this.hBytes = 0;
85 this.finalized = this.hashed = false;
86 this.first = true;
87 }
88
89 Sha1.prototype.update = function (message) {
90 if (this.finalized) {
91 return;
92 }
93 var notString = typeof(message) !== 'string';
94 if (notString && message.constructor === root.ArrayBuffer) {
95 message = new Uint8Array(message);
96 }
97 var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
98
99 while (index < length) {
100 if (this.hashed) {
101 this.hashed = false;
102 blocks[0] = this.block;
103 blocks[16] = blocks[1] = blocks[2] = blocks[3] =
104 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
105 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
106 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
107 }
108
109 if(notString) {
110 for (i = this.start; index < length && i < 64; ++index) {
111 blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
112 }
113 } else {
114 for (i = this.start; index < length && i < 64; ++index) {
115 code = message.charCodeAt(index);
116 if (code < 0x80) {
117 blocks[i >> 2] |= code << SHIFT[i++ & 3];
118 } else if (code < 0x800) {
119 blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
120 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
121 } else if (code < 0xd800 || code >= 0xe000) {
122 blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
123 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
124 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
125 } else {
126 code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
127 blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
128 blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
129 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
130 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
131 }
132 }
133 }
134
135 this.lastByteIndex = i;
136 this.bytes += i - this.start;
137 if (i >= 64) {
138 this.block = blocks[16];
139 this.start = i - 64;
140 this.hash();
141 this.hashed = true;
142 } else {
143 this.start = i;
144 }
145 }
146 if (this.bytes > 4294967295) {
147 this.hBytes += this.bytes / 4294967296 << 0;
148 this.bytes = this.bytes % 4294967296;
149 }
150 return this;
151 };
152
153 Sha1.prototype.finalize = function () {
154 if (this.finalized) {
155 return;
156 }
157 this.finalized = true;
158 var blocks = this.blocks, i = this.lastByteIndex;
159 blocks[16] = this.block;
160 blocks[i >> 2] |= EXTRA[i & 3];
161 this.block = blocks[16];
162 if (i >= 56) {
163 if (!this.hashed) {
164 this.hash();
165 }
166 blocks[0] = this.block;
167 blocks[16] = blocks[1] = blocks[2] = blocks[3] =
168 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
169 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
170 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
171 }
172 blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
173 blocks[15] = this.bytes << 3;
174 this.hash();
175 };
176
177 Sha1.prototype.hash = function () {
178 var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
179 var f, j, t, blocks = this.blocks;
180
181 for(j = 16; j < 80; ++j) {
182 t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
183 blocks[j] = (t << 1) | (t >>> 31);
184 }
185
186 for(j = 0; j < 20; j += 5) {
187 f = (b & c) | ((~b) & d);
188 t = (a << 5) | (a >>> 27);
189 e = t + f + e + 1518500249 + blocks[j] << 0;
190 b = (b << 30) | (b >>> 2);
191
192 f = (a & b) | ((~a) & c);
193 t = (e << 5) | (e >>> 27);
194 d = t + f + d + 1518500249 + blocks[j + 1] << 0;
195 a = (a << 30) | (a >>> 2);
196
197 f = (e & a) | ((~e) & b);
198 t = (d << 5) | (d >>> 27);
199 c = t + f + c + 1518500249 + blocks[j + 2] << 0;
200 e = (e << 30) | (e >>> 2);
201
202 f = (d & e) | ((~d) & a);
203 t = (c << 5) | (c >>> 27);
204 b = t + f + b + 1518500249 + blocks[j + 3] << 0;
205 d = (d << 30) | (d >>> 2);
206
207 f = (c & d) | ((~c) & e);
208 t = (b << 5) | (b >>> 27);
209 a = t + f + a + 1518500249 + blocks[j + 4] << 0;
210 c = (c << 30) | (c >>> 2);
211 }
212
213 for(; j < 40; j += 5) {
214 f = b ^ c ^ d;
215 t = (a << 5) | (a >>> 27);
216 e = t + f + e + 1859775393 + blocks[j] << 0;
217 b = (b << 30) | (b >>> 2);
218
219 f = a ^ b ^ c;
220 t = (e << 5) | (e >>> 27);
221 d = t + f + d + 1859775393 + blocks[j + 1] << 0;
222 a = (a << 30) | (a >>> 2);
223
224 f = e ^ a ^ b;
225 t = (d << 5) | (d >>> 27);
226 c = t + f + c + 1859775393 + blocks[j + 2] << 0;
227 e = (e << 30) | (e >>> 2);
228
229 f = d ^ e ^ a;
230 t = (c << 5) | (c >>> 27);
231 b = t + f + b + 1859775393 + blocks[j + 3] << 0;
232 d = (d << 30) | (d >>> 2);
233
234 f = c ^ d ^ e;
235 t = (b << 5) | (b >>> 27);
236 a = t + f + a + 1859775393 + blocks[j + 4] << 0;
237 c = (c << 30) | (c >>> 2);
238 }
239
240 for(; j < 60; j += 5) {
241 f = (b & c) | (b & d) | (c & d);
242 t = (a << 5) | (a >>> 27);
243 e = t + f + e - 1894007588 + blocks[j] << 0;
244 b = (b << 30) | (b >>> 2);
245
246 f = (a & b) | (a & c) | (b & c);
247 t = (e << 5) | (e >>> 27);
248 d = t + f + d - 1894007588 + blocks[j + 1] << 0;
249 a = (a << 30) | (a >>> 2);
250
251 f = (e & a) | (e & b) | (a & b);
252 t = (d << 5) | (d >>> 27);
253 c = t + f + c - 1894007588 + blocks[j + 2] << 0;
254 e = (e << 30) | (e >>> 2);
255
256 f = (d & e) | (d & a) | (e & a);
257 t = (c << 5) | (c >>> 27);
258 b = t + f + b - 1894007588 + blocks[j + 3] << 0;
259 d = (d << 30) | (d >>> 2);
260
261 f = (c & d) | (c & e) | (d & e);
262 t = (b << 5) | (b >>> 27);
263 a = t + f + a - 1894007588 + blocks[j + 4] << 0;
264 c = (c << 30) | (c >>> 2);
265 }
266
267 for(; j < 80; j += 5) {
268 f = b ^ c ^ d;
269 t = (a << 5) | (a >>> 27);
270 e = t + f + e - 899497514 + blocks[j] << 0;
271 b = (b << 30) | (b >>> 2);
272
273 f = a ^ b ^ c;
274 t = (e << 5) | (e >>> 27);
275 d = t + f + d - 899497514 + blocks[j + 1] << 0;
276 a = (a << 30) | (a >>> 2);
277
278 f = e ^ a ^ b;
279 t = (d << 5) | (d >>> 27);
280 c = t + f + c - 899497514 + blocks[j + 2] << 0;
281 e = (e << 30) | (e >>> 2);
282
283 f = d ^ e ^ a;
284 t = (c << 5) | (c >>> 27);
285 b = t + f + b - 899497514 + blocks[j + 3] << 0;
286 d = (d << 30) | (d >>> 2);
287
288 f = c ^ d ^ e;
289 t = (b << 5) | (b >>> 27);
290 a = t + f + a - 899497514 + blocks[j + 4] << 0;
291 c = (c << 30) | (c >>> 2);
292 }
293
294 this.h0 = this.h0 + a << 0;
295 this.h1 = this.h1 + b << 0;
296 this.h2 = this.h2 + c << 0;
297 this.h3 = this.h3 + d << 0;
298 this.h4 = this.h4 + e << 0;
299 };
300
301 Sha1.prototype.hex = function () {
302 this.finalize();
303
304 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
305
306 return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
307 HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
308 HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
309 HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
310 HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
311 HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
312 HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
313 HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
314 HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
315 HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
316 HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
317 HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
318 HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
319 HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
320 HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
321 HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
322 HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
323 HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
324 HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
325 HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
326 };
327
328 Sha1.prototype.toString = Sha1.prototype.hex;
329
330 Sha1.prototype.digest = function () {
331 this.finalize();
332
333 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
334
335 return [
336 (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
337 (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
338 (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
339 (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
340 (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF
341 ];
342 };
343
344 var alphabet = '0123456789abcdefghjkmnpqrtuvwxyz'
345 var alias = { o:0, i:1, l:1, s:5 }
346
347 Sha1.prototype.b32 = function () {
348 var bytes = this.digest();
349
350 var skip = 0 // how many bits we will skip from the first byte
351 var bits = 0 // 5 high bits, carry from one byte to the next
352 var out = ''
353
354 for (var i = 0; i < bytes.length; ) {
355 var byte = bytes[i];
356
357 if (skip < 0) { // we have a carry from the previous byte
358 bits |= (byte >> (-skip))
359 } else { // no carry
360 bits = (byte << skip) & 248
361 }
362
363 if (skip > 3) {
364 // not enough data to produce a character, get us another one
365 skip -= 8;
366 i += 1;
367 continue
368 }
369
370 if (skip < 4) {
371 // produce a character
372 out += alphabet[bits >> 3]
373 skip += 5
374 }
375 }
376
377 out = out + (skip < 0 ? alphabet[bits >> 3] : '')
378
379 return out;
380 };
381
382 Sha1.prototype.array = Sha1.prototype.digest;
383
384 Sha1.prototype.arrayBuffer = function () {
385 this.finalize();
386
387 var buffer = new ArrayBuffer(20);
388 var dataView = new DataView(buffer);
389 dataView.setUint32(0, this.h0);
390 dataView.setUint32(4, this.h1);
391 dataView.setUint32(8, this.h2);
392 dataView.setUint32(12, this.h3);
393 dataView.setUint32(16, this.h4);
394 return buffer;
395 };
396
397 var exports = createMethod();
398
399 if (COMMON_JS) {
400 module.exports = exports;
401 } else {
402 root.sha1 = exports;
403 if (AMD) {
404 define(function () {
405 return exports;
406 });
407 }
408 }
409})();
\No newline at end of file