Sha256: 06d6e8208e0d54fe52b77c5c1babcb68b2addf9ba8269b8f2b492d29c6652aa4

Contents?: true

Size: 1.73 KB

Versions: 48

Compression:

Stored size: 1.73 KB

Contents

'use strict';

const baseEncodeTables = {
  26: 'abcdefghijklmnopqrstuvwxyz',
  32: '123456789abcdefghjkmnpqrstuvwxyz', // no 0lio
  36: '0123456789abcdefghijklmnopqrstuvwxyz',
  49: 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no lIO
  52: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  58: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', // no 0lIO
  62: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  64: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
};

function encodeBufferToBase(buffer, base) {
  const encodeTable = baseEncodeTables[base];
  if (!encodeTable) {
    throw new Error('Unknown encoding base' + base);
  }

  const readLength = buffer.length;
  const Big = require('big.js');

  Big.RM = Big.DP = 0;
  let b = new Big(0);

  for (let i = readLength - 1; i >= 0; i--) {
    b = b.times(256).plus(buffer[i]);
  }

  let output = '';
  while (b.gt(0)) {
    output = encodeTable[b.mod(base)] + output;
    b = b.div(base);
  }

  Big.DP = 20;
  Big.RM = 1;

  return output;
}

function getHashDigest(buffer, hashType, digestType, maxLength) {
  hashType = hashType || 'md5';
  maxLength = maxLength || 9999;

  const hash = require('crypto').createHash(hashType);

  hash.update(buffer);

  if (
    digestType === 'base26' ||
    digestType === 'base32' ||
    digestType === 'base36' ||
    digestType === 'base49' ||
    digestType === 'base52' ||
    digestType === 'base58' ||
    digestType === 'base62' ||
    digestType === 'base64'
  ) {
    return encodeBufferToBase(hash.digest(), digestType.substr(4)).substr(
      0,
      maxLength
    );
  } else {
    return hash.digest(digestType || 'hex').substr(0, maxLength);
  }
}

module.exports = getHashDigest;

Version data entries

48 entries across 28 versions & 9 rubygems

Version Path
disco_app-0.14.0 test/dummy/node_modules/loader-utils/lib/getHashDigest.js
disco_app-0.14.0 test/dummy/node_modules/webpack-cli/node_modules/loader-utils/lib/getHashDigest.js
disco_app-0.14.0 test/dummy/node_modules/babel-loader/node_modules/loader-utils/lib/getHashDigest.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/loader-utils/lib/getHashDigest.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/webpack-cli/node_modules/loader-utils/lib/getHashDigest.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/babel-loader/node_modules/loader-utils/lib/getHashDigest.js
tang-0.2.1 spec/tang_app/node_modules/loader-utils/lib/getHashDigest.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/loader-utils/lib/getHashDigest.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/loader-utils/lib/getHashDigest.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/loader-utils/lib/getHashDigest.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/loader-utils/lib/getHashDigest.js
tang-0.2.0 spec/tang_app/node_modules/loader-utils/lib/getHashDigest.js
tang-0.1.0 spec/tang_app/node_modules/loader-utils/lib/getHashDigest.js
tang-0.0.9 spec/tang_app/node_modules/loader-utils/lib/getHashDigest.js
enju_library-0.3.8 spec/dummy/node_modules/loader-utils/lib/getHashDigest.js
ilog-0.4.1 node_modules/loader-utils/lib/getHashDigest.js
ilog-0.4.1 node_modules/laravel-mix/node_modules/vue-style-loader/node_modules/loader-utils/lib/getHashDigest.js
ilog-0.4.0 node_modules/loader-utils/lib/getHashDigest.js
ilog-0.4.0 node_modules/laravel-mix/node_modules/vue-style-loader/node_modules/loader-utils/lib/getHashDigest.js
ilog-0.3.3 node_modules/loader-utils/lib/getHashDigest.js