longchute

about

Simple XOR Hash

02 Jun 2012

NOTE: I originally posted this on Snipplr.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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]))