-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathjoaat.py
More file actions
39 lines (29 loc) · 937 Bytes
/
joaat.py
File metadata and controls
39 lines (29 loc) · 937 Bytes
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
"""
The Bob Jenkins hash is a fast, non-cryptographic hash function
designed for general-purpose use, such as hash table lookups.
source: https://en.wikipedia.org/wiki/Jenkins_hash_function
"""
def joaat(key: str) -> int:
"""
Calculate Jenkins One-at-a-Time hash for a key.
>>> joaat("apple")
2297466611
>>> joaat("test")
1064684737
>>> joaat("")
0
"""
hash_value = 0
mask = 0xFFFFFFFF
key_bytes = key.encode("utf-8")
for byte in key_bytes:
hash_value = (hash_value + byte) & mask
hash_value = (hash_value + (hash_value << 10)) & mask
hash_value = (hash_value ^ (hash_value >> 6)) & mask
hash_value = (hash_value + (hash_value << 3)) & mask
hash_value = (hash_value ^ (hash_value >> 11)) & mask
hash_value = (hash_value + (hash_value << 15)) & mask
return hash_value
if __name__ == "__main__":
import doctest
doctest.testmod()