| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const 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 |
|
|---|
| 14 | function 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 |
|
|---|
| 42 | let createMd4 = undefined;
|
|---|
| 43 | let BatchedHash = undefined;
|
|---|
| 44 |
|
|---|
| 45 | function getHashDigest(buffer, hashType, digestType, maxLength) {
|
|---|
| 46 | hashType = hashType || 'md4';
|
|---|
| 47 | maxLength = maxLength || 9999;
|
|---|
| 48 |
|
|---|
| 49 | let hash;
|
|---|
| 50 |
|
|---|
| 51 | try {
|
|---|
| 52 | hash = require('crypto').createHash(hashType);
|
|---|
| 53 | } catch (error) {
|
|---|
| 54 | if (error.code === 'ERR_OSSL_EVP_UNSUPPORTED' && hashType === 'md4') {
|
|---|
| 55 | if (createMd4 === undefined) {
|
|---|
| 56 | createMd4 = require('./hash/md4');
|
|---|
| 57 |
|
|---|
| 58 | if (BatchedHash === undefined) {
|
|---|
| 59 | BatchedHash = require('./hash/BatchedHash');
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | hash = new BatchedHash(createMd4());
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (!hash) {
|
|---|
| 67 | throw error;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | hash.update(buffer);
|
|---|
| 72 |
|
|---|
| 73 | if (
|
|---|
| 74 | digestType === 'base26' ||
|
|---|
| 75 | digestType === 'base32' ||
|
|---|
| 76 | digestType === 'base36' ||
|
|---|
| 77 | digestType === 'base49' ||
|
|---|
| 78 | digestType === 'base52' ||
|
|---|
| 79 | digestType === 'base58' ||
|
|---|
| 80 | digestType === 'base62'
|
|---|
| 81 | ) {
|
|---|
| 82 | return encodeBufferToBase(hash.digest(), digestType.substr(4)).substr(
|
|---|
| 83 | 0,
|
|---|
| 84 | maxLength
|
|---|
| 85 | );
|
|---|
| 86 | } else {
|
|---|
| 87 | return hash.digest(digestType || 'hex').substr(0, maxLength);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | module.exports = getHashDigest;
|
|---|