/** * XOR obfuscation applied to the compressed payload of a container. * * The first 16 bytes are XORed with a fixed key; if the payload is at least 32 bytes long, * the 16 bytes ending at the last 4-byte boundary are XORed with the same key words in * reverse order. The operation is its own inverse. */ const KEY_WORDS = ['55aa55aa', '0ff00ff0', '55aa55aa', '48124812'].map((word) => Buffer.from(word, 'hex')); const HEAD_KEY = Buffer.concat(KEY_WORDS); const TAIL_KEY = Buffer.concat([...KEY_WORDS].reverse()); const SIZE_MASK = 0x03ff_ffff; export function toggleObfuscation(payload: Uint8Array): Uint8Array { const buffer = Uint8Array.from(payload); const size = buffer.length & SIZE_MASK; if (size >= 16) xorInto(buffer, 0, HEAD_KEY); if (size >= 32) xorInto(buffer, (size & 0x03ff_fffc) - 16, TAIL_KEY); return buffer; } function xorInto(buffer: Uint8Array, start: number, key: Uint8Array): void { key.forEach((keyByte, index) => { buffer[start + index]! ^= keyByte; }); }