0

In service A I have a string that get hashed like this:

fun String.toHash(): Long {
    var hashCode = this.hashCode().toLong()
    if (hashCode < 0L) {
        hashCode *= -1
    }
    return hashCode
}

I want to replicate this code in service B written in Golang so for the same word I get the exact same hash. For what I understand from Kotlin's documentation the hash applied returns a 64bit integer. So in Go I am doing this:

func hash(s string) int64 {
    h := fnv.New64()
    h.Write([]byte(s))
    v := h.Sum64()
    return int64(v)
}

But while unit testing this I do not get the same value. I get:

func Test_hash(t *testing.T) {
    tests := []struct {
        input  string
        output int64
    }{
        {input: "papafritas", output: 1079370635},
    }
    for _, test := range tests {
        got := hash(test.input)
        assert.Equal(t, test.output, got)
    }
}

Result:

7841672725449611742

Am I doing something wrong?

2
  • If you don't want to use a standard hash, you can implement the java version yourself: stackoverflow.com/questions/15518418/… Commented Feb 6, 2023 at 20:34
  • @JimB Thanks so much! That was the impulse I needed and a constructive answer. Commented Feb 6, 2023 at 21:16

1 Answer 1

3

Java and therefore Kotlin uses different hash function than Go.

Possible options are:

  1. Use a standard hash function.
  2. Reimplement Java hashCode for Strings in Go.
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I can see that. Is there any way I can replicate "Kotlin's" way in Go? I really do not want to change anything on then Kotlin service
The Java/Kotlin hash algorithm isn't given in the specification, is it? So you can't rely on it — it coud change between JVM implementations (or even different versions of the same JVM). If you need a specific algorithm, I think it's far safer to implement it yourself.
@gidds: I'm not sure where java draws the documented versus specified line, but the algorithm is documented (but I do agree using a standardized hash would make more sense)
@JimB Ah, so it is documented! Thanks for the link. (I wasn't sure, hence the question :-) So it would be safe to re-implement it. Though I guess if you have to implement the hash in Go anyway, it's probably clearer (and easier to debug) if you pick your own algorithm and implement it in both languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.