fetch-dht/lib/fakePeerId/index.js
2020-04-12 16:33:32 -05:00

21 lines
428 B
JavaScript

const crypto = require('crypto');
const memo = new Map();
const fakePeerId = (addr) => {
const hash = crypto.createHash('sha1');
hash.update(addr);
return hash.digest('hex').slice(0, 20);
};
// not super expensive, but probably worth memoizing
module.exports = (addr) => {
if (memo.size >= 300000) {
memo.clear();
}
if (!memo.has(addr)) {
memo.set(addr, fakePeerId(addr));
}
return memo.get(addr);
};