Adding ELFHash Algorithm (#6731)

* Adding ELFHash Algorithm

Adding a new Hash Algorithm.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update elf.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update elf.py

* Update elf.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update elf.py

* Apply suggestions from code review

Co-authored-by: Caeden <caedenperelliharris@gmail.com>

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Caeden <caedenperelliharris@gmail.com>
This commit is contained in:
Micael Pereira 2022-10-30 10:49:22 +00:00 committed by GitHub
parent 00fc53de97
commit 6b6d8cc111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

23
hashes/elf.py Normal file
View File

@ -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()