Hi all, it may be (or not) a dummy question: how t...
# announcements
g
Hi all, it may be (or not) a dummy question: how to you calculate a unique hash from a data class, in such a way that you have the guarantee (statistically) that two objects are different as soon as their hash are different? (hash may be calculated and stored on different servers at different time)
m
How is this related to Kotlin?
g
The language may provide some helpers? Or maybe you know some helpful library in Kotlin?
b
data class in kotlin implements hashcode by default. It's polynomial hash (a0*x^k+a1*x(k-1)+....+ak) with x=31. Robustness of this hash heavily depends on your usecase. it's only 32bit, so most likely this is not enough for your distributed case.
t
the default
hashCode()
implementation is pretty basic. Good enough for things like HashMaps, but not very collision-proof. For example it generates the same hash for the values
null
,
0
and
""
a
I would say for this you need to go the full monty using SHA256 or something similar.
👍 1
g
ok - thx. It's curious I've not found maintained library to do so
t
Maybe there just isn't much need for such a library. Assuming you're running in JVM, it is pretty easy to write any serializable object to a byte array and use MessageDigest to get a hash of that.
👍 1
a
@Tobias Berger, serialized object sounds like a great idea. Suppose it all depends on the if one has control over the objects being hashed.