java string hash function with avalanche effect

2019-04-15 23:33发布

问题:

In testing with String.hashCode() I noticed that it does not have an avalanche effect. I know that there's a java port of Jenkins hash, but I was wondering if there's a hash function, maybe in some apache library or something, that has this property.

Edit: I'm looking for a function that exhibits this property, and returns a 32-bit (or 64-bit) integer (for example, like Jenkins hash). I'm not using it for cryptography, and I'm not intending to replace String.hashCode in general. I just thought hashCode had this property, and it turns out it doesn't, and I'm wondering if there's anything in java's standard libs or maybe an apache lib, that satisfies my need.

回答1:

The avalanche effect, as described in the wikipedia page you linked to, is an important property of cryptographic hash functions. String.hashCode() is not a cryptographic hash function. Its only goal is to generate sufficiently distributed hash codes for different strings so that HashMap, HashSet and all other hash-based collections are efficient when holding strings.

For cryptographic hash functions, look at JCA, which allows generating SHA-1, MD5, and other cryptographic digests, which all have the effect you're looking for.