Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Created February 6, 2014 15:57
Show Gist options
  • Select an option

  • Save cameronp98/8846982 to your computer and use it in GitHub Desktop.

Select an option

Save cameronp98/8846982 to your computer and use it in GitHub Desktop.
An attempt at a hash function
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