Skip to content

Instantly share code, notes, and snippets.

@peter
Created December 17, 2020 14:55
Embed
What would you like to do?
// See: https://medium.com/@promentol/cryptography-for-javascript-node-js-developers-part-1-hash-function-86d119c7304
function hash(data, digest = 'hex') {
return require('crypto').createHash('sha256').update(data).digest(digest)
}
// ('foo', 10) => 6
// ('bar', 10) => 6
// ('zz', 10) => 2
// ('foo', 6) => 2
// ('bar', 6) => 4
// ('foo', 20) => 16
// NOTE: probably better to use https://en.wikipedia.org/wiki/Consistent_hashing
function selectPartition(data, nPartitions) {
return parseInt(hash(data), 16) % nPartitions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment