From 38bd19fd89b09ddd4ba4e7860fec439d13f29cb8 Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 20:29:43 +0100 Subject: [PATCH 1/9] Adding ELFHash Algorithm Adding a new Hash Algorithm. --- hashes/elf.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 hashes/elf.py diff --git a/hashes/elf.py b/hashes/elf.py new file mode 100644 index 000000000000..2b0c73ee6ae0 --- /dev/null +++ b/hashes/elf.py @@ -0,0 +1,12 @@ +def ELFHash(data): + """ + Implementation of ElfHash Algorithm, a variant of PJW hash function. + """ + hash = x = i = 0 + for i in range(len(data)): + hash = (hash << 4) + ord(data[i]) + x = hash & 0xF0000000 + if x != 0: + hash ^= (x >> 24) + hash &= ~x + return hash \ No newline at end of file From fbac99b942251f4e9d3d4c5717cac80d2a228dc3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 19:34:02 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/elf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hashes/elf.py b/hashes/elf.py index 2b0c73ee6ae0..d147c429f345 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -7,6 +7,6 @@ def ELFHash(data): hash = (hash << 4) + ord(data[i]) x = hash & 0xF0000000 if x != 0: - hash ^= (x >> 24) + hash ^= x >> 24 hash &= ~x - return hash \ No newline at end of file + return hash From 7666accdfd554f5eb44861cdf3f35a7abb693b7c Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:35:19 +0100 Subject: [PATCH 3/9] Update elf.py --- hashes/elf.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/hashes/elf.py b/hashes/elf.py index 2b0c73ee6ae0..55fbee458562 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -1,6 +1,11 @@ -def ELFHash(data): +def elf_hash(data): """ Implementation of ElfHash Algorithm, a variant of PJW hash function. + + Returns: + [int] -- [32 bit binary int] + >>> elf_hash('lorem ipsum') + '253956621' """ hash = x = i = 0 for i in range(len(data)): @@ -9,4 +14,9 @@ def ELFHash(data): if x != 0: hash ^= (x >> 24) hash &= ~x - return hash \ No newline at end of file + return hash + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 71b5e8eeb36fcc155554502de831110ed7cb3117 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 20:38:19 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/elf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hashes/elf.py b/hashes/elf.py index ba38797886af..c59aebeed51d 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -16,7 +16,8 @@ def elf_hash(data): hash &= ~x return hash + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 1f03436d4d11b0adac524e8337bb275e9fa42348 Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:40:56 +0100 Subject: [PATCH 5/9] Update elf.py --- hashes/elf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/elf.py b/hashes/elf.py index ba38797886af..a4f13ab3db56 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -5,7 +5,7 @@ def elf_hash(data): Returns: [int] -- [32 bit binary int] >>> elf_hash('lorem ipsum') - '253956621' + 253956621 """ hash = x = i = 0 for i in range(len(data)): From 09a6e1cb6a8dba8ae5cb0f77ac246cec34451705 Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:45:56 +0100 Subject: [PATCH 6/9] Update elf.py --- hashes/elf.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hashes/elf.py b/hashes/elf.py index e48284f4849f..9dc16d881a68 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -1,4 +1,4 @@ -def elf_hash(data): +def elf_hash(data: str): """ Implementation of ElfHash Algorithm, a variant of PJW hash function. @@ -16,8 +16,7 @@ def elf_hash(data): hash &= ~x return hash - if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From 636de59e9bd7cffc189f8f6b70f28a74f2cccde7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 20:46:55 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/elf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hashes/elf.py b/hashes/elf.py index 9dc16d881a68..01cbe847821b 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -16,7 +16,8 @@ def elf_hash(data: str): hash &= ~x return hash + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From efc745c6f7bc377023707f99b3b63a616abf5cca Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:47:47 +0100 Subject: [PATCH 8/9] Update elf.py --- hashes/elf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/elf.py b/hashes/elf.py index 9dc16d881a68..d0ae1ec1d0a6 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -1,4 +1,4 @@ -def elf_hash(data: str): +def elf_hash(data: str) -> int: """ Implementation of ElfHash Algorithm, a variant of PJW hash function. From 62eefeb52e19769acf5f880f269ab927ad6a371e Mon Sep 17 00:00:00 2001 From: Micael Pereira <8707982+micaelalex@users.noreply.github.com> Date: Wed, 5 Oct 2022 22:01:05 +0100 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Caeden --- hashes/elf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hashes/elf.py b/hashes/elf.py index ad697893188f..87fe339da44d 100644 --- a/hashes/elf.py +++ b/hashes/elf.py @@ -7,9 +7,9 @@ def elf_hash(data: str) -> int: >>> elf_hash('lorem ipsum') 253956621 """ - hash = x = i = 0 - for i in range(len(data)): - hash = (hash << 4) + ord(data[i]) + hash = x = 0 + for letter in data: + hash = (hash << 4) + ord(letter) x = hash & 0xF0000000 if x != 0: hash ^= x >> 24