Provide a mechanism for computing the CRC-32 checksum of a dataset.
More...
Namespaces |
namespace | bdlde |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a mechanism for computing the CRC-32 checksum of a dataset.
-
- Classes:
-
- See also:
-
- Description:
- This component implements a mechanism for computing, updating, and streaming a CRC-32 checksum (a cyclic redundancy check comprised of 32 bits). This checksum is a strong and fast technique for determining whether or not a message was received without errors. Note that a CRC-32 checksum does not aid in error correction and is not naively useful in any sort of cryptographic application. Compared to other methods such as MD5 and SHA-256, it is relatively easy to find alternate texts with identical checksum.
-
- Usage:
- The following snippets of code illustrate a typical use of the
bdlde::Crc32
class. Each function would typically execute in separate processes or potentially on separate machines. The senderExample
function below demonstrates how a message sender can write a message and its CRC-32 checksum to a bdex
output stream. Note that Out
may be a typedef
of any class that implements the bslx::OutStream
protocol: void senderExample(Out& output)
{
bsl::string message = "This is a test message.";
bdlde::Crc32 crc(message.data(), message.length());
output << message;
const int VERSION = 1;
crc.bdexStreamOut(output, VERSION);
}
The receiverExample
function below illustrates how a message receiver can read a message and its CRC-32 checksum from a bdex
input stream, then perform a local CRC-32 computation to verify that the message was received intact. Note that In
may be a typedef
of any class that implements the bslx::InStream
protocol: void receiverExample(In& input)
{
bsl::string message;
input >> message;
bdlde::Crc32 crc;
const int VERSION = 1;
crc.bdexStreamIn(input, VERSION);
bdlde::Crc32 crcLocal;
crcLocal.update(message.data(), message.length());
assert(crcLocal == crc);
}