从字符串生成哈希,这会依赖于操作系统吗?
当用户输入文本时,我想生成文本的哈希值。我会将这个哈希值存储在数据库中。
然后,每当其他人输入文本时,我都会生成一个散列,并比较对该散列值执行数据库查找,如果存在,我就知道输入的文本是重复的。
.hash 方法足够好吗?它在多个服务器上会保持一致吗?
bio = "my name is blankman"
bio_hash = bio.hash
b = Bio.new()
b.body = bio
b.save unless Bio.find_by_hash(bio_hash)
When a user enters text, I want to generate a hash of the text. I will store this hash in the db.
Then whenever someone else enters text, I will generate a hash and compare perform a db lookup for that hash value, if it exists I know the text entered is a duplicate.
Will the .hash method be good enough for this? Will it be consistant accross multipel servers?
bio = "my name is blankman"
bio_hash = bio.hash
b = Bio.new()
b.body = bio
b.save unless Bio.find_by_hash(bio_hash)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,Ruby 的哈希函数不保证一致。如果您想要一致的哈希函数,请使用 MD5 或为此目的设计的其他函数。
No, Ruby's hashing function is not guaranteed to be consistent. If you want a consistent hashing function, use MD5 or another that is designed for this purpose.
根据问题 Ruby 使用什么哈希函数?,Ruby 使用 Murmur 哈希 其字符串。
您可以在从 此处。从该实现中您可以看到,代码运行可能会根据系统的字节顺序、字节大小和其他因素而有很大差异。
我的 C-fu 不足以证明相同的 Ruby 版本会在不同的系统上产生不同的哈希值,但我当然不会放心地声称它会产生相同哈希值。
Per the question What hash function does Ruby use?, Ruby uses a Murmur hash for its strings.
You can see the implementation of
st_hash
(ultimately used byString#hash
) in the source starting here. From that implementation you can see that the code run may vary greatly based on the endian-ness of the system, the size of bytes, and other things.My C-fu is not sufficient to prove that the same Ruby version would produce different hashes on different systems, but I certainly wouldn't feel comfortable claiming that it would produce the same hash.