Hash一致性及均衡算法
1,计算目标的HASH值
static ulong CalHashCode(byte[] bytes) { using (var hash = System.Security.Cryptography.MD5.Create()) { var data = hash.ComputeHash(bytes); var a = BitConverter.ToUInt64(data, 0); var b = BitConverter.ToUInt64(data, 8); ulong hashCode = a ^ b; return hashCode; } }
2,计算目标均衡结果
// 定义桶数量 ulong slot = 8; var hashCode = CalHashCode({YOU DATA}); // 计算目标结果 var result = (int)(hashCode % slot);
Categories:
.net
Tags:
hash
已禁用评论。