-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.py
53 lines (44 loc) · 1.24 KB
/
hashing.py
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
49
50
51
52
53
"""
CS 5008
Fall 2023
Final Research Project
Marian Padron
Hashing file, contains different hashing functions. Primarily taken from hashing lab from class.
"""
FNV_PRIME_32 = 16777619
FNV_OFFSET_32 = 2166136261
DJB2_PRIME = 5381
def simple_hash(key):
"""The simple hash, for each character in key, sum the ascii values
return that sum as the hash"""
hash_val = 0
for char in key:
hash_val += ord(char)
return hash_val
def djb2(key):
"""Start with prime number DJB2_PRIME as hash
for every character in key,
hash = hash * 33 + c
return hash"""
hash_val = DJB2_PRIME
for char in key:
hash_val = ((hash_val << 5) + hash_val) + ord(char)
return hash_val
def fnv_hash(key):
"""FNV hash function"""
hash_val = FNV_OFFSET_32
for char in key:
hash_val ^= ord(char)
hash_val *= FNV_PRIME_32
return hash_val
def jenkins_one_at_a_time_hash(key):
"""Jenkins one-at-a-time hash function"""
hash_val = 0
for char in key:
hash_val += ord(char)
hash_val += (hash_val << 10)
hash_val ^= (hash_val >> 6)
hash_val += (hash_val << 3)
hash_val ^= (hash_val >> 11)
hash_val += (hash_val << 15)
return hash_val