Note that there are some explanatory texts on larger screens.

plurals
  1. POA particular type of hash on a String concatenation
    primarykey
    data
    text
    <p>I need a specialised hash function h(X,Y) in Java with the following properties.</p> <ol> <li>X and Y are strings.</li> <li>h(X,Y) = h(Y,X).</li> <li>X and Y are arbitrary length strings and there is no length limit on the result of h(X,Y) either.</li> <li>h(X,Y) and h(Y,X) should not collide with h(A,B) = h(B,A) if X is not equal to A and Y is not equal to B.</li> <li>h() does not need to be a secure hash function unless it is necessary to meet the aforementioned requirements.</li> <li>Fairly high-performant but this is an open-ended criterion.</li> </ol> <p>In my mind, I see requirements 2 and 4 slightly contradictory but perhaps I am worrying too much.</p> <p>At the moment, what I am doing in Java is the following:</p> <pre><code>public static BigInteger hashStringConcatenation(String str1, String str2) { BigInteger bA = BigInteger.ZERO; BigInteger bB = BigInteger.ZERO; for(int i=0; i&lt;str1.length(); i++) { bA = bA.add(BigInteger.valueOf(127L).pow(i+1).multiply(BigInteger.valueOf(str1.codePointAt(i)))); } for(int i=0; i&lt;str2.length(); i++) { bB = bB.add(BigInteger.valueOf(127L).pow(i+1).multiply(BigInteger.valueOf(str2.codePointAt(i)))); } return bA.multiply(bB); } </code></pre> <p>I think this is hideous but that's why I am looking for nicer solutions. Thanks.</p> <p>Forgot to mention that on a 2.53GHz dual core Macbook Pro with 8GB RAM and Java 1.6 on OS X 10.7, the hash function takes about 270 micro-seconds for two 8 (ASCII) character Strings. I suspect this would be higher with the increase in the String size, or if Unicode characters are used.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload