【发布时间】:2011-05-13 04:19:13
【问题描述】:
有没有我可以使用的 Jenkins Hash 的 javascript 实现——而不是我自己实现它?
我知道有一个 python 实现可以用来编写我自己的 js。但我不是 Javascript 专家,因此我更喜欢别人的实现。
更新:(我确实尝试过转换http://www.burtleburtle.net/bob/c/lookup3.c) 更新 2:此代码为您提供与 lookup3.c 中 hashlittle2 相同的哈希
var Jenkins = {
rot: function(x,k) {
return (x<<k) | (x>>>(32-k));
},
mix: function(a,b,c) {
a = (a - c) | 0; a ^= Jenkins.rot(c, 4); c = (c + b) | 0;
b = (b - a) | 0; b ^= Jenkins.rot(a, 6); a = (a + c) | 0;
c = (c - b) | 0; c ^= Jenkins.rot(b, 8); b = (b + a) | 0;
a = (a - c) | 0; a ^= Jenkins.rot(c,16); c = (c + b) | 0;
b = (b - a) | 0; b ^= Jenkins.rot(a,19); a = (a + c) | 0;
c = (c - b) | 0; c ^= Jenkins.rot(b, 4); b = (b + a) | 0;
return {a : a, b : b, c : c};
},
final: function(a,b,c) {
c ^= b; c -= Jenkins.rot(b,14) | 0;
a ^= c; a -= Jenkins.rot(c,11) | 0;
b ^= a; b -= Jenkins.rot(a,25) | 0;
c ^= b; c -= Jenkins.rot(b,16) | 0;
a ^= c; a -= Jenkins.rot(c,4) | 0;
b ^= a; b -= Jenkins.rot(a,14) | 0;
c ^= b; c -= Jenkins.rot(b,24) | 0;
return {a : a, b : b, c : c};
},
hashlittle2: function(k, initval, initval2) {
var length = k.length;
a = b = c = 0xdeadbeef + length + initval;
c += initval2;
offset = 0;
while (length > 12) {
a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); a = a>>>0;
b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); b = b>>>0;
c += (k.charCodeAt(offset+8) + (k.charCodeAt(offset+9)<<8) + (k.charCodeAt(offset+10)<<16) + (k.charCodeAt(offset+11)<<24)); c = c>>>0;
o = Jenkins.mix(a,b,c);
a = o.a; b = o.b; c = o.c;
length -= 12;
offset += 12;
}
switch(length) {
case 12: c += (k.charCodeAt(offset+8) + (k.charCodeAt(offset+9)<<8) + (k.charCodeAt(offset+10)<<16) + (k.charCodeAt(offset+11)<<24)); b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 11: c += (k.charCodeAt(offset+8) + (k.charCodeAt(offset+9)<<8) + (k.charCodeAt(offset+10)<<16)); b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 10: c += (k.charCodeAt(offset+8) + (k.charCodeAt(offset+9)<<8)); b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 9: c += (k.charCodeAt(offset+8)); b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 8: b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16) + (k.charCodeAt(offset+7)<<24)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 7: b += (k.charCodeAt(offset+4) + (k.charCodeAt(offset+5)<<8) + (k.charCodeAt(offset+6)<<16)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 6: b += ((k.charCodeAt(offset+5)<<8) + k.charCodeAt(offset+4)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 5: b += (k.charCodeAt(offset+4)); a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 4: a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16) + (k.charCodeAt(offset+3)<<24)); break;
case 3: a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8) + (k.charCodeAt(offset+2)<<16)); break;
case 2: a += (k.charCodeAt(offset+0) + (k.charCodeAt(offset+1)<<8)); break;
case 1: a += (k.charCodeAt(offset+0)); break;
case 0: return {b : b, c : c};
}
o = Jenkins.final(a,b,c);
a = o.a; b = o.b; c = o.c;
return {b : b>>>0, c : c>>>0};
}
}
【问题讨论】:
-
来吧,会很有趣的。此外,如果您有现有的有效测试数据来源......:p
-
按位 OR 代替
+在将字节组合成 32 位字时效率显着提高。尽你所能防止 JS 转换回浮点数。您可以在对其进行基准测试时看到差异。您还可以删除break语句并简单地添加相关部分而不是整个内容,因为case 12、case 11可以实现为位掩码。
标签: javascript hash