fetch-dht/lib/fakePeerId/index.js

21 lines
428 B
JavaScript
Raw Permalink Normal View History

2020-04-12 21:33:32 +00:00
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);
};