function generateCRC16(payload) {
    let crc = 0xFFFF;
    for (let i = 0; i < payload.length; i++) {
        let c = payload.charCodeAt(i);
        crc ^= (c << 8);
        for (let j = 0; j < 8; j++) {
            if ((crc & 0x8000) !== 0) {
                crc = (crc << 1) ^ 0x1021;
            } else {
                crc = crc << 1;
            }
        }
        crc = crc & 0xFFFF; 
    }
    return crc.toString(16).toUpperCase().padStart(4, '0');
}

const payload = "00020126360014br.gov.bcb.pix0114+5511961534225520400005303986540520.005802BR5917Ricardo Goncalves6021Sao Bernardo do campo62070503***6304";
console.log("Payload:", payload);
console.log("Calculated CRC:", generateCRC16(payload));
console.log("Expected CRC:", "407E");
