1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const fs = require('fs');
|
10 | const util = require('util');
|
11 | const events = require('events');
|
12 | const crypto = require('crypto');
|
13 | const stream = require('stream');
|
14 | const utils = exports;
|
15 |
|
16 |
|
17 | const BUG_FREE = require('./bugfree');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | exports.bugfree = function bugfree(doNotOutput) {
|
27 | if (doNotOutput) {
|
28 | return BUG_FREE;
|
29 | }
|
30 | console.log(BUG_FREE);
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | exports.format = util.format;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | exports.sha1 = function sha1(text) {
|
50 | return crypto.createHash('sha1').update(utils.toBuffer(text)).digest('hex');
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | exports.md5 = function md5(text) {
|
60 | return crypto.createHash('md5').update(utils.toBuffer(text)).digest('hex');
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | exports.fileSha1 = function fileSha1(filename, callback) {
|
70 | fs.readFile(filename, function (err, data) {
|
71 | if (err) return callback(err);
|
72 | callback(null, utils.sha1(data));
|
73 | });
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.fileMd5 = function fileMd5(filename, callback) {
|
83 | fs.readFile(filename, function (err, data) {
|
84 | if (err) return callback(err);
|
85 | callback(null, utils.md5(data));
|
86 | });
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | exports.hash = function hash(method, text) {
|
97 | return crypto.createHash(method).update(utils.toBuffer(text)).digest('hex');
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | exports.encryptPassword = function encryptPassword(password) {
|
107 | const random = utils.md5(Math.random() + '' + Math.random()).toUpperCase();
|
108 | const left = random.substr(0, 2);
|
109 | const right = random.substr(-2);
|
110 | const newpassword = utils.md5(left + password + right).toUpperCase();
|
111 | return [ left, newpassword, right ].join(':');
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | exports.validatePassword = function validatePassword(password, encrypted) {
|
122 | const random = encrypted.toUpperCase().split(':');
|
123 | if (random.length < 3) return false;
|
124 | const left = random[0];
|
125 | const right = random[2];
|
126 | const main = random[1];
|
127 | const newpassword = utils.md5(left + password + right).toUpperCase();
|
128 | return newpassword === main;
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | exports.encryptData = function encryptData(data, secret) {
|
139 | const str = JSON.stringify(data);
|
140 | const cipher = crypto.createCipher('aes192', secret);
|
141 | let enc = cipher.update(str, 'utf8', 'hex');
|
142 | enc += cipher.final('hex');
|
143 | return enc;
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | exports.decryptData = function decryptData(str, secret) {
|
154 | const decipher = crypto.createDecipher('aes192', secret);
|
155 | let dec = decipher.update(str, 'hex', 'utf8');
|
156 | dec += decipher.final('utf8');
|
157 | const data = JSON.parse(dec);
|
158 | return data;
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | exports.randomString = function randomString(size, chars) {
|
169 | size = size || 6;
|
170 | chars = chars || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
171 | const max = chars.length;
|
172 | let ret = '';
|
173 | for (let i = 0; i < size; i++) {
|
174 | ret += chars.charAt(Math.floor(Math.random() * max));
|
175 | }
|
176 | return ret;
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | exports.randomNumber = function randomNumber(size) {
|
186 | return utils.randomString(size, '0123456789');
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | exports.randomLetter = function randomLetter(size) {
|
196 | return utils.randomString(size, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | exports.date = function date(format, timestamp) {
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | let jsdate, f;
|
264 |
|
265 |
|
266 | const txt_words = [
|
267 | 'Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur',
|
268 | 'January', 'February', 'March', 'April', 'May', 'June',
|
269 | 'July', 'August', 'September', 'October', 'November', 'December',
|
270 | ];
|
271 |
|
272 |
|
273 |
|
274 | const formatChr = /\\?(.?)/gi;
|
275 | const formatChrCb = function (t, s) {
|
276 | return f[t] ? f[t]() : s;
|
277 | };
|
278 | const _pad = function (n, c) {
|
279 | n = String(n);
|
280 | while (n.length < c) {
|
281 | n = '0' + n;
|
282 | }
|
283 | return n;
|
284 | };
|
285 | f = {
|
286 |
|
287 | d() {
|
288 |
|
289 | return _pad(f.j(), 2);
|
290 | },
|
291 | D() {
|
292 |
|
293 | return f.l()
|
294 | .slice(0, 3);
|
295 | },
|
296 | j() {
|
297 |
|
298 | return jsdate.getDate();
|
299 | },
|
300 | l() {
|
301 |
|
302 | return txt_words[f.w()] + 'day';
|
303 | },
|
304 | N() {
|
305 |
|
306 | return f.w() || 7;
|
307 | },
|
308 | S() {
|
309 |
|
310 | const j = f.j();
|
311 | let i = j % 10;
|
312 | if (i <= 3 && parseInt((j % 100) / 10, 10) === 1) {
|
313 | i = 0;
|
314 | }
|
315 | return [ 'st', 'nd', 'rd' ][i - 1] || 'th';
|
316 | },
|
317 | w() {
|
318 |
|
319 | return jsdate.getDay();
|
320 | },
|
321 | z() {
|
322 |
|
323 | const a = new Date(f.Y(), f.n() - 1, f.j());
|
324 | const b = new Date(f.Y(), 0, 1);
|
325 | return Math.round((a - b) / 864e5);
|
326 | },
|
327 |
|
328 |
|
329 | W() {
|
330 |
|
331 | const a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3);
|
332 | const b = new Date(a.getFullYear(), 0, 4);
|
333 | return _pad(1 + Math.round((a - b) / 864e5 / 7), 2);
|
334 | },
|
335 |
|
336 |
|
337 | F() {
|
338 |
|
339 | return txt_words[6 + f.n()];
|
340 | },
|
341 | m() {
|
342 |
|
343 | return _pad(f.n(), 2);
|
344 | },
|
345 | M() {
|
346 |
|
347 | return f.F()
|
348 | .slice(0, 3);
|
349 | },
|
350 | n() {
|
351 |
|
352 | return jsdate.getMonth() + 1;
|
353 | },
|
354 | t() {
|
355 |
|
356 | return (new Date(f.Y(), f.n(), 0))
|
357 | .getDate();
|
358 | },
|
359 |
|
360 |
|
361 | L() {
|
362 |
|
363 | const j = f.Y();
|
364 | return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0;
|
365 | },
|
366 | o() {
|
367 |
|
368 | const n = f.n();
|
369 | const W = f.W();
|
370 | const Y = f.Y();
|
371 |
|
372 | return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0);
|
373 | },
|
374 | Y() {
|
375 |
|
376 | return jsdate.getFullYear();
|
377 | },
|
378 | y() {
|
379 |
|
380 | return f.Y()
|
381 | .toString()
|
382 | .slice(-2);
|
383 | },
|
384 |
|
385 |
|
386 | a() {
|
387 |
|
388 | return jsdate.getHours() > 11 ? 'pm' : 'am';
|
389 | },
|
390 | A() {
|
391 |
|
392 | return f.a()
|
393 | .toUpperCase();
|
394 | },
|
395 | B() {
|
396 |
|
397 | const H = jsdate.getUTCHours() * 36e2;
|
398 |
|
399 | const i = jsdate.getUTCMinutes() * 60;
|
400 |
|
401 | const s = jsdate.getUTCSeconds();
|
402 | return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
|
403 | },
|
404 | g() {
|
405 |
|
406 | return f.G() % 12 || 12;
|
407 | },
|
408 | G() {
|
409 |
|
410 | return jsdate.getHours();
|
411 | },
|
412 | h() {
|
413 |
|
414 | return _pad(f.g(), 2);
|
415 | },
|
416 | H() {
|
417 |
|
418 | return _pad(f.G(), 2);
|
419 | },
|
420 | i() {
|
421 |
|
422 | return _pad(jsdate.getMinutes(), 2);
|
423 | },
|
424 | s() {
|
425 |
|
426 | return _pad(jsdate.getSeconds(), 2);
|
427 | },
|
428 | u() {
|
429 |
|
430 | return _pad(jsdate.getMilliseconds() * 1000, 6);
|
431 | },
|
432 |
|
433 |
|
434 | e() {
|
435 |
|
436 |
|
437 |
|
438 | |
439 |
|
440 | throw new Error('Not supported (see source code of date() for timezone on how to add support)');
|
441 | },
|
442 | I() {
|
443 |
|
444 |
|
445 |
|
446 | const a = new Date(f.Y(), 0);
|
447 |
|
448 | const c = Date.UTC(f.Y(), 0);
|
449 |
|
450 | const b = new Date(f.Y(), 6);
|
451 |
|
452 | const d = Date.UTC(f.Y(), 6);
|
453 | return ((a - c) !== (b - d)) ? 1 : 0;
|
454 | },
|
455 | O() {
|
456 |
|
457 | const tzo = jsdate.getTimezoneOffset();
|
458 | const a = Math.abs(tzo);
|
459 | return (tzo > 0 ? '-' : '+') + _pad(Math.floor(a / 60) * 100 + a % 60, 4);
|
460 | },
|
461 | P() {
|
462 |
|
463 | const O = f.O();
|
464 | return (O.substr(0, 3) + ':' + O.substr(3, 2));
|
465 | },
|
466 | T() {
|
467 |
|
468 |
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | return 'UTC';
|
494 | },
|
495 | Z() {
|
496 |
|
497 | return -jsdate.getTimezoneOffset() * 60;
|
498 | },
|
499 |
|
500 |
|
501 | c() {
|
502 |
|
503 | return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb);
|
504 | },
|
505 | r() {
|
506 |
|
507 | return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
|
508 | },
|
509 | U() {
|
510 |
|
511 | return jsdate / 1000 | 0;
|
512 | },
|
513 | };
|
514 | this.date = function (format, timestamp) {
|
515 |
|
516 | jsdate = (timestamp === undefined ? new Date() :
|
517 | (timestamp instanceof Date) ? new Date(timestamp) :
|
518 | new Date(timestamp * 1000)
|
519 | );
|
520 | return format.replace(formatChr, formatChrCb);
|
521 | };
|
522 | return this.date(format, timestamp);
|
523 | };
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 | exports.noop = function noop(err) {
|
531 | if (err) {
|
532 | console.error('noop callback: %s', err);
|
533 | }
|
534 | };
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 | exports.isString = function isString(str) {
|
543 | return (typeof str === 'string');
|
544 | };
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 | exports.isInteger = function isInteger(str) {
|
553 | return (Math.round(str) === Number(str));
|
554 | };
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 | exports.isNumber = function isNumber(str) {
|
563 | return (!isNaN(str));
|
564 | };
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | exports.cloneObject = function cloneObject(obj) {
|
573 | return JSON.parse(JSON.stringify(obj));
|
574 | };
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | exports.merge = function merge() {
|
584 | const ret = {};
|
585 | for (let i = 0; i < arguments.length; i++) {
|
586 | const obj = arguments[i];
|
587 | Object.keys(obj).forEach(function (k) {
|
588 | ret[k] = obj[k];
|
589 | });
|
590 | }
|
591 | return ret;
|
592 | };
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 | exports.jsonStringify = function jsonStringify(data, space) {
|
602 | const seen = [];
|
603 | return JSON.stringify(data, function (key, val) {
|
604 | if (!val || typeof val !== 'object') {
|
605 | return val;
|
606 | }
|
607 | if (seen.indexOf(val) !== -1) {
|
608 | return '[Circular]';
|
609 | }
|
610 | seen.push(val);
|
611 | return val;
|
612 | }, space);
|
613 | };
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 | exports.argumentsToArray = function argumentsToArray(args) {
|
622 | return Array.prototype.slice.call(args);
|
623 | };
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 | exports.getArrayLastItem = function getArrayLastItem(arr) {
|
632 | return arr[arr.length - 1];
|
633 | };
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | exports.throttleAsync = function throttleAsync(fn, maxCount) {
|
643 | if (!(maxCount > 1)) maxCount = 1;
|
644 | let counter = 0;
|
645 | return function () {
|
646 | const args = utils.argumentsToArray(arguments);
|
647 | const callback = utils.getArrayLastItem(args);
|
648 | if (counter >= maxCount) return callback(new Error('throttleAsync() out of limit'));
|
649 | args.pop();
|
650 | args.push(function () {
|
651 | counter -= 1;
|
652 | callback.apply(null, arguments);
|
653 | });
|
654 | counter += 1;
|
655 | fn.apply(null, args);
|
656 | };
|
657 | };
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 | exports.inheritsEventEmitter = function inheritsEventEmitter(fn) {
|
666 | return util.inherits(fn, events.EventEmitter);
|
667 | };
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 | exports.inherits = function inherits(fn, superConstructor) {
|
677 | return util.inherits(fn, superConstructor);
|
678 | };
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 | exports.extend = function extend(obj) {
|
687 | return utils.merge(exports, obj);
|
688 | };
|
689 | exports.extends = exports.extend;
|
690 |
|
691 | exports.array = {};
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | exports.array.last = function last(arr) {
|
700 | return arr[arr.length - 1];
|
701 | };
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 | exports.array.head = function head(arr) {
|
710 | return arr[0];
|
711 | };
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 | exports.array.first = function first(arr) {
|
720 | return arr[0];
|
721 | };
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 | exports.array.rest = function rest(arr) {
|
730 | return arr.slice(1);
|
731 | };
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 | exports.array.copy = function copy(arr) {
|
740 | return arr.slice();
|
741 | };
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 | exports.array.concat = function concat() {
|
751 | const ret = [];
|
752 | return ret.concat.apply(ret, arguments);
|
753 | };
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 | exports.customError = function customError(name, info) {
|
763 | name = name || 'CustomError';
|
764 | info = info || {};
|
765 | const code = '' +
|
766 | 'function ' + name + '(message, info2) {\n' +
|
767 | ' Error.captureStackTrace(this, ' + name + ');\n' +
|
768 | ' this.name = "' + name + '";\n' +
|
769 | ' this.message = (message || "");\n' +
|
770 | ' info2 = info2 || {};\n' +
|
771 | ' for (var i in info) this[i] = info[i];\n' +
|
772 | ' for (var i in info2) this[i] = info2[i];\n' +
|
773 | '}\n' +
|
774 | name + '.prototype = Error.prototype;' + name;
|
775 | return eval(code);
|
776 | };
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 | exports.isPromise = function isPromise(p) {
|
785 | return (p && typeof p.then === 'function' && typeof p.catch === 'function');
|
786 | };
|
787 |
|
788 | exports.promise = {};
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 | exports.promise.call = function call(fn) {
|
797 | const args = utils.argumentsToArray(arguments).slice(1);
|
798 | return new Promise(function (resolve, reject) {
|
799 | args.push(function (err, ret) {
|
800 | if (err) {
|
801 | reject(err);
|
802 | } else {
|
803 | resolve(ret);
|
804 | }
|
805 | });
|
806 | let ret;
|
807 | try {
|
808 | ret = fn.apply(null, args);
|
809 | } catch (err) {
|
810 | return reject(err);
|
811 | }
|
812 | if (utils.isPromise(ret)) {
|
813 | ret.then(resolve).catch(reject);
|
814 | }
|
815 | });
|
816 | };
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 | exports.promise.all = function all(_args) {
|
825 | const args = Array.isArray(_args) ? _args : utils.argumentsToArray(arguments);
|
826 | return new Promise(function (resolve, reject) {
|
827 | const results = [];
|
828 | let counter = 0;
|
829 | function check() {
|
830 | counter += 1;
|
831 | if (counter === args.length) {
|
832 | resolve(results);
|
833 | }
|
834 | }
|
835 | args.forEach(function (p, i) {
|
836 | p.then(function (ret) {
|
837 | results[i] = [ null, ret ];
|
838 | check();
|
839 | }).catch(function (err) {
|
840 | results[i] = [ err ];
|
841 | check();
|
842 | });
|
843 | });
|
844 | });
|
845 | };
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 | exports.ipToInt = function ipToInt(ip) {
|
857 | const s = ip.split('.');
|
858 | if (s.length !== 4) return false;
|
859 | for (let i = 0; i < 4; i++) {
|
860 | const v = s[i] = parseInt(s[i], 10);
|
861 | if (v < 0 || v > 255) return false;
|
862 | }
|
863 | return s[0] * 16777216 + s[1] * 65536 + s[2] * 256 + s[3];
|
864 | };
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 | exports.toBuffer = function toBuffer(data) {
|
873 | if (Buffer.isBuffer(data)) return data;
|
874 | if (typeof data === 'string') return new Buffer(data);
|
875 | throw new Error('invalid data type, must be string or buffer');
|
876 | };
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 | exports.encrypt = function encrypt(algorithm, key, data) {
|
887 | key = Buffer.isBuffer(key) ? key : keyHash(key);
|
888 | data = utils.toBuffer(data);
|
889 | const cipher = crypto.createCipheriv(algorithm, key, new Buffer(0));
|
890 | const encrypted = [ cipher.update(data), cipher.final() ];
|
891 | return Buffer.concat(encrypted);
|
892 | };
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 | exports.encryptStream = function encryptStream(algorithm, key, inStream) {
|
903 | key = Buffer.isBuffer(key) ? key : keyHash(key);
|
904 | const cipher = crypto.createCipheriv(algorithm, key, new Buffer(0));
|
905 | return inStream.pipe(cipher);
|
906 | };
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 | exports.decrypt = function decrypt(algorithm, key, data) {
|
917 | key = Buffer.isBuffer(key) ? key : keyHash(key);
|
918 | data = utils.toBuffer(data);
|
919 | const cipher = crypto.createDecipheriv(algorithm, key, new Buffer(0));
|
920 | const encrypted = [ cipher.update(data), cipher.final() ];
|
921 | return Buffer.concat(encrypted);
|
922 | };
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 | exports.decryptStream = function decryptStream(algorithm, key, inStream) {
|
933 | key = Buffer.isBuffer(key) ? key : keyHash(key);
|
934 | const cipher = crypto.createDecipheriv(algorithm, key, new Buffer(0));
|
935 | return inStream.pipe(cipher);
|
936 | };
|
937 |
|
938 | function keyHash(data) {
|
939 | data = utils.toBuffer(data);
|
940 | return crypto.createHash('sha256').update(data).digest();
|
941 | }
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 | exports.hashTransform = function hashTransform(method, callback) {
|
950 | const cipher = crypto.createHash(method);
|
951 | const transform = new stream.Transform({
|
952 | transform(chunk, encoding, callback) {
|
953 | cipher.update(chunk, encoding);
|
954 | this.push(chunk);
|
955 | callback();
|
956 | },
|
957 | });
|
958 | transform.once('finish', () => callback(null, cipher.digest()));
|
959 | return transform;
|
960 | };
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 | exports.createPromiseCallback = function createPromiseCallback() {
|
968 | const callback = (err, ret) => {
|
969 | if (err) {
|
970 | callback.reject(err);
|
971 | } else {
|
972 | callback.resolve(ret);
|
973 | }
|
974 | };
|
975 | callback.promise = new Promise((resolve, reject) => {
|
976 | callback.resolve = resolve;
|
977 | callback.reject = reject;
|
978 | });
|
979 | return callback;
|
980 | };
|