Socket hex with checksum

0

I'm developing a communication socket between a crawler and the server. The socket is in perfect working order.

My problem is with communication when checking the checksum. The device sends a message ranging from 0 to 200 bytes to the server that converting to hexadecimal: 2929b1000725cadc610ce80d that I transform into an array that looks like this:

{["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]} , the 10th position of the array is the e8 checksum that is calculated with an XOR sum, like this: 29 Xor 29 Xor b1 Xor 00 Xor 07 Xor 25 Xor ca Xor dc Xor 61 Xor 0c = e8 .

Soon I developed the following excerpt to make this sum:

// transformando a 1ª posição em bit
$checksum = pack('H*',$data[0]);
// correndo o array
for ($i = 1; $i < count($data)-2; $i++) {
  // Fazendo a soma XOR 
  $checksum ^= pack('H*',$data[$i]);
}
return $checksum;

where $data is the array I showed above. The result you are giving is d7 ;

the values I showed are print_r I gave in the variables.

What's wrong?

I put the script to run and I noticed that it is giving an irregularity in the result.

01-12-17 03:33:38pm Connection from 186.227.156.12:18747
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
01-12-17 03:33:38pm CheckSUM: cd
01-12-17 03:33:48pm Connection from 186.227.156.12:18747
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
01-12-17 03:33:48pm CheckSUM: 6b
01-12-17 03:33:58pm Connection from 186.227.156.12:18747
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
01-12-17 03:33:58pm CheckSUM: 6b
01-12-17 03:34:08pm Connection from 186.227.156.12:18747
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
01-12-17 03:34:08pm CheckSUM: 6b
01-12-17 03:34:17pm Connection from 186.227.156.12:18747
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
01-12-17 03:34:17pm CheckSUM: 6b
01-12-17 03:34:17pm Connection to 186.227.156.12:18747 closed
Handle ...
01-12-17 03:34:20pm Connection from 186.227.156.12:23463
 Interact ...
01-12-17 03:34:22pm Connection from 186.227.156.12:23463
 RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
 IMEI -- {37749297}
    
asked by anonymous 01.12.2017 / 18:17

1 answer

0

When reading the PHP documentation regarding the XOR function, I noticed that the language does not distinguish string, decimal or hexadecimal. And as I've noticed in many cases, calculating in decimal ends up being even easier for us. So it replaces the following excerpt:

function checkSum($data){
// Realizo a conversão de Hexadecimal para Decimal:
$checksum = hexdec($data[0]);
// Corro o array para calcular.
for ($i = 1; $i < count($data)-2; $i++) {
  // faço a soma xor convertendo para decimal
  $checksum ^= hexdec($data[$i]);
  // Uso minha função para registrar o log em arquivo.
  printLog(null, date("d-m-y h:i:sa") . " - " .$data[$i]." Somando XOR: ".dechex($checksum));

}
// Retorno convertendo para hexadecimal:
return dechex($checksum);

}

That way it worked and is running perfectly.

    
01.12.2017 / 20:04