/* Encrypts input where each byte is XOR'd with the previous encrypted byte. * @alias module:tplink-smarthome-crypto.encrypt * @param {(Buffer|string)} input Buffer/string to encrypt * @param {number} [firstKey=0xAB] * @return {Buffer} encrypted buffer */ export const encrypt = (input, firstKey = 0xab) => { const buf = Buffer.from(input); let key = firstKey; for (let i = 0; i < buf.length; i++) { buf[i] = buf[i] ^ key; key = buf[i]; } return buf; }; /** * Decrypts input where each byte is XOR'd with the previous encrypted byte. * @alias module:tplink-smarthome-crypto.decrypt * @param {Buffer} input encrypted Buffer * @param {number} [firstKey=0xAB] * @return {Buffer} decrypted buffer */ export function decrypt(input, firstKey = 0xab) { const buf = Buffer.from(input); let key = firstKey; let nextKey; for (let i = 0; i < buf.length; i++) { nextKey = buf[i]; buf[i] = buf[i] ^ key; key = nextKey; } return buf; }