Adler32

From Crypto++ Wiki
Jump to navigation Jump to search
Adler32
Documentation
#include <cryptopp/adler32.h>

Adler32 is a checksum algorithm. The zLib algorithm and ZlibCompressor‏‎ use Adler32 as its check-summing function.

All Crypto++ hashes derive from HashTransformation. The base class provides functions like Update, Final and Verify. You can swap-in any hash for any other hash in your program. You can also use ChannelSwitch to send data to multiple hashes at the same time.

Sample Programs

There are five sample programs. The first prints information about the checksum. The second creates a crc code using Adler32 class. The third creates a crc code using a pipeline. The fourth and fifth examples show how to verify an existing digest.

The examples below use Adler32, but you can swap-in any hash function, like PanamaHash or SM3.

The first example dumps the name, digest size and internal block size of Adler32.

#include "cryptlib.h"
#include "adler32.h"
#include <iostream>

int main (int argc, char* argv[])
{
    using namespace CryptoPP;

    Adler32 adler32;	
    std::cout << "Name: " << adler32.AlgorithmName() << std::endl;
    std::cout << "Digest size: " << adler32.DigestSize() << std::endl;
    std::cout << "Block size: " << adler32.BlockSize() << std::endl;

    return 0; 
}

Running the program results in the following. In general you should use DigestSize and avoid BlockSize. BlockSize is usually not required by a program. In the case of Adler32 the block size is 0.

$ ./test.exe
Name: Adler32
Digest size: 4
Block size: 0

The second example creates a hash using the hash object and member functions. You add data using Update and you calculate the hash using Final. Calling Final resets the hash so you don't need to do it manually.

using namespace CryptoPP;
HexEncoder encoder(new FileSink(std::cout));

std::string msg = "Yoda said, Do or do not. There is no try.";
std::string digest;

Adler32 adler32;
adler32.Update((const byte*)&msg[0], msg.size());
digest.resize(adler32.DigestSize());
adler32.Final((byte*)&digest[0]);

std::cout << "Message: " << msg << std::endl;

std::cout << "Digest: ";
StringSource(digest, true, new Redirector(encoder));
std::cout << std::endl;

Running the program results in the following.

$ ./test.exe
Message: Yoda said, Do or do not. There is no try.
Digest: 1C5A0D9F

You can also obtain a truncated hash rather than the full hash using TruncatedFinal.

std::cout << "Message: " << msg << std::endl;

adler32.Update((const byte*)&msg[0], msg.size());
digest.resize(adler32.DigestSize()/2);
adler32.TruncatedFinal((byte*)&digest[0], digest.size());

std::cout << "Digest: ";
StringSource(digest, true, new Redirector(encoder));
std::cout << std::endl;

The program produces the following result.

$ ./test.exe
Message: Yoda said, Do or do not. There is no try.
Digest: 1C5A

Using a pipeline produces the same result. It relieves you of calling Update and Final manually. The code also uses a HashFilter, which has its own wiki page at HashFilter.

std::string msg = "Yoda said, Do or do not. There is no try.";
std::string digest;

StringSource(msg, true, new HashFilter(hash, new StringSink(digest)));

std::cout << "Message: " << msg << std::endl;

std::cout << "Digest: ";
StringSource(digest, true, new Redirector(encoder));
std::cout << std::endl;

Running the program results in the following.

$ ./test.exe
Message: Yoda said, Do or do not. There is no try.
Digest: 1C5A0D9F

The fourth program verifies an existing hash using the hash object. Notice the program proceeds as if the hash is going to be calculated. But rather than calling Final to retrieve the hash, Verify is called to verify the existing adler32.

Adler32 adler32;
adler32.Update((const byte*)&msg[0], msg.size());
bool verified = adler32.Verify((const byte*)digest.data());

if (verified == true)
    std::cout << "Verified hash over message" << std::endl;
else
    std::cout << "Failed to verify hash over message" << std::endl;

The final program verifies an existing hash using a pipeline. The code uses a HashVerificationFilter, which has its own wiki page at HashVerificationFilter.

bool result;
StringSource(digest+msg, true, new HashVerificationFilter(hash,
                 new ArraySink((byte*)&result, sizeof(result))));

if (result == true)
    std::cout << "Verified hash over message" << std::endl;
else
    std::cout << "Failed to verify hash over message" << std::endl;

Running the program results in the following output.

$ ./test.exe
Message: Yoda said, Do or do not. There is no try.
Digest: 1C5A0D9F
Verified hash over message

Downloads

No downloads available.