UNPKG

@omneedia/socketcluster

Version:

SocketCluster - A Highly parallelized WebSocket server cluster to make the most of multi-core machines/instances.

24 lines (18 loc) 511 B
var crypto = require('crypto'); function Hasher() {} Hasher.prototype.hashToIndex = function (key, modulo) { var ch; var hash = key; for (var i = 0; i < key.length; i++) { ch = key.charCodeAt(i); hash = ((hash << 5) - hash) + ch; hash = hash & hash; } return Math.abs(hash || 0) % modulo; }; Hasher.prototype.hashToHex = function (key, algorithm) { var hasher = crypto.createHash(algorithm || 'md5'); hasher.update(key); return hasher.digest('hex'); }; module.exports = Hasher;