Last active
February 26, 2023 21:58
-
-
Save mourginakis/131a1fbf23a69f5fdc2789ceb4d07c5c to your computer and use it in GitHub Desktop.
jenkins one at a time hash in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
def string_to_byte_array(s : str) -> np.ndarray: | |
return np.array([ord(c) for c in s], dtype=np.uint8) | |
def jenkins_one_at_a_time(bytes: np.ndarray[np.uint8]) -> np.uint32: | |
hash = np.uint32(0) | |
for byte in bytes: | |
hash += np.uint8(byte) | |
hash += np.uint32(hash) << np.uint8(10) | |
hash ^= np.uint32(hash) >> np.uint8(6) | |
hash += np.uint32(hash) << np.uint8(3) | |
hash ^= np.uint32(hash) >> np.uint8(11) | |
hash += np.uint32(hash) << np.uint8(15) | |
return hash | |
np.seterr(over='ignore') | |
## Assertions | |
s1 = "a" | |
b1 = string_to_byte_array(s1) | |
a1 = jenkins_one_at_a_time(b1) | |
assert (a1 == 0xca2e9442) | |
s2 = "The quick brown fox jumps over the lazy dog" | |
b2 = string_to_byte_array(s2) | |
a2 = jenkins_one_at_a_time(b2) | |
assert (a2 == 0x519e91f5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment