# Posted By

weilawei on 06/02/12

# Statistics

Viewed 274 times
Favorited by 0 user(s)

# Simple XOR Hash

/ Published in: Python Save to your folder(s) This is a simple hash that pads its input to the block size and XORs every block together. Output is in hexadecimal octets. Probability of collisions is extremely high and they are easy to calculate, although the function is one-way, so this is more (though, not very) useful as a checksum.

Copy this code and paste it in your HTML
`def xor_hash(a_value, block_size=16):    value_size  = len(a_value)    padding     = value_size % block_size    padding     = 0 if padding == 0 else block_size - padding    value_size  += padding     if (padding != 0):        a_value += b'\x00' * padding     retval = bytearray(bytes(block_size))     for a_block in range(1, value_size, block_size):        a_block = a_value[(a_block-1):(a_block+block_size-1)]         for i in range(0, block_size):            retval[i] ^= a_block[i]     return retval if (__name__ == "__main__"):    import sys     a_value = bytearray(' '.join(sys.argv[1:]), encoding="utf-8")    a_hash  = xor_hash(a_value)    print(''.join(["%0.2x" % a_byte for a_byte in a_hash]))`

## Comments Subscribe to comments

You need to login to post a comment.