source: trip-planner-front/node_modules/loader-utils/lib/getHashDigest.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[6a3a178]1'use strict';
2
3const baseEncodeTables = {
4 26: 'abcdefghijklmnopqrstuvwxyz',
5 32: '123456789abcdefghjkmnpqrstuvwxyz', // no 0lio
6 36: '0123456789abcdefghijklmnopqrstuvwxyz',
7 49: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no lIO
8 52: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
9 58: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no 0lIO
10 62: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
11 64: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
12};
13
14function encodeBufferToBase(buffer, base) {
15 const encodeTable = baseEncodeTables[base];
16 if (!encodeTable) {
17 throw new Error('Unknown encoding base' + base);
18 }
19
20 const readLength = buffer.length;
21 const Big = require('big.js');
22
23 Big.RM = Big.DP = 0;
24 let b = new Big(0);
25
26 for (let i = readLength - 1; i >= 0; i--) {
27 b = b.times(256).plus(buffer[i]);
28 }
29
30 let output = '';
31 while (b.gt(0)) {
32 output = encodeTable[b.mod(base)] + output;
33 b = b.div(base);
34 }
35
36 Big.DP = 20;
37 Big.RM = 1;
38
39 return output;
40}
41
42function getHashDigest(buffer, hashType, digestType, maxLength) {
43 hashType = hashType || 'md4';
44 maxLength = maxLength || 9999;
45
46 const hash = require('crypto').createHash(hashType);
47
48 hash.update(buffer);
49
50 if (
51 digestType === 'base26' ||
52 digestType === 'base32' ||
53 digestType === 'base36' ||
54 digestType === 'base49' ||
55 digestType === 'base52' ||
56 digestType === 'base58' ||
57 digestType === 'base62' ||
58 digestType === 'base64'
59 ) {
60 return encodeBufferToBase(hash.digest(), digestType.substr(4)).substr(
61 0,
62 maxLength
63 );
64 } else {
65 return hash.digest(digestType || 'hex').substr(0, maxLength);
66 }
67}
68
69module.exports = getHashDigest;
Note: See TracBrowser for help on using the repository browser.