Logo
Explore Help
Register Sign In
TheAlgorithms/Python
1
0
Fork 0
You've already forked Python
Code Issues Pull Requests Actions 2 Packages Projects Releases Wiki Activity
Files
ab9d8f3874ba550bea0103e0891160b8d9145208
Python/hashes/elf.py

24 lines
483 B
Python
Raw Normal View History

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>
2022-10-30 10:49:22 +00:00
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()
Reference in New Issue Copy Permalink
Powered by Gitea Version: 1.24.6 Page: 88ms Template: 2ms
English
Bahasa Indonesia Deutsch English Español Français Gaeilge Italiano Latviešu Magyar nyelv Nederlands Polski Português de Portugal Português do Brasil Suomi Svenska Türkçe Čeština Ελληνικά Български Русский Українська فارسی മലയാളം 日本語 简体中文 繁體中文(台灣) 繁體中文(香港) 한국어
Licenses API