Does a string hash exist which can ignore the order of chars in this string

ε祈祈猫儿з 提交于 2020-01-02 04:54:10

问题


Does a string hash exist which can ignore the order of chars in this string? Eg."helloword" and "wordhello" can map into the same bucket.


回答1:


If you don't mind strings like "ac" and "bb" generating the same hash value, you can add the values of the characters together. (a + b + c is equal to a + c + b.)

If you want to reduce the possibility of hash code collisions of that kind, then you can multiply the values of the characters together. (a * b * c is also equal to a * c * b.)

If that's not good enough either, then you can sort all the characters in the string before applying the default string hashing function offered to you by whatever language it is that you are using. (So, both "helloword" ad "wordhello" would become "dehlloorw" before hashing, thus generating the same hash code.)




回答2:


Although the other suggestions of multiplying or adding characters would work, notice that such a hash function is not secure at all.

The reason is that it will introduce a ton of collisions and one of the main properties a hash function has is the low probability of collisions.

For example, a + b + c is the same as c + b + a. However, it is also the same as a + a + d (since the sum of the ascii characters are the same). The same thing applies for multiplying or xor-ing the numbers.

In sum, if you want to achieve a hash function which ignores order, you can but it will introduce a ton of collisions which will potentially make your program faulty and insecure.



来源:https://stackoverflow.com/questions/21545698/does-a-string-hash-exist-which-can-ignore-the-order-of-chars-in-this-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!