Return to Snippet

Revision: 57588
at June 2, 2012 00:20 by weilawei


Initial Code
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]))

Initial URL


Initial Description
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.

Initial Title
Simple XOR Hash

Initial Tags


Initial Language
Python