title: Validate 1500 bytes of memory quickly via checksum
body: <p>I am writing a program in C++ that is sending 1500 bytes of data from machine A to machine B.</p>
  
  <p>Assume the following:</p>
  
  <pre><code>char* tx_data = (char*)operator new(1500)
  for (int i = 0; i &lt; 1500; i++) {
    tx_data[i] = (char)((int) 65); // ASCII 65 = A;
  }
  send_tx_data();
  </code></pre>
  
  <p>So we have the letter 'A' 1500 times to fill this buffer (<code>tx_data</code>). The receiving host grabs incoming data and dumps it into a buffer (<code>rx_data</code>), and grabs the length of the received data (<code>rxLength</code>);</p>
  
  <pre><code>rxLength = recvfrom(sock, rxdata, 1500, 0, NULL, NULL);
             //             ^ the buffer we are putting into
  </code></pre>
  
  <p>If <code>rxLength == 1500</code>, we have received 1500 bytes of data, but we need to be sure it's the same 1500 bytes we sent (and not 1500 bytes of data from else where, flying round the network)! Typically when comparing some small amount of user input for example, <code>strncmp()</code> might be used. I'm not suggestion that <code>strncmp()</code> is appropriate here, what I am saying though, is that I don't think it's a good idea to have on the receiving end, a buffer that might be called <code>expected_data</code> for example, that contains 1500 x 'A' and I loop through and compare the two, like <code>strncmp()</code>.</p>
  
  <p>How can I evaluate the 1500 bytes of data received in an efficient manner? [By efficient I mean quickly. This will be happening thousands of times a second, so the code needs to be fairly optimal!]. I had an idea about a checksum of some sort, as we know what we should be receiving, but I'm not sure of a good way to do this. Can anyone suggest a good checksum method? Alternatively, if that is a silly idea, could you explain why and perhaps recommend something else?</p>