Skip to content

Commit 6b6d8cc

Browse files
micaelalexpre-commit-ci[bot]CaedenPH
authored
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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Caeden <[email protected]>
1 parent 00fc53d commit 6b6d8cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

hashes/elf.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def elf_hash(data: str) -> int:
2+
"""
3+
Implementation of ElfHash Algorithm, a variant of PJW hash function.
4+
5+
Returns:
6+
[int] -- [32 bit binary int]
7+
>>> elf_hash('lorem ipsum')
8+
253956621
9+
"""
10+
hash = x = 0
11+
for letter in data:
12+
hash = (hash << 4) + ord(letter)
13+
x = hash & 0xF0000000
14+
if x != 0:
15+
hash ^= x >> 24
16+
hash &= ~x
17+
return hash
18+
19+
20+
if __name__ == "__main__":
21+
import doctest
22+
23+
doctest.testmod()

0 commit comments

Comments
 (0)