admin管理员组文章数量:1289542
I'm looking for a simple hash algroithm that will give me one byte of output for a string input (the inputs will be RFC822 email addresses, if that helps).
I'd like it to be simple, fast, and to magnify input differences (so two similar addresses have differnt outputs). (Yes, I am asking for a lot in one byte of output.)
Idealy, I'd like an XSL answer, but I can take it in either Java or Javascript (and then pass the hash as an argument to the XSL processor).
Thanks.
I'm looking for a simple hash algroithm that will give me one byte of output for a string input (the inputs will be RFC822 email addresses, if that helps).
I'd like it to be simple, fast, and to magnify input differences (so two similar addresses have differnt outputs). (Yes, I am asking for a lot in one byte of output.)
Idealy, I'd like an XSL answer, but I can take it in either Java or Javascript (and then pass the hash as an argument to the XSL processor).
Thanks.
Share Improve this question edited Dec 28, 2009 at 17:37 Brian Agnew 272k38 gold badges340 silver badges442 bronze badges asked Dec 28, 2009 at 17:29 Moose MoralsMoose Morals 1,6581 gold badge27 silver badges35 bronze badges 2- If you want an XSLT based solution, then does it really matter if it results in 1 byte? – Mads Hansen Commented Dec 28, 2009 at 22:15
- 2 Perhaps en.m.wikipedia/wiki/Pearson_hashing ? – patricksurry Commented Jul 25, 2023 at 14:00
4 Answers
Reset to default 3Why not take the most/least significant byte of the standard String hashCode()
function ?
Every hash function has its strengths and weaknesses, and fast and easy to pute ones tend to behave badly for certain classes of data. Trial and error needs to be a part of any solution. In addition to the other suggestions, you might try using integer multiplication as part of the hash function, for example
hash = 0
for (int i=0; i<data.length; i++)
hash = ((37 * hash) + data[i]) & 0xff;
Use a CRC-8, which has 9 bits of information, then drop a bit off either end and call it a day. Otherwise use any of the other mon CRC algorithms.
My suggestion would be to simply XOR all the bytes in the string. Every bit of every byte will influence the end result, and any single-bit error will definitely cause the hash to differ.
Very simple, very fast. And probably nearly as good as any other solution, given the small number of result bits.
本文标签: javaSimple hash function (1 byte output from string input)Stack Overflow
版权声明:本文标题:java - Simple hash function (1 byte output from string input) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741408246a2377077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论