用字符串播种 prng 的良好哈希算法是什么?
我正在寻找一种哈希算法,可以生成 31/32 位有符号/无符号整数作为 utf8 字符串的摘要,目的是使用输出来播种 prng,例如 Park-Miller-Carta LCG 或 Mersenne-龙卷风。
我研究过 FNV1 和 FNV1a,但它们为最后一个字符不同的相似字符串提供了非常接近的值; 我希望有一个低冲突哈希,只要对输入字符串进行最小的修改,它就会发生根本性的变化。 性能不是问题。
我当前的方法包括使用字符代码和素数作为乘数的脏 LCG:
a = 524287;
for ( i = 0; i < n; i ++ )
a = ( a * string.charCodeAt ( i ) * 16807 + 524287 ) % 2147483647;
请让我知道任何更好的替代方案。
I am looking for a hashing algorithm that produces a 31/32 bit signed/unsigned integer as a digest for a utf8 string with the purpose of using the output for seeding a prng, such as a Park-Miller-Carta LCG or a Mersenne-Twister.
I have looked into FNV1 and FNV1a, but they provide very close values for similar strings differing in their last character; I would like to have a low collision hash that radically changes upon minimal modifications on the input string. Performance is not an issue.
My current approach consists in a dirty LCG that uses character codes and a prime number as multipliers:
a = 524287;
for ( i = 0; i < n; i ++ )
a = ( a * string.charCodeAt ( i ) * 16807 + 524287 ) % 2147483647;
Please let me know of any better alternatives.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 SHA-2
它是最好/最新的那里有哈希算法。 始终建议使用标准算法。
Use SHA-2
It is the best/latest hashing algorithm out there. It is always advisable to go with standard algorithms.
如果您要生成 32 位值,请考虑使用经典 CRC32。 FNV 应该是 CRC 的快速替代品,并且您说性能不是问题。
If you're generating 32-bit value, consider using classic CRC32. FNV is suposed to be fast alternative to CRC, and you're saying, that performance is not an issue.
任何加密强哈希都将具有您想要的属性,但会生成更多位,但将结果简单截断为 32 位就可以了。 我认为加密强度不是实际要求,因此像 MD5 这样有缺陷的(但广泛使用的)哈希方案就足够了 - 并且在许多库中都很容易获得。
Any cryptographically strong hash will have the properties you want, but generate more bits, but simple truncation of the result to 32 bits would be fine. I presume cryptographic strength is not an actual requirement so that flawed (but widely used) hash schemes like MD5 would be adequate - and readily available in many libraries.