diff --git a/hashes/elf.py b/hashes/elf.py new file mode 100644 index 000000000000..87fe339da44d --- /dev/null +++ b/hashes/elf.py @@ -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()