Created
February 6, 2014 15:57
-
-
Save cameronp98/8846982 to your computer and use it in GitHub Desktop.
An attempt at a hash function
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
| def msum(l): | |
| "Product of elements in l" | |
| x = 1 | |
| for i in l: | |
| x *= i | |
| return x | |
| def make_blocks(s, size): | |
| "Create blocks from s of specified size" | |
| for i in range(0, len(s), size): | |
| block = s[i:i+size] | |
| while len(block) < size: | |
| block += chr(95 + len(block)) # 95: arbitrary ascii number | |
| yield block | |
| def combine(blocks, block_size): | |
| "Combine the blocks" | |
| h = 1 | |
| for block in blocks: | |
| h ^= msum(map(ord, block)) | |
| return str(hex(h))[2:] | |
| def hash_(string, block_size=16): | |
| blocks = make_blocks(string, block_size) | |
| return combine(blocks, block_size) | |
| def main(): | |
| print(hash_("Testing the hash function")) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment