0

I need simple hash function that takes two parameters, with the following parameters:

  • a,b,c,d are different strings (approximately 30 characters)
  • f(a,b) = f(b,a)
  • f(a,c) ≠ f(a,d)
  • f(c,b) ≠ f(d,b)
ghi
  • 697
  • 2
  • 9
  • 20

1 Answers1

2

Sort and concatenate the two parameters (to ensure that f(a,b) equals f(b,a). Since there are only two items to sort the result will be either ab or ba.

If the strings have the property that ab may equal cd (for example strong + hearted and strongheart + ed) you may want to "salt" the string by prepending it with the length of the first string coded in a fixed number of bytes.

Then apply a string hash on the result. There are numerous examples online.

Note that there is no guarantee that two different strings won't result in the same hash value, but a good hash algorithm will reduce the probability.

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 1
    Why was this downvoted? Is this answer incorrect? It looks correct to me (my first thought was to do this exact thing). – Dogbert Oct 04 '16 at 13:46
  • 1
    In addition to sorting, it's a good precaution to prepend the strings before concatenation with the length of the strings. This is to prevent finding trivial collisions like `f("abc", "def") = f("ab", "cdef")`. I'd suggest prepending 4-byte int (instead of length in ascii string). So for example: `f(a,b) = h(len(a).to_bytes(4) + a + len(b).to_bytes(4) + b)` where a < b. – Lie Ryan Oct 05 '16 at 09:06
  • 1
    @LieRyan Thanks! I have improved my answer. (it is actually sufficient to include the length of the first string). – Klas Lindbäck Oct 05 '16 at 10:14