Skip to content

implement sdbm hash algorithm #2094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions conversions/decimal_to_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def decimal_to_any(num: int, base: int) -> str:
for base in range(2, 37):
for num in range(1000):
assert int(decimal_to_any(num, base), base) == num, (
num, base, decimal_to_any(num, base),
int(decimal_to_any(num, base), base)
)
num,
base,
decimal_to_any(num, base),
int(decimal_to_any(num, base), base),
)
32 changes: 32 additions & 0 deletions hashes/sdbm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library.
It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits.
It also happens to be a good general hashing function with good distribution.
The actual function (pseudo code) is:
for i in i..len(str):
hash(i) = hash(i - 1) * 65599 + str[i];

What is included below is the faster version used in gawk. [there is even a faster, duff-device version]
The magic constant 65599 was picked out of thin air while experimenting with different constants.
It turns out to be a prime.
This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.

source: http://www.cse.yorku.ca/~oz/hash.html
"""


def sdbm(plain_text: str) -> str:
"""
Function implements sdbm hash, easy to use, great for bits scrambling.
iterates over each character in the given string and applies function to each of them.

>>> sdbm('Algorithms')
1462174910723540325254304520539387479031000036

>>> sdbm('scramble bits')
730247649148944819640658295400555317318720608290373040936089
"""
hash = 0
for plain_chr in plain_text:
hash = ord(plain_chr) + (hash << 6) + (hash << 16) - hash
return hash