简体   繁体   中英

Java String to short hash code

I want to "implement" a hash function from Strings to shorts, using the java standard hashCode() function of String object. I came up with the following simple implementation:

static short shortHashCode(String str)
{
   int strHashCode = str.hashCode();
   short shorterHashCode = (short) (strHashCode % Short.MAX_VALUE);
   return shorterHashCode;
}
  1. Is my shortHashCode function a good hash function? Meaning is the chance of collisions small (chance that two different Strings will have the same hash code close to 1/Short.MAX_VALUE) ?
  2. Is there a better way to implement hash function from Strings to shorts?
(short) (strHashCode % Short.MAX_VALUE);

is losing information unnecessarily.

 (short) (strHashCode % ((Short.MAX_VALUE + 1) << 1));

would not, but would be equivalent anyway to

 (short) strHashCode

since casting an integral type to a smaller integral type just truncates the most significant bits.


It also assumes that all bits have the same entropy, which may not be true. You could try and spread the entropy around:

 (short) (strHashCode ^ (strHashCode >>> 16))

which XORs the high 16 bits with the low 16 bits.


Meaning is the chance of collisions small (chance that two different Strings will have the same hash code close to 1/Short.MAX_VALUE) ?

java.lang.String.hashCode is not a cryptographically strong hash function , so it only has that property if an attacker can't control one or both inputs to force a collision.

If you expose it to strings from an untrusted source, you might see a much higher rate of hash collisions, possibly allowing an attacker to deny service.

Also, it is designed to tradeoff a small increase in collision rate for better performance, and cross-version stability. There are better string hashing functions out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM