ISO Checksum

Background

Checksums are traditional Data Communication tools that let you determine if there has been a transmission error.

Most checksum algorithms generate unique values for the "packet" of data being sent, then send these values along with the data. At the receiving end, the system checks that the data generates the SAME unique values, meaning there was no transmission error.

This version is a simple ISO Checksum, demonstrated using a small "packet" of integers, and 2 checksums.

Code

Let's try it out:

Notes:

Data :
a='.$a.' b= '.$b.'
'; //now 2 checksum bytes $x = $a - $b; if ($x < 0) { $x += 256; } elseif ($x > 255) { $x -= 256; } $y = $b - 2*$a; if ($y < 0) { $y += 256; } elseif ($y > 255) { $y -= 256; } $grid[$num-2] = $x; $grid[$num-1] = $y; echo 'x= ',$x.' y= '.$y.''; echo '

Packet to transmit: "'; $output = ''; for ($j=0; $j < $num; $j++) { $output = $output. $grid[$j].','; } $output = substr($output, 0, -1); echo $output.'"

'; } elseif (isset($_POST['confirm'])) { //confirm the checksums //go get the submitted data and clean it up $stuff = trim($_POST['data']); $grid = explode(',', $stuff); $num = count($grid); $a = 0; $b = 0; //total and total of totals for ($i=0; $i < $num ; $i++) { $a = ($a + $grid[$i]) % 256; $b = ($b + $a) % 256; } echo 'Data received: '.$stuff.'
'. ' a='.$a.' b= '.$b; if (($a==0) or ($b==0)) { echo '

CHECKSUMS MATCH: The packet arrived undamaged.

'; } else { echo '

CHECKSUM ERROR: The packet was damaged in transit.

'; } } else { echo '

Enter data then press calculate or confirm.

'; } ?>