Skip to content

Adding ELFHash Algorithm #6731

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 12 commits into from
Oct 30, 2022
23 changes: 23 additions & 0 deletions hashes/elf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def elf_hash(data: str) -> int:
"""
Implementation of ElfHash Algorithm, a variant of PJW hash function.

Returns:
[int] -- [32 bit binary int]
>>> elf_hash('lorem ipsum')
253956621
"""
hash = x = 0
for letter in data:
hash = (hash << 4) + ord(letter)
x = hash & 0xF0000000
if x != 0:
hash ^= x >> 24
hash &= ~x
return hash


if __name__ == "__main__":
import doctest

doctest.testmod()