Socket Hex with checksum

Asked

Viewed 80 times

0

I’m developing a communication socket between a tracker and the server. The socket is working perfectly.

My problem is with communication when it comes to checking the checksum. The device sends a message ranging from 0 to 200 bytes to the server converting to hexadecimal of the: 2929b1000725cadc610ce80d which I turn 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 checksum e8 which is calculated with a XOR sum, thus: 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 accomplish 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 that this giving is d7;

*Obs. the values I have shown are print_r that I gave in the variables.

What is wrong?

I put the script to run and noticed that it is giving 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}

1 answer

0


When reading the PHP documentation regarding the XOR function, I noticed that the language does not distinguish between string, decimal or hexadecimal. And as I have noticed in several cases, calculating in decimal ends up being even easier for us. So replace with the following section:

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);

}

This way it worked and is running perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.