Files
30-seconds-of-code/node_modules/gatsby/dist/utils/get-hash-fn.js
2019-08-20 15:52:05 +02:00

26 lines
637 B
JavaScript

"use strict";
const createHash = require(`crypto`).createHash;
const getHashFn = ({
hashFunction = `md5`,
hashDigest = `hex`,
hashDigestBits = 48,
cache = new Set()
}) => input => {
const hash = createHash(hashFunction);
hash.update(input);
const digest = hash.digest(hashDigest);
const partialDigest = digest.substr(0, hashDigestBits / 4);
const output = parseInt(partialDigest, 16); // guard against collisions
if (cache.has(output)) {
throw Error(`Hash collision at f(${input}) = ${output}`);
}
cache.add(output);
return output;
};
module.exports = getHashFn;
//# sourceMappingURL=get-hash-fn.js.map