-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathFNV1a.java
48 lines (42 loc) · 1.51 KB
/
FNV1a.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.common.hash;
import java.nio.charset.StandardCharsets;
/**
* Provides hashing function using FNV1a hash function. @see <a href="http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1">FNV author's website</a>.
* 32 bit Java port of http://www.isthe.com/chongo/src/fnv/hash_32a.c
* 64 bit Java port of http://www.isthe.com/chongo/src/fnv/hash_64a.c
*
* @opensearch.internal
*/
public class FNV1a {
private static final long FNV_OFFSET_BASIS_32 = 0x811c9dc5L;
private static final long FNV_PRIME_32 = 0x01000193L;
private static final long FNV_OFFSET_BASIS_64 = 0xcbf29ce484222325L;
private static final long FNV_PRIME_64 = 0x100000001b3L;
// FNV-1a hash computation for 32-bit hash
public static long hash32(String input) {
long hash = FNV_OFFSET_BASIS_32;
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
hash ^= (b & 0xFF);
hash *= FNV_PRIME_32;
}
return hash;
}
// FNV-1a hash computation for 64-bit hash
public static long hash64(String input) {
long hash = FNV_OFFSET_BASIS_64;
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
hash ^= (b & 0xFF);
hash *= FNV_PRIME_64;
}
return hash;
}
}