Break a toy XOR-of-blocks hash: forge a message with a chosen digest by appending one compensating block, and print both messages with their shared digest.
Break a toy XOR-of-blocks hash: forge a message with a chosen digest by appending one compensating block, and print both messages with their shared digest.
Answer
def xor_hash(data, block=4): data = data + b'\x00' * (-len(data) % block) acc = bytearray(block) for i in range(0, len(data), block): for j in range(block): acc[j] ^= data[i + j] return bytes(acc) honest = b'PAY BOB 0010' forged = b'PAY EVE 9999' patch = bytes(a ^ b for a, b in zip(xor_hash(honest), xor_hash(forged))) forged = forged + patch print('honest', honest.decode(), xor_hash(honest).hex()) print('forged', forged[:12].decode() + '+' + patch.hex(), xor_hash(forged).hex()) print('same digest:', xor_hash(honest) == xor_hash(forged))
Stallings & Brown, Computer Security 5e, ch2 §2.2; ch21 §21.1