64Bit & 32Bit HashCode

时间:2023-03-08 16:20:00
64Bit & 32Bit HashCode
以下为64位和32位的hash值计算方法
public class HashUtil {

    private static final long FNV_64_INIT = 0xcbf29ce484222325L;
private static final long FNV_64_PRIME = 0x100000001b3L; private static final long FNV_32_INIT = 2166136261L;
private static final long FNV_32_PRIME = 16777619; public static Long getUnsignedHash64(final String seed) {
Long res = 0L;
res = FNV_64_INIT;
int len = seed.length();
for (int i = 0; i < len; i++) {
res *= FNV_64_PRIME;
res ^= seed.charAt(i);
}
if (res < 0) res = -res;
return res;
} public static Long getUnsignedHash32(final String seed) {
Long res = 0L;
res = FNV_32_INIT;
int len = seed.length();
for (int i = 0; i < len; i++) {
res *= FNV_32_PRIME;
res ^= seed.charAt(i);
}
if (res < 0) res = -res;
return res;
} }