HashCode.java#L292 | padToLong() | String Max length check
ramkumarbj opened this issue · comments
Hi team,
We are using the Guava lib for Hashing. The padToLong() method is considering only the first 8 digits to generate the hashing.
Eventhough we are passing the 10 digits its considering the first 8 digits, can you please let us know why team is considering only first 8 digits and do we have option to override this to consider the full string...
https://github.com/google/guava/blob/master/guava/src/com/google/common/hash/HashCode.java#L292
Hashing.consistentHash(HashCode.fromBytes(StringWith10Digits.toByteArray()), bucket) + 1
for example: Will get same value for all the below combinations.
- 12345678
- 1234567890
- 1234567898
- 1234567897
If you just want to hash a byte array, you should use Arrays.hashCode(...)
instead.
padToLong()
appears to be working as intended: it pads the byte array to the size of a long, which is 8 bytes.
I think HashCode.fromBytes(...)
is assuming the input byte array is already the result of some kind of hashing and should therefore be somewhat uniformly distributed. It does not actually perform any kind of hashing. You should use Arrays.hashCode(...)
for that instead. You can then convert that to a HashCode
with HashCode.fromInt(Arrays.hashCode(...))
.
Hashing.consistentHash(...)
is then supposed to take that HashCode
and assign it to some bucket, which should probably work if HashCode
is actually a hash value. It won't work in your example because HashCode.fromBytes(StringWith10Digits.toByteArray())
is not actually a hash of the string.